| 1 | /* |
| 2 | * Created on Aug 22, 2004 |
| 3 | * |
| 4 | * TODO To change the template for this generated file go to |
| 5 | * Window - Preferences - Java - Code Style - Code Templates |
| 6 | */ |
| 7 | package biz.xsoftware.buildtemplate; |
| 8 | |
| 9 | import java.io.File; |
| 10 | import java.io.FileInputStream; |
| 11 | import java.io.FileOutputStream; |
| 12 | import java.io.IOException; |
| 13 | import java.net.URL; |
| 14 | import java.util.Arrays; |
| 15 | import java.util.List; |
| 16 | import java.util.Properties; |
| 17 | import java.util.StringTokenizer; |
| 18 | import java.util.jar.JarFile; |
| 19 | import java.util.logging.ConsoleHandler; |
| 20 | import java.util.logging.Handler; |
| 21 | import java.util.logging.Level; |
| 22 | import java.util.logging.Logger; |
| 23 | |
| 24 | /** |
| 25 | * @author Dean Hiller |
| 26 | * |
| 27 | * TODO To change the template for this generated type comment go to |
| 28 | * Window - Preferences - Java - Code Style - Code Templates |
| 29 | */ |
| 30 | public class Main { |
| 31 | |
| 32 | private final static Logger log = Logger.getLogger(Main.class.getName()); |
| 33 | public final static int USAGE_INCORRECT = 2; |
| 34 | public final static String f = File.separator; |
| 35 | public final static String TOOLS_DIR = "tools"; |
| 36 | public final static String TEMPLATE_JAR_FILE = TOOLS_DIR+f+"buildtemplate.jar"; |
| 37 | public final static String JUNIT_JAR = "input"+f+"lib"+f+"tools"+f+"junit.jar"; |
| 38 | public final static String JUNIT_ANT = TOOLS_DIR+f+"ant-optional"+f+"ant-junit.jar"; |
| 39 | private final static String VERSION_FILE = TOOLS_DIR+"/buildtemplate.version"; |
| 40 | |
| 41 | private static boolean testing = false; |
| 42 | |
| 43 | private Version version = new VersionImpl(); |
| 44 | private Exit exit = new ExitImpl(); |
| 45 | private FileLocator fileLocator = new Util(); |
| 46 | private Shutdown shutdown; |
| 47 | |
| 48 | public static void main(String[] args) throws Exception { |
| 49 | Main m = new Main(); |
| 50 | m.start(args); |
| 51 | } |
| 52 | |
| 53 | public void setVersion(Version v) { |
| 54 | version = v; |
| 55 | } |
| 56 | public void setExit(Exit e) { |
| 57 | exit = e; |
| 58 | } |
| 59 | public void setFileLocator(FileLocator f) { |
| 60 | fileLocator = f; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Usage of template after installation.... |
| 65 | * ./build -version |
| 66 | * prints version of just ant. |
| 67 | * |
| 68 | * ./build <target> |
| 69 | * extracts template, runs <target> on ant, then compresses template back |
| 70 | * |
| 71 | * java -jar buildtemplate.jar -version |
| 72 | * prints version of buildtemplate. |
| 73 | * |
| 74 | * To enable the above, build script has to call |
| 75 | * java -jar buildtemplate.jar dummyParam $@ leaving room for -version |
| 76 | * ie. if -version is the first param in args param below, user wants the |
| 77 | * buildtemplate version. If it is the second param, user wants the |
| 78 | * ant version. |
| 79 | * |
| 80 | * java -jar buildtemplate.jar -initDir=<dir> |
| 81 | * This is how someone creates the template for the first time. If |
| 82 | * <dir> does not exist, it will be created. |
| 83 | */ |
| 84 | public void start(String[] args) throws Exception { |
| 85 | try { |
| 86 | startImpl(args); |
| 87 | } catch(Exception e) { |
| 88 | log.log(Level.WARNING, "Exception occurred in build template", e); |
| 89 | throw e; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | public void startImpl(String[] args) throws Exception { |
| 94 | |
| 95 | //this is kind of not nice...if someone wants to change the formatter of |
| 96 | //the ConsoleHandler, they can't because we reset it here. What should we |
| 97 | //do instead though to pretty up the output by default? |
| 98 | Handler[] h = Logger.getLogger("").getHandlers(); |
| 99 | for(int i = 0; i < h.length; i++) { |
| 100 | if(h[i] instanceof ConsoleHandler) { |
| 101 | h[i].setFormatter(new LogFormatter()); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | List list = Arrays.asList(args); |
| 106 | log.info("params supplied to buildtemplate="+list); |
| 107 | |
| 108 | boolean wantCompress = true; |
| 109 | if(args.length < 1) { |
| 110 | displayHelp(); |
| 111 | exit.exit(USAGE_INCORRECT); |
| 112 | } else if("-installed".equals(args[0])) { |
| 113 | String[] antArgs; |
| 114 | if(args.length > 1 && "-nocompress".equals(args[1])) { |
| 115 | wantCompress = false; |
| 116 | antArgs = new String[args.length-2]; |
| 117 | System.arraycopy(args, 2, antArgs, 0, antArgs.length); |
| 118 | } else { |
| 119 | antArgs = new String[args.length-1]; |
| 120 | System.arraycopy(args, 1, antArgs, 0, antArgs.length); |
| 121 | } |
| 122 | String userDirProp = System.getProperty("user.dir"); |
| 123 | File userDir = new File(userDirProp); |
| 124 | startBuild(userDir, antArgs, wantCompress); |
| 125 | } else if("-directory".equals(args[0])){ |
| 126 | //template is not installed yet, must copy this jar to proper location |
| 127 | if(args.length < 2) { |
| 128 | displayHelp(); |
| 129 | exit.exit(1); |
| 130 | } |
| 131 | String directory = args[1]; |
| 132 | File baseDir = new File(directory); |
| 133 | File f = new File(directory,TOOLS_DIR); |
| 134 | f.mkdirs(); |
| 135 | URL url = Main.class.getResource("Main.class"); |
| 136 | File me = fileLocator.getFile(url); |
| 137 | |
| 138 | File dest = new File(baseDir, TEMPLATE_JAR_FILE); |
| 139 | if(!me.getCanonicalPath().equals(dest.getCanonicalPath())) { |
| 140 | log.finer("copying jar from="+me.getCanonicalPath()); |
| 141 | log.finer("copying jar to ="+dest.getCanonicalPath()); |
| 142 | Util.copyFile(me, dest); |
| 143 | } |
| 144 | startBuild(baseDir, new String[] {"clean"}, wantCompress); |
| 145 | } else { |
| 146 | displayHelp(); |
| 147 | exit.exit(USAGE_INCORRECT); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | |
| 152 | |
| 153 | public void startBuild(File userDir, String[] antArgs, boolean wantCompress) throws Exception { |
| 154 | log.info("running from baseDir="+userDir.getCanonicalPath()); |
| 155 | |
| 156 | String templateFile = TOOLS_DIR+"/buildtemplate.jar"; |
| 157 | File templateJarFile = new File(userDir, templateFile); |
| 158 | if(!templateJarFile.exists()) { |
| 159 | log.severe("File='"+templateFile+"' does not exist, user setup problem, please talk to buildtemplate owner"); |
| 160 | exit.exit(1); |
| 161 | } |
| 162 | |
| 163 | JarFile jarFile = new JarFile(templateJarFile); |
| 164 | String newVersion = getNewVersionOfBuildTemplate(userDir, jarFile); |
| 165 | jarFile.close(); |
| 166 | |
| 167 | boolean isNewVersion = newVersion != null; |
| 168 | if(isNewVersion) { |
| 169 | System.err.println(); |
| 170 | log.info("********************************************"); |
| 171 | log.info("Template upgrade to release="+newVersion); |
| 172 | log.info("Preparing to install new goodies and bug fixes"); |
| 173 | log.info("********************************************"); |
| 174 | System.err.println(); |
| 175 | } |
| 176 | |
| 177 | Properties prevTemplate = new Properties(); |
| 178 | File f = new File(userDir, VERSION_FILE); |
| 179 | if(f.exists()) { |
| 180 | FileInputStream prevIn = new FileInputStream(f); |
| 181 | prevTemplate.load(prevIn); |
| 182 | prevIn.close(); |
| 183 | } else |
| 184 | f.createNewFile(); |
| 185 | |
| 186 | String oldVersion = prevTemplate.getProperty("version"); |
| 187 | |
| 188 | RunAnt ant = new RunAnt(); |
| 189 | BuildTemplate template = new BuildTemplate(templateJarFile, userDir); |
| 190 | |
| 191 | prepareShutdown(template, ant, wantCompress); |
| 192 | |
| 193 | //an early test so we can catch problems now instead of halfway through |
| 194 | //the extraction of the template leaving it half installed |
| 195 | template.testExtraction(); |
| 196 | //extract |
| 197 | FileBean[] files = template.extract(); |
| 198 | //run ant |
| 199 | ant.run(userDir, antArgs); |
| 200 | |
| 201 | //if is a new template or user edited a generated file, we need to |
| 202 | //update the versioning properties file.... |
| 203 | if(files.length > 0){ |
| 204 | if(isNewVersion) { |
| 205 | System.err.println(); |
| 206 | System.err.println(); |
| 207 | log.info("*********************PLEASE NOTE************************"); |
| 208 | log.info("We have noticed you just upgraded you buildtemplate.jar"); |
| 209 | log.info("Please check in the following files to your SCM..."); |
| 210 | |
| 211 | log.info("file=<project>/tools/buildtemplate.jar"); |
| 212 | log.info("file=<project>/tools/buildtemplate.version"); |
| 213 | for(int i = 0; i < files.length; i++) { |
| 214 | log.info("file=<project>/"+files[i].getName()); |
| 215 | } |
| 216 | } |
| 217 | //diff extracted files with files in the properties file and report |
| 218 | |
| 219 | //list all permanent files |
| 220 | FileOutputStream out = new FileOutputStream(f); |
| 221 | |
| 222 | if(newVersion== null) |
| 223 | newVersion = oldVersion; |
| 224 | |
| 225 | Properties p = new Properties(); |
| 226 | p.setProperty("version", newVersion); |
| 227 | for(int i = 0; i < files.length; i++) { |
| 228 | p.setProperty("file"+i, files[i].getName()); |
| 229 | p.setProperty(files[i].getName(), ""+files[i].getLastMod()); |
| 230 | } |
| 231 | |
| 232 | p.store(out, "DO NOT EDIT THIS FILE, IT IS GENERATED"); |
| 233 | out.close(); |
| 234 | } |
| 235 | } |
| 236 | public void prepareShutdown(BuildTemplate template, RunAnt ant, boolean wantCompress) { |
| 237 | shutdown = new Shutdown(template, ant, wantCompress); |
| 238 | if(!testing) |
| 239 | Runtime.getRuntime().addShutdownHook(new Shutdown(template, ant, wantCompress)); |
| 240 | } |
| 241 | |
| 242 | public static void setTesting() { |
| 243 | testing = true; |
| 244 | } |
| 245 | public void shutdown() { |
| 246 | shutdown.run(); |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * |
| 251 | * @param dir |
| 252 | * @param templateJarFile |
| 253 | * @return The new version if buildtemplate was upgraded, else return null. |
| 254 | * @throws IOException |
| 255 | */ |
| 256 | private String getNewVersionOfBuildTemplate(File dir, JarFile zip) throws IOException { |
| 257 | |
| 258 | String newVersion = version.getVersion(); |
| 259 | log.fine("version in jar file="+newVersion); |
| 260 | |
| 261 | File versionFile = new File(dir, VERSION_FILE); |
| 262 | if(!versionFile.exists()) |
| 263 | return newVersion; |
| 264 | |
| 265 | FileInputStream oldIn = new FileInputStream(versionFile); |
| 266 | |
| 267 | //get old version number...... |
| 268 | Properties oldProps = new Properties(); |
| 269 | oldProps.load(oldIn); |
| 270 | String oldVersion = oldProps.getProperty("version"); |
| 271 | log.fine("old version="+oldVersion); |
| 272 | |
| 273 | oldIn.close(); |
| 274 | |
| 275 | if(isLeftNewerThanRight(newVersion.trim(), oldVersion.trim())) |
| 276 | return newVersion; |
| 277 | |
| 278 | return null; |
| 279 | } |
| 280 | |
| 281 | private boolean isLeftNewerThanRight(String newVersion, String oldVersion) { |
| 282 | if("Developer-Build".equals(newVersion) |
| 283 | || "Developer-Build".equals(oldVersion)) |
| 284 | return true; |
| 285 | |
| 286 | try { |
| 287 | StringTokenizer newTok = new StringTokenizer(newVersion, "r-"); |
| 288 | StringTokenizer oldTok = new StringTokenizer(oldVersion, "r-"); |
| 289 | |
| 290 | while(newTok.hasMoreTokens()) { |
| 291 | newVersion = newTok.nextToken(); |
| 292 | oldVersion = oldTok.nextToken(); |
| 293 | |
| 294 | int newInt = Integer.parseInt(newVersion); |
| 295 | int oldInt = Integer.parseInt(oldVersion); |
| 296 | |
| 297 | if(newInt > oldInt) |
| 298 | return true; |
| 299 | } |
| 300 | |
| 301 | return false; |
| 302 | } catch(Throwable e) { |
| 303 | log.log(Level.WARNING,"Corrupt version file, installing this template", e); |
| 304 | return true; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | private void displayHelp() { |
| 309 | System.err.println(); |
| 310 | log.info("Use as follows...."); |
| 311 | log.info("java -jar buildtemplate.jar -directory <directory>"); |
| 312 | log.info(" NOTE: <directory> will be created if it does not exist"); |
| 313 | log.info("or"); |
| 314 | log.info("java -jar buildtemplate.jar -version"); |
| 315 | log.info("or"); |
| 316 | log.info("java -jar buildtemplate.jar -manifest"); |
| 317 | log.info("or"); |
| 318 | log.info("if upgrading, just copy the buildtemplate.jar over the"); |
| 319 | log.info("old one and run ./build from root directory of project"); |
| 320 | System.err.println(); |
| 321 | } |
| 322 | |
| 323 | // private void createNewProject(String directory) throws Exception { |
| 324 | // System.out.println(); |
| 325 | // String userDir = System.getProperty("user.dir"); |
| 326 | // System.out.println("Running from dir="+userDir); |
| 327 | // |
| 328 | // File srcTemplateFile = new File(userDir, "buildtemplate.jar"); |
| 329 | // File dir = new File(directory); |
| 330 | // if(!srcTemplateFile.exists()) { |
| 331 | // System.err.println("Could not find file='buildtemplate.jar' in dir="+userDir); |
| 332 | // System.err.println("Please run java -jar buildtemplate.jar from the location of the jar file"); |
| 333 | // exit.exit(1); |
| 334 | // } else if(!dir.exists()) { |
| 335 | // System.out.println("Creating directory="+dir.getCanonicalPath()); |
| 336 | // dir.mkdirs(); |
| 337 | // } else if(!dir.isDirectory()) { |
| 338 | // System.err.println("ERROR: <directory>="+directory+" is not a directory. try again"); |
| 339 | // displayHelp(); |
| 340 | // exit.exit(1); |
| 341 | // } |
| 342 | // |
| 343 | // JarFile zip = new JarFile(srcTemplateFile); |
| 344 | // if(isNewVersionOfBuildTemplate(dir, zip)) { |
| 345 | // System.out.println("Extracting build template to directory="+directory); |
| 346 | // //explodeJar(zip, dir); |
| 347 | // |
| 348 | // copyJarFile(srcTemplateFile, dir); |
| 349 | // |
| 350 | // System.out.println(); |
| 351 | // System.out.println("*********************************************************"); |
| 352 | // System.out.println("Congratulations...You now have installed or upgraded the"); |
| 353 | // System.out.println("build template. In the future, to upgrade, simply drop"); |
| 354 | // System.out.println("a new version of the buildtemplate.jar into the TOOLS"); |
| 355 | // System.out.println("directory and run ./build from the root directory of"); |
| 356 | // System.out.println("the build template"); |
| 357 | // System.out.println("*********************************************************"); |
| 358 | // } else |
| 359 | // System.out.println("Build Template has not been upgraded, so it will not be extracted"); |
| 360 | //} |
| 361 | } |