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.ArgHandlerExtra; 22 import com.google.gwt.util.tools.ArgHandlerOutDir; 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 35 public final class ApplicationCreator extends ToolBase { 36 37 41 protected class ArgHandlerAppClass extends ArgHandlerExtra { 42 43 public boolean addExtraArg(String arg) { 44 if (fullClassName != null) { 45 System.err.println("Too many arguments"); 46 return false; 47 } 48 49 if (!arg.matches("[\\w\\$]+(\\.[\\w\\$]+)+")) { 51 System.err.println("'" + arg 52 + "' does not appear to be a valid fully-qualified Java class name"); 53 return false; 54 } 55 56 if (arg.indexOf('$') != -1) { 59 System.err.println("'" + arg 60 + "': This version of the tool does not support nested classes"); 61 return false; 62 } 63 64 String [] parts = arg.split("\\."); 65 if (parts.length < 2 || !parts[parts.length - 2].equals("client")) { 66 System.err.println("'" 67 + arg 68 + "': Please use 'client' as the final package, as in 'com.example.foo.client.MyApp'.\n" 69 + "It isn't technically necessary, but this tool enforces the best practice."); 70 return false; 71 } 72 73 fullClassName = arg; 74 return true; 75 } 76 77 public String getPurpose() { 78 return "The fully-qualified name of the application class to create"; 79 } 80 81 public String [] getTagArgs() { 82 return new String [] {"className"}; 83 } 84 85 public boolean isRequired() { 86 return true; 87 } 88 } 89 90 private static final String PACKAGE_PATH; 91 92 static { 93 String path = ApplicationCreator.class.getName(); 94 path = path.substring(0, path.lastIndexOf('.') + 1); 95 PACKAGE_PATH = path.replace('.', '/'); 96 } 97 98 public static void main(String [] args) { 99 ApplicationCreator creator = new ApplicationCreator(); 100 if (creator.processArgs(args)) { 101 if (creator.run()) { 102 return; 103 } 104 } 105 System.exit(1); 106 } 107 108 117 static void createApplication(String fullClassName, File outDir, 118 String eclipse, boolean overwrite, boolean ignore) throws IOException { 119 120 String installPath = Utility.getInstallPath(); 122 String gwtUserPath = installPath + '/' + "gwt-user.jar"; 123 String gwtDevPath = installPath + '/' + Utility.getDevJarName(); 124 125 boolean isWindows = gwtDevPath.substring(gwtDevPath.lastIndexOf('/') + 1).indexOf( 128 "windows") >= 0; 129 boolean isMacOsX = gwtDevPath.substring(gwtDevPath.lastIndexOf('/') + 1).indexOf( 130 "mac") >= 0; 131 132 String basePathEnv; 136 if (!new File (installPath).isAbsolute()) { 137 if (isWindows) { 138 basePathEnv = "%~dp0\\"; 139 } else { 140 basePathEnv = "$APPDIR/"; 141 } 142 } else { 143 basePathEnv = ""; 144 } 145 146 int pos = fullClassName.lastIndexOf('.'); 149 String clientPackageName = fullClassName.substring(0, pos); 150 String className = fullClassName.substring(pos + 1); 151 152 pos = clientPackageName.lastIndexOf('.'); 155 File basePackageDir; 156 String moduleName; 157 File javaDir = Utility.getDirectory(outDir, "src", true); 158 if (pos >= 0) { 159 String basePackage = clientPackageName.substring(0, pos); 160 moduleName = basePackage + "." + className; 161 basePackage = basePackage.replace('.', '/'); 162 basePackageDir = Utility.getDirectory(javaDir, basePackage, true); 163 } else { 164 moduleName = className; 165 basePackageDir = javaDir; 166 } 167 File clientDir = Utility.getDirectory(basePackageDir, "client", true); 168 File publicDir = Utility.getDirectory(basePackageDir, "public", true); 169 String startupUrl = moduleName + "/" + className + ".html"; 170 171 Map replacements = new HashMap (); 174 replacements.put("@className", className); 175 replacements.put("@moduleName", moduleName); 176 replacements.put("@clientPackage", clientPackageName); 177 replacements.put("@gwtUserPath", basePathEnv + gwtUserPath); 178 replacements.put("@gwtDevPath", basePathEnv + gwtDevPath); 179 replacements.put("@shellClass", "com.google.gwt.dev.GWTShell"); 180 replacements.put("@compileClass", "com.google.gwt.dev.GWTCompiler"); 181 replacements.put("@startupUrl", startupUrl); 182 replacements.put("@vmargs", isMacOsX ? "-XstartOnFirstThread" : ""); 183 184 { 185 File moduleXML = Utility.createNormalFile(basePackageDir, className 187 + ".gwt.xml", overwrite, ignore); 188 if (moduleXML != null) { 189 String out = Utility.getFileFromClassPath(PACKAGE_PATH 190 + "Module.gwt.xmlsrc"); 191 Utility.writeTemplateFile(moduleXML, out, replacements); 192 } 193 } 194 195 { 196 File publicHTML = Utility.createNormalFile(publicDir, 198 className + ".html", overwrite, ignore); 199 if (publicHTML != null) { 200 String out = Utility.getFileFromClassPath(PACKAGE_PATH 201 + "AppHtml.htmlsrc"); 202 Utility.writeTemplateFile(publicHTML, out, replacements); 203 } 204 } 205 206 { 207 File javaClass = Utility.createNormalFile(clientDir, className + ".java", 209 overwrite, ignore); 210 if (javaClass != null) { 211 String out = Utility.getFileFromClassPath(PACKAGE_PATH 212 + "AppClassTemplate.javasrc"); 213 Utility.writeTemplateFile(javaClass, out, replacements); 214 } 215 } 216 217 if (eclipse != null) { 218 replacements.put("@projectName", eclipse); 220 File launchConfig = Utility.createNormalFile(outDir, className 221 + ".launch", overwrite, ignore); 222 if (launchConfig != null) { 223 String out = Utility.getFileFromClassPath(PACKAGE_PATH 224 + "App.launchsrc"); 225 Utility.writeTemplateFile(launchConfig, out, replacements); 226 } 227 } 228 229 String extension; 231 if (isWindows) { 232 extension = ".cmd"; 233 } else { 234 extension = ""; 235 } 236 237 File gwtshell = Utility.createNormalFile(outDir, className + "-shell" 238 + extension, overwrite, ignore); 239 if (gwtshell != null) { 240 String out = Utility.getFileFromClassPath(PACKAGE_PATH + "gwtshell" 241 + extension + "src"); 242 Utility.writeTemplateFile(gwtshell, out, replacements); 243 if (extension.length() == 0) { 244 chmodExecutable(gwtshell); 245 } 246 } 247 248 File gwtcompile = Utility.createNormalFile(outDir, className + "-compile" 249 + extension, overwrite, ignore); 250 if (gwtcompile != null) { 251 String out = Utility.getFileFromClassPath(PACKAGE_PATH + "gwtcompile" 252 + extension + "src"); 253 Utility.writeTemplateFile(gwtcompile, out, replacements); 254 if (extension.length() == 0) { 255 chmodExecutable(gwtcompile); 256 } 257 } 258 } 259 260 267 private static void chmodExecutable(File file) { 268 try { 269 Runtime.getRuntime().exec("chmod u+x " + file.getAbsolutePath()); 270 } catch (Throwable e) { 271 System.err.println(("Warning: cannot exec chmod to set permission on generated file.")); 272 } 273 } 274 275 private String eclipse = null; 276 private String fullClassName = null; 277 private boolean ignore = false; 278 private File outDir; 279 private boolean overwrite = false; 280 281 protected ApplicationCreator() { 282 283 registerHandler(new ArgHandlerEclipse() { 284 public String getPurpose() { 285 return "Creates a debug launch config for the named eclipse project"; 286 } 287 288 public boolean setString(String str) { 289 eclipse = str; 290 return true; 291 } 292 }); 293 294 registerHandler(new ArgHandlerOutDir() { 295 public void setDir(File dir) { 296 outDir = dir; 297 } 298 }); 299 300 registerHandler(new ArgHandlerOverwrite() { 301 public boolean setFlag() { 302 if (ignore) { 303 System.err.println("-overwrite cannot be used with -ignore"); 304 return false; 305 } 306 overwrite = true; 307 return true; 308 } 309 }); 310 311 registerHandler(new ArgHandlerIgnore() { 312 public boolean setFlag() { 313 if (overwrite) { 314 System.err.println("-ignore cannot be used with -overwrite"); 315 return false; 316 } 317 ignore = true; 318 return true; 319 } 320 }); 321 322 registerHandler(new ArgHandlerAppClass()); 323 } 324 325 protected boolean run() { 326 try { 327 createApplication(fullClassName, outDir, eclipse, overwrite, ignore); 328 return true; 329 } catch (IOException e) { 330 System.err.println(e.getClass().getName() + ": " + e.getMessage()); 331 return false; 332 } 333 } 334 335 } 336 | Popular Tags |