Index: maven-commandline-plugin/src/java/org/codelutin/option/generate/RstGeneratorGoal.java diff -u maven-commandline-plugin/src/java/org/codelutin/option/generate/RstGeneratorGoal.java:1.10 maven-commandline-plugin/src/java/org/codelutin/option/generate/RstGeneratorGoal.java:1.11 --- maven-commandline-plugin/src/java/org/codelutin/option/generate/RstGeneratorGoal.java:1.10 Tue Mar 18 22:56:32 2008 +++ maven-commandline-plugin/src/java/org/codelutin/option/generate/RstGeneratorGoal.java Wed Mar 19 20:21:42 2008 @@ -20,90 +20,122 @@ import org.codelutin.i18n.I18n; import static org.codelutin.i18n.I18n._; +import org.codelutin.option.AbstractContext; +import org.codelutin.option.ConfigKey; +import org.codelutin.option.ConfigPropertyKey; +import org.codelutin.option.OptionKey; import org.codelutin.option.OptionParser; -import org.codelutin.option.ParserUtil; +import static org.codelutin.option.ParserUtil.addTitle; +import org.codelutin.option.def.OptionDefinition; +import org.codelutin.option.generate.util.AbstractGeneratorGoal; +import org.codelutin.util.ReflectUtil; import org.codelutin.util.Resource; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; +import java.io.IOException; import java.io.Writer; -import java.net.MalformedURLException; +import java.util.List; /** - * Permet de générer la factory de définitions d'options de ligne de commande, - * pour un fichier de propriété contenant la definition formelle des options. + * Permet de générer les fichiers d'usages de l'application à partir de la classe de context.. * * @author chemit * @goal genRst * @phase pre-site */ -public class RstGeneratorGoal extends AbstractGeneratorGoal { +public class RstGeneratorGoal extends AbstractGeneratorGoal { - /** - * @description nom du parseur a utiliser - * @parameter expression="${commandline.parserFQN}" - * @required - */ - protected String parserFQN; - /** - * @description le chemin où trouver les fichiers de traductions pour l'initialisation I18N, sinon le fichier du plugin est utilisé, alors qu'il faut que ce soit celui de l'application - * @parameter expression="${commandline.i18nCP}" default-value="${basedir}/target/gen/i18n" - */ - protected String i18nCP; - /** - * @description Target rst file. - * @parameter expression="${commandline.rstFilePath}" - * @required - */ - protected File rstFilePath; - - /** - * @description Répertoire de sortie des classes. - * @parameter expression="${commandline.out}" default-value="${basedir}/target/classes" - */ - protected File out; protected Class parserClass; + protected Class contextClass; + + public RstGeneratorGoal() { + super(RstGeneratorContext.class); + } + @SuppressWarnings({"unchecked"}) - protected void prepare() throws ClassNotFoundException, MalformedURLException { - getLog().info("projectCP:" + out); + protected void init() throws Exception { + + getLog().info("projectCP:" + getContext().getOut()); // add i18n location as classpath - Resource.addDefaultClassLoader(new File(i18nCP).toURI().toURL()); - Resource.addDefaultClassLoader(out.toURI().toURL()); + Resource.addDefaultClassLoader(new File(getContext().getI18nCP()).toURI().toURL()); + Resource.addDefaultClassLoader(getContext().getOut().toURI().toURL()); // init i18n I18n.initISO88591(System.getProperty("user.language"), System.getProperty("user.country")); - checkInstanceOf(parserFQN, OptionParser.class); + checkInstanceOf(getContext().getParserFQN(), OptionParser.class); + + checkInstanceOf(getContext().getContextFQN(), AbstractContext.class); - // parse source - parserClass = (Class) Class.forName(parserFQN); - if (!rstFilePath.getParentFile().exists()) { - rstFilePath.getParentFile().mkdirs(); + parserClass = (Class) Class.forName(getContext().getParserFQN()); + + contextClass = (Class) Class.forName(getContext().getParserFQN()); + + if (!getContext().getRstFilePath().getParentFile().exists()) { + getContext().getRstFilePath().getParentFile().mkdirs(); } } - public void doAction() throws Exception { + public void generate() throws Exception { Writer w = null; try { + File rstFilePath = getContext().getRstFilePath(); + w = new BufferedWriter(new FileWriter(rstFilePath)); - ParserUtil.toString(parserClass, w, + String prefix = getContext().getPrefix(); + + List optionKeys = ReflectUtil.getConstants(parserClass, OptionKey.class); + List configKeys = ReflectUtil.getConstants(contextClass, ConfigKey.class); + + toString(w, _("commandline.generateRstFile.head", prefix), _("commandline.generateRstFile.prefix") + ' ', _("commandline.generateRstFile.options.head", prefix), - _("commandline.generateRstFile.configs.head", prefix) + _("commandline.generateRstFile.configs.head", prefix), + optionKeys, configKeys ); getLog().info(_("commandline.generateRstFile.info", rstFilePath)); } finally { if (w != null) { + w.flush(); w.close(); } } } + + public void toString(Writer w, String head, String prefix, String prefixOption, String prefixConfig, List optionKeys, List configKeys) throws IOException { + + w.append(addTitle(head, '=', true)).append("\n\n"); + w.append(addTitle(prefixOption, '-', false)).append("\n"); + + for (OptionKey key : optionKeys) { + OptionDefinition definition = key.getDefinition(); + w.append(prefix).append(definition.toString()); + w.append("\n ").append(key.getDescription()).append("\n\n"); + } + if (configKeys.isEmpty()) { + return; + } + w.append(addTitle(prefixConfig, '-', false)).append("\n"); + for (ConfigKey key : configKeys) { + w.append("\n").append(addTitle(key.getDescription() + " (" + key.getCategory() + ")", '~', false)).append("\n"); + for (ConfigPropertyKey propertyKey : ReflectUtil.getConstants(key.getAbstractConfigClass(), ConfigPropertyKey.class)) { + w.append(prefix).append(propertyKey.getKey()).append(" (").append(propertyKey.getType().getSimpleName()).append(")"); + if (propertyKey.getDefaultValue() != null) { + w.append(" "); + + } + w.append("\n ").append(propertyKey.getDescription()).append("\n\n"); + } + } + } + } \ No newline at end of file Index: maven-commandline-plugin/src/java/org/codelutin/option/generate/JavaGeneratorGoal.java diff -u maven-commandline-plugin/src/java/org/codelutin/option/generate/JavaGeneratorGoal.java:1.19 maven-commandline-plugin/src/java/org/codelutin/option/generate/JavaGeneratorGoal.java:1.20 --- maven-commandline-plugin/src/java/org/codelutin/option/generate/JavaGeneratorGoal.java:1.19 Wed Mar 19 17:23:20 2008 +++ maven-commandline-plugin/src/java/org/codelutin/option/generate/JavaGeneratorGoal.java Wed Mar 19 20:21:42 2008 @@ -37,6 +37,7 @@ import org.codelutin.option.generate.java.OptionJavaGenerator; import org.codelutin.option.generate.java.OptionKeyJavaGenerator; import org.codelutin.option.generate.java.OptionParserJavaGenerator; +import org.codelutin.option.generate.util.AbstractGeneratorGoal; import java.io.File; import java.io.StringWriter; @@ -52,7 +53,7 @@ * @goal genJava * @phase generate-sources */ -public class JavaGeneratorGoal extends AbstractGeneratorGoal { +public class JavaGeneratorGoal extends AbstractGeneratorGoal { /** * @description Chemin du fichier de propriétés contenant les définitions d'options à  utiliser pour générer la factory de définitions d'options. @@ -62,12 +63,6 @@ protected File source; /** - * @description Répertoire de sortie des sources java générés. - * @parameter expression="${commandline.out}" default-value="${basedir}/target/gen/java" - */ - protected File out; - - /** * @description nom du paquetage utilisé pour la génération du parseur * @parameter expression="${commandline.parserPackageName}" * @required @@ -176,17 +171,17 @@ */ protected boolean concreteConfig; - protected DefinitionParser parser; - - //protected boolean needActionConstructor; - public JavaGeneratorGoal() { + super(JavaGeneratorContext.class); } + protected DefinitionParser parser; - protected void prepare() throws Exception { + //protected boolean needActionConstructor; + protected void init() throws Exception { + super.init(); //I18n.initISO88591(System.getProperty("user.language"), System.getProperty("user.country")); checkInstanceOf(parserSuperClass, OptionParser.class); @@ -208,24 +203,19 @@ contextPackageName = parserPackageName; } if (parserSimpleName == null) { - parserSimpleName = prefix + "OptionParser"; + parserSimpleName = getContext().getPrefix() + "OptionParser"; } if (optionSimpleName == null) { - optionSimpleName = prefix + "Option"; + optionSimpleName = getContext().getPrefix() + "Option"; } if (actionSimpleName == null) { - actionSimpleName = prefix + "AbstractOptionAction"; + actionSimpleName = getContext().getPrefix() + "AbstractOptionAction"; } if (configSimpleName == null) { - configSimpleName = prefix + "Config"; + configSimpleName = getContext().getPrefix() + "Config"; } if (contextSimpleName == null) { - contextSimpleName = prefix + "AbstractContext"; - } - - // make sure out exists - if (!out.exists()) { - out.mkdirs(); + contextSimpleName = getContext().getPrefix() + "AbstractContext"; } // get type of source for parser @@ -269,7 +259,7 @@ } } - public void doAction() throws Exception { + public void generate() throws Exception { String optionKeySimpleName = parserSimpleName; OptionContext[] optionContexts = parser.getOptions(); @@ -291,7 +281,7 @@ // generate specialized abstract OptionAction for this parser if (needSpecializedAction()) { - AbstractOptionActionJavaGenerator.doGenerate(out, t0, i18nPrefix, + AbstractOptionActionJavaGenerator.doGenerate(getContext().getOut(), timestamp, getContext().getI18nPrefix(), parserPackageName, contextPackageName, parserSimpleName, @@ -302,11 +292,11 @@ actionSuperClass = actionSimpleName; } - configSimpleName = prefix + (concreteConfig ? "" : "Abstract") + "Config"; - + configSimpleName = getContext().getPrefix() + (concreteConfig ? "" : "Abstract") + "Config"; + // generate specialized abstract SimpleConfigImpl for this parser if (needSpecializedAbstractConfig()) { - AbstractConfigJavaGenerator.doGenerate(out, t0, i18nPrefix, + AbstractConfigJavaGenerator.doGenerate(getContext().getOut(), timestamp, getContext().getI18nPrefix(), configContexts, parserPackageName, contextPackageName, @@ -315,16 +305,16 @@ parserSimpleName, contextSimpleName, optionKeySimpleName + "", - prefix + "AbstractConfig", + getContext().getPrefix() + "AbstractConfig", configSimpleName, configSuperClass ); // the super classes of generatred configs is the one generated here - configSuperClass = prefix + "AbstractConfig"; + configSuperClass = getContext().getPrefix() + "AbstractConfig"; } // generate specialized OptionKey for this parser - OptionKeyJavaGenerator.doGenerate(out, t0, i18nPrefix, + OptionKeyJavaGenerator.doGenerate(getContext().getOut(), timestamp, getContext().getI18nPrefix(), parserPackageName, contextPackageName, optionPackageName, @@ -335,7 +325,7 @@ ); // generate OptionParser implementation - OptionParserJavaGenerator.doGenerate(out, t0, i18nPrefix, + OptionParserJavaGenerator.doGenerate(getContext().getOut(), timestamp, getContext().getI18nPrefix(), optionContexts, parserPackageName, parserSimpleName, @@ -348,7 +338,7 @@ ); - AbstractContextJavaGenerator.doGenerate(out, t0, i18nPrefix, + AbstractContextJavaGenerator.doGenerate(getContext().getOut(), timestamp, getContext().getI18nPrefix(), configContexts, parserPackageName, configPackageName, @@ -361,11 +351,11 @@ // generate Config implementations for (ConfigContext context : configContexts) { String suffix = StringUtils.capitalize(context.getCategory()); - ConfigJavaGenerator.doGenerate(out, t0, i18nPrefix, + ConfigJavaGenerator.doGenerate(getContext().getOut(), timestamp, getContext().getI18nPrefix(), context, parserPackageName, configPackageName, - configSimpleName+ suffix, + configSimpleName + suffix, parserSimpleName, configSuperClass, concreteConfig @@ -374,7 +364,7 @@ // generate Option implementations for (OptionContext context : optionContexts) { - OptionJavaGenerator.doGenerate(out, t0, i18nPrefix, context, + OptionJavaGenerator.doGenerate(getContext().getOut(), timestamp, getContext().getI18nPrefix(), context, optionPackageName, map.get(context.getKey()), optionSuperClass @@ -385,7 +375,7 @@ for (OptionContext context : optionContexts) { String suffix = StringUtils.capitalize(context.getKey()); String optionClassName = map.get(context.getKey()); - OptionActionJavaGenerator.doGenerate(out, t0, i18nPrefix, context, + OptionActionJavaGenerator.doGenerate(getContext().getOut(), timestamp, getContext().getI18nPrefix(), context, actionPackageName, actionSimpleName + suffix, actionSuperClass + "<" + optionClassName + ">", Index: maven-commandline-plugin/src/java/org/codelutin/option/generate/JavaGeneratorContext.java diff -u /dev/null maven-commandline-plugin/src/java/org/codelutin/option/generate/JavaGeneratorContext.java:1.1 --- /dev/null Wed Mar 19 20:21:47 2008 +++ maven-commandline-plugin/src/java/org/codelutin/option/generate/JavaGeneratorContext.java Wed Mar 19 20:21:42 2008 @@ -0,0 +1,22 @@ +/** + * # #% Copyright (C) 2008 Code Lutin, 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.option.generate; + +import org.codelutin.option.generate.util.AbstractGeneratorContext; + +/** @author chemit */ +public class JavaGeneratorContext extends AbstractGeneratorContext { + +} Index: maven-commandline-plugin/src/java/org/codelutin/option/generate/RstGeneratorContext.java diff -u /dev/null maven-commandline-plugin/src/java/org/codelutin/option/generate/RstGeneratorContext.java:1.1 --- /dev/null Wed Mar 19 20:21:47 2008 +++ maven-commandline-plugin/src/java/org/codelutin/option/generate/RstGeneratorContext.java Wed Mar 19 20:21:42 2008 @@ -0,0 +1,70 @@ +/** + * # #% Copyright (C) 2008 Code Lutin, 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.option.generate; + +import org.codelutin.option.generate.util.AbstractGeneratorContext; + +import java.io.File; + +/** @author chemit */ +public class RstGeneratorContext extends AbstractGeneratorContext { + + /** + * @description le chemin où trouver les fichiers de traductions pour l'initialisation I18N, sinon le fichier du plugin est utilisé, alors qu'il faut que ce soit celui de l'application + * @parameter expression="${commandline.i18nCP}" default-value="${basedir}/target/gen/i18n" + */ + protected String i18nCP; + /** + * @description Target rst file. + * @parameter expression="${commandline.rstFilePath}" + * @required + */ + protected File rstFilePath; + + protected String parserFQN; + protected String contextFQN; + + public String getI18nCP() { + return i18nCP; + } + + public void setI18nCP(String i18nCP) { + this.i18nCP = i18nCP; + } + + public String getParserFQN() { + return parserFQN; + } + + public String getContextFQN() { + return contextFQN; + } + + public File getRstFilePath() { + return rstFilePath; + } + + public void setRstFilePath(File rstFilePath) { + this.rstFilePath = rstFilePath; + } + + public void init() { + super.init(); + parserFQN = getPackageName()+'.'+getPrefix()+"OptionParser"; + + contextFQN = getPackageName()+'.'+getPrefix()+"AbstractContext"; + + } +} \ No newline at end of file