Index: maven-commandline-plugin/src/java/org/codelutin/util/Generate.java diff -u maven-commandline-plugin/src/java/org/codelutin/util/Generate.java:1.4 maven-commandline-plugin/src/java/org/codelutin/util/Generate.java:1.5 --- maven-commandline-plugin/src/java/org/codelutin/util/Generate.java:1.4 Thu Dec 13 10:35:49 2007 +++ maven-commandline-plugin/src/java/org/codelutin/util/Generate.java Fri Dec 14 19:06:04 2007 @@ -1,25 +1,27 @@ -/** - * ##% Copyright (C) 2002, 2007 Code Lutin This program is free software; you - * can redistribute it and/or modify it under the terms of the GNU General - * Public License as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. This program is - * distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; - * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A - * PARTICULAR PURPOSE. See the GNU General Public License for more details. You - * should have received a copy of the GNU General Public License along with this - * program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - * - Suite 330, Boston, MA 02111-1307, USA. ##%* - */ - +/* +* ##% Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 Code Lutin, +* Benjamin Poussin, Tony Chemit +* +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +* ##% */ package org.codelutin.util; -import javassist.CannotCompileException; import javassist.ClassClassPath; -import javassist.ClassPath; import javassist.ClassPool; import javassist.CtClass; -import javassist.CtMethod; -import javassist.NotFoundException; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; @@ -27,6 +29,7 @@ import org.codelutin.i18n.I18n; import static org.codelutin.i18n.I18n._; import org.codelutin.util.OptionParserAnnotationHelper.ApplicationA; +import org.codelutin.util.generator.OptionParserGenerator; import java.io.BufferedWriter; import java.io.File; @@ -34,6 +37,7 @@ import java.io.IOException; import java.io.StringWriter; import java.io.Writer; +import static java.lang.System.getProperty; /** * Permet de générer la factory de définitions d'options de ligne de commande, @@ -47,8 +51,7 @@ static { // init I18N - I18n.init(System.getProperty("user.language"), - System.getProperty("user.country")); + I18n.init(getProperty("user.language"), getProperty("user.country")); } protected Log log = getLog(); @@ -86,16 +89,22 @@ * @parameter expression="${commandLine.rstFilePath}" default-value="${basedir}/src/site/fr/rst/${project.name}_usage.rst" */ protected File rstFilePath; + /** * @description flag to generate rst file. * @parameter expression="${commandLine.generateRst}" default-value="true" */ protected boolean generateRst; /** + * @description flag to force generation of class. + * @parameter expression="${commandLine.force}" default-value="false" + */ + protected boolean force; + /** * @description flag to generate or not specialized Option - * @parameter expression="${commandLine.generateOptionImplementation}" default-value="false" + * @parameter expression="${commandLine.generateOptions}" default-value="false" */ - protected boolean generateOptionImplementation; + protected boolean generateOptions; /** * @description flag to show errors of parsing. * @parameter expression="${commandLine.showError}" default-value="false" @@ -112,12 +121,13 @@ // get source (make sure to be on a absolute path) source = new File(source.getAbsolutePath()); - /*if (isUpToDate()) { - log.info(_("commandline.parser.uptodate",parserFullyQualifiedName)); + if (isUpToDate()) { + log.info(_("commandline.parser.uptodate", parserFullyQualifiedName)); return; - }*/ + } CtClass clazz = null; + OptionParserGenerator generator=null; try { // parse source OptionDefinitionParser parser = doParse(); @@ -128,28 +138,26 @@ if (parser.hasFailed()) { throw new RuntimeException(_("commandline.parser.parsing.error", parser, parser.errors.length)); } - - // init Class pool and make backups of generated class (@see backupClass) + + // init Class pool ClassPool pool = initClassPool(); - // instanciate a OptionParser generator - OptionParserGenerator parserGenerator = new OptionParserGenerator( - out.getAbsolutePath(), parserFullyQualifiedName, - generateOptionImplementation - ); + // instanciate generator + generator = new OptionParserGenerator(out.getAbsolutePath(), generateOptions, backupClass? System.currentTimeMillis():0); ApplicationA anno; try { // generate OptionParser implementation and return applicationA - anno = parserGenerator.generateOptionParser(pool, parser); + anno = generator.generate(pool, parserFullyQualifiedName, parser); + + // build usage rst file (@see generateRst) + generateRst(anno); } catch (Exception e) { throw new MojoExecutionException(e.getMessage(), e); } - - // build usage rst file (@see generateRst) - generateRst(anno); - } finally { - detachClazz(clazz); + if (generator!=null) { + generator.detachClazz(clazz); + } } } @@ -166,8 +174,8 @@ } catch (Exception e) { // ignore here (will be treate by parser) } - // do parse definitions try { + // do parse definitions and return parser OptionDefinitionParser parser = OptionDefinitionParser.doParse(typeP, source); log.info(_("commandline.parser.result.info", parser, parser.options.length)); return parser; @@ -177,7 +185,10 @@ } protected boolean isUpToDate() { - //TOOD check if parser exists, and is not older than source + if (force) { + return false; + } + //TODO check if parser exists, and is not older than source long newtimestamp = source.lastModified(); int lastIndex = parserFullyQualifiedName.lastIndexOf('.'); String path; @@ -191,18 +202,14 @@ } else { path = parserFullyQualifiedName; } - File f = new File(out, path+".class"); + File f = new File(out, path + ".class"); return f.exists() && newtimestamp <= f.lastModified(); } protected ClassPool initClassPool() throws MojoFailureException { // create new root class pool ClassPool pool = new ClassPool(null); - - // backup previously generated classes if asked - //TODO Backup also OptionImpl ? try { - backupPreviousGeneratedClass(pool, parserFullyQualifiedName); // we need the classpath of OptionParser, since we load it as super // class of generated classe, make sure pool know it. pool.appendClassPath(new ClassClassPath(OptionParser.class)); @@ -215,7 +222,7 @@ } } - protected void generateRst(ApplicationA applicationA) throws MojoFailureException { + protected void generateRst(ApplicationA applicationA) throws IOException { if (rstFilePath == null) { return; } @@ -223,27 +230,17 @@ rstFilePath.getParentFile().mkdirs(); } Writer w; - try { - w = new BufferedWriter(new FileWriter(rstFilePath)); - } catch (IOException e) { - throw new MojoFailureException(e.getMessage()); - } + w = new BufferedWriter(new FileWriter(rstFilePath)); log.info(_("commandline.generateRstFile.info", rstFilePath)); try { String head = _("commandline.generateRstFile.head", projectName); String prefixO = _("commandline.generateRstFile.options.head", projectName); String prefix = _("commandline.generateRstFile.prefix") + ' '; w.append(OptionParserUtil.toString(applicationA, head, prefixO, prefix)); - } catch (IOException e) { - throw new MojoFailureException(e.getMessage()); } finally { if (w != null) { - try { - w.flush(); - w.close(); - } catch (IOException e) { - throw new MojoFailureException(e.getMessage()); - } + w.flush(); + w.close(); } } } @@ -268,51 +265,4 @@ } } } - - protected String buildMethodName(boolean booleanValueType, String methodName) { - methodName = (booleanValueType ? "is" : "get") + methodName; - return methodName; - } - - protected void addMethod(CtClass impl, String prefix, String methodName, String body) throws CannotCompileException { - CtMethod method; - String b = prefix + " " + methodName + "() {" + body + "}"; - //log.user(b); - method = CtMethod.make(b, impl); - impl.addMethod(method); - } - - protected void detachClazz(CtClass... impl) { - for (CtClass ctClass : impl) { - if (ctClass != null) { - ctClass.detach(); - } - } - } - - protected void backupPreviousGeneratedClass(ClassPool pool, String... classNames) throws NotFoundException, CannotCompileException, IOException { - if (!backupClass) { - return; - } - ClassPath cp = pool.insertClassPath(out.getAbsolutePath()); - String SUFFIX = "_" + System.currentTimeMillis(); - for (String name : classNames) { - try { - CtClass clazz = pool.get(name); - if (clazz != null) { - // rename this clazz to be safe - String newClassName = name + SUFFIX; - clazz.replaceClassName(name, newClassName); - log.info(_("commandline.backupClass", name, newClassName)); - clazz.writeFile(out.getAbsolutePath()); - } - } catch (NotFoundException e) { - // we are safe : class does not exists in class path, - // so no collision is possible :) - } - } - cp.close(); - pool.removeClassPath(cp); - } - -} +} \ No newline at end of file