1 16 package com.google.gwt.user.tools; 17 18 import com.google.gwt.user.tools.util.ArgHandlerEclipse; 19 import com.google.gwt.user.tools.util.ArgHandlerIgnore; 20 import com.google.gwt.user.tools.util.ArgHandlerOverwrite; 21 import com.google.gwt.util.tools.ArgHandlerOutDir; 22 import com.google.gwt.util.tools.ArgHandlerString; 23 import com.google.gwt.util.tools.ToolBase; 24 import com.google.gwt.util.tools.Utility; 25 26 import java.io.File ; 27 import java.io.IOException ; 28 import java.util.HashMap ; 29 import java.util.Map ; 30 31 34 public final class ProjectCreator extends ToolBase { 35 36 private static final String PACKAGE_PATH; 37 38 static { 39 String path = ProjectCreator.class.getName(); 40 path = path.substring(0, path.lastIndexOf('.') + 1); 41 PACKAGE_PATH = path.replace('.', '/'); 42 } 43 44 public static void main(String [] args) { 45 ProjectCreator creator = new ProjectCreator(); 46 if (creator.processArgs(args)) { 47 if (creator.run()) { 48 return; 49 } 50 } 51 52 System.exit(1); 53 } 54 55 63 static void createProject(String eclipse, String ant, File outDir, 64 boolean overwrite, boolean ignore) throws IOException { 65 66 String installPath = Utility.getInstallPath(); 68 69 Map replacements = new HashMap (); 72 replacements.put("@gwtUserPath", installPath + '/' + "gwt-user.jar"); 73 74 Utility.getDirectory(outDir, "src", true); 75 Utility.getDirectory(outDir, "test", true); 76 77 if (ant != null) { 78 replacements.put("@projectName", ant); 80 File antXML = Utility.createNormalFile(outDir, ant + ".ant.xml", 81 overwrite, ignore); 82 if (antXML != null) { 83 String out = Utility.getFileFromClassPath(PACKAGE_PATH 84 + "project.ant.xmlsrc"); 85 Utility.writeTemplateFile(antXML, out, replacements); 86 } 87 } 88 89 if (eclipse != null) { 90 replacements.put("@projectName", eclipse); 92 File dotProject = Utility.createNormalFile(outDir, ".project", overwrite, 93 ignore); 94 if (dotProject != null) { 95 String out = Utility.getFileFromClassPath(PACKAGE_PATH + ".projectsrc"); 96 Utility.writeTemplateFile(dotProject, out, replacements); 97 } 98 99 File dotClasspath = Utility.createNormalFile(outDir, ".classpath", 101 overwrite, ignore); 102 if (dotClasspath != null) { 103 String out = Utility.getFileFromClassPath(PACKAGE_PATH 104 + ".classpathsrc"); 105 Utility.writeTemplateFile(dotClasspath, out, replacements); 106 } 107 } 108 } 109 110 private String ant = null; 111 112 private String eclipse = null; 113 114 private boolean ignore = false; 115 private File outDir = null; 116 private boolean overwrite = false; 117 118 protected ProjectCreator() { 119 120 registerHandler(new ArgHandlerString() { 121 122 public String getPurpose() { 123 return "Generate an Ant buildfile to compile source (.ant.xml will be appended)"; 124 } 125 126 public String getTag() { 127 return "-ant"; 128 } 129 130 public String [] getTagArgs() { 131 return new String [] {"projectName"}; 132 } 133 134 public boolean setString(String str) { 135 ant = str; 136 return true; 137 } 138 139 }); 140 141 registerHandler(new ArgHandlerEclipse() { 142 public String getPurpose() { 143 return "Generate an eclipse project"; 144 } 145 146 public boolean setString(String str) { 147 eclipse = str; 148 return true; 149 } 150 }); 151 152 registerHandler(new ArgHandlerOutDir() { 153 public void setDir(File dir) { 154 outDir = dir; 155 } 156 }); 157 158 registerHandler(new ArgHandlerOverwrite() { 159 public boolean setFlag() { 160 if (ignore) { 161 System.err.println("-overwrite cannot be used with -ignore."); 162 return false; 163 } 164 overwrite = true; 165 return true; 166 } 167 }); 168 169 registerHandler(new ArgHandlerIgnore() { 170 public boolean setFlag() { 171 if (overwrite) { 172 System.err.println("-ignore cannot be used with -overwrite."); 173 return false; 174 } 175 ignore = true; 176 return true; 177 } 178 }); 179 } 180 181 protected boolean run() { 182 try { 183 if (ant == null && eclipse == null) { 184 System.err.println("Please specify either -ant or -eclipse."); 185 printHelp(); 186 return false; 187 } 188 createProject(eclipse, ant, outDir, overwrite, ignore); 189 return true; 190 } catch (IOException e) { 191 System.err.println(e.getClass().getName() + ": " + e.getMessage()); 192 return false; 193 } 194 } 195 } 196 | Popular Tags |