Author: tchemit Date: 2008-10-07 18:59:47 +0000 (Tue, 07 Oct 2008) New Revision: 888 Modified: lutinjaxx/trunk/jaxx-core/src/main/java/jaxx/compiler/CompilerOptions.java lutinjaxx/trunk/maven-jaxx-plugin/src/main/java/org/codelutin/jaxx/JaxxGeneratorMojo.java lutinjaxx/trunk/maven-jaxx-plugin/src/test/java/org/codelutin/jaxx/AbstractCompilerTest.java lutinjaxx/trunk/maven-jaxx-plugin/src/test/java/org/codelutin/jaxx/CompileConfig.java lutinjaxx/trunk/maven-jaxx-plugin/src/test/java/org/codelutin/jaxx/CompilerTest.java lutinjaxx/trunk/maven-jaxx-plugin/src/test/java/org/codelutin/jaxx/I18nTest.java Log: add force and i18nable options to mojo generate Modified: lutinjaxx/trunk/jaxx-core/src/main/java/jaxx/compiler/CompilerOptions.java =================================================================== --- lutinjaxx/trunk/jaxx-core/src/main/java/jaxx/compiler/CompilerOptions.java 2008-10-07 18:59:01 UTC (rev 887) +++ lutinjaxx/trunk/jaxx-core/src/main/java/jaxx/compiler/CompilerOptions.java 2008-10-07 18:59:47 UTC (rev 888) @@ -1,6 +1,7 @@ package jaxx.compiler; import java.io.File; +import java.util.Arrays; /** * Stores options which affect the jaxxc tool's operation. These options are generally specified by the @@ -14,6 +15,8 @@ private boolean keepJavaFiles; private boolean optimize; private boolean verbose; + /** a flag to enable or disable i18n generation */ + private boolean i18nable; /** * Returns the target directory, generally specified with the "-d" option on the command line. @@ -148,4 +151,25 @@ public void setVerbose(boolean verbose) { this.verbose = verbose; } + + public boolean isI18nable() { + return i18nable; + } + + public void setI18nable(boolean i18nable) { + this.i18nable = i18nable; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(super.toString()); + sb.append("\nclassPath: ").append(getClassPath()); + sb.append("\njavaOut : ").append(getTargetDirectory()); + sb.append("\noutClass : ").append(getJavacTargetDirectory()); + sb.append("\njavacOpts: ").append(getJavacOpts()); + sb.append("\nverbose: ").append(isVerbose()); + sb.append("\noptiomize: ").append(getOptimize()); + sb.append("\ni18nable: ").append(isI18nable()); + return sb.toString(); + } } Modified: lutinjaxx/trunk/maven-jaxx-plugin/src/main/java/org/codelutin/jaxx/JaxxGeneratorMojo.java =================================================================== --- lutinjaxx/trunk/maven-jaxx-plugin/src/main/java/org/codelutin/jaxx/JaxxGeneratorMojo.java 2008-10-07 18:59:01 UTC (rev 887) +++ lutinjaxx/trunk/maven-jaxx-plugin/src/main/java/org/codelutin/jaxx/JaxxGeneratorMojo.java 2008-10-07 18:59:47 UTC (rev 888) @@ -29,14 +29,18 @@ import org.apache.maven.plugin.logging.Log; import org.apache.maven.project.MavenProject; import org.codehaus.plexus.util.DirectoryScanner; +import org.codelutin.util.FileUpdaterHelper; +import org.codelutin.util.MirroredFileUpdater; import java.io.File; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; import java.util.Set; -import java.util.Arrays; /** * Classe permettant de transformer des sources jaxx vers du source java. @@ -111,12 +115,32 @@ */ protected String[] includes; + /** + * to force generation of java source for any jaxx files with no timestamp checking. + * <p/> + * By default, never force generation. + * + * @parameter expression="${jaxx.force}" default-value="false" + */ + protected boolean force; + + /** + * to make compiler i18nable, says add the {@link org.codelutin.i18n.I18n#_(String, Object[])} method + * invocation on {@link jaxx.compiler.I18nHelper#I18N_ATTRIBUTES} attributes. + * + * @parameter expression="${jaxx.i18nable}" default-value="true" + * @see jaxx.compiler.I18nHelper + */ + protected boolean i18nable; + protected String[] files; private static final String[] INCLUDES = {"**\\/*.jaxx"}; protected CompilerOptions options; + protected MirroredFileUpdater updater ; + protected void init() { if (!outResource.exists()) { @@ -135,8 +159,36 @@ boolean noIncludes = includes == null || includes.length == 0; ds.setIncludes(noIncludes ? INCLUDES : includes); ds.scan(); - files = ds.getIncludedFiles(); - getLog().info("jaxx - found " + files.length + " file(s) to generate. "); + String[] files = ds.getIncludedFiles(); + if (verbose) { + getLog().info("jaxx - discover " + files.length + " jaxx file(s). "); + } + + updater = (MirroredFileUpdater) FileUpdaterHelper.newJaxxFileUpdater(src, outJava); + + if (force) { + // we will regenerate all files + this.files = files; + } else { + // filter files + List<String> listFiles = new ArrayList<String>(); + + for (String file : files) { + if (updater.isFileUpToDate(new File(src, file))) { + if (verbose) { + getLog().info("jaxx - skip file [" + file + "]."); + } + } else { + if (verbose) { + getLog().info("jaxx - will generate file [" + file + "]."); + } + listFiles.add(file); + } + } + this.files = listFiles.toArray(new String[listFiles.size()]); + } + + options = toCompilerOptions(); if (verbose) { @@ -166,18 +218,15 @@ result.setJavacTargetDirectory(outClass); result.setTargetDirectory(outJava); result.setVerbose(verbose); + result.setI18nable(i18nable); return result; } protected void printInit() { - getLog().info("classPath: " + options.getClassPath()); - getLog().info("javaOut : " + options.getTargetDirectory()); - getLog().info("outClass : " + options.getJavacTargetDirectory()); - getLog().info("javacOpts: " + options.getJavacOpts()); - getLog().info("optiomize: " + options.getOptimize()); + getLog().info(options.toString()); getLog().info("includes : " + Arrays.toString(includes)); for (String file : files) { - getLog().info(file); + getLog().info("will generate "+file); } ClassLoader cl = Thread.currentThread().getContextClassLoader(); @@ -197,7 +246,7 @@ } //fixme should remove this silly test when we will make real maven plugin tests :) - if (getPluginContext()!=null) { + if (getPluginContext() != null) { for (Object e : getPluginContext().keySet()) { getLog().info("pluginContext " + e + " : " + getPluginContext().get(e)); } @@ -208,6 +257,13 @@ init(); + if (files.length == 0) { + getLog().info("jaxx - no jaxx file to treate. "); + return; + } + + getLog().info("jaxx - will generate " + this.files.length + " jaxx file(s). "); + try { doExecute(); @@ -227,7 +283,7 @@ protected void fixCompileSourceRoots() { //fixme should remove this silly test when we will make real maven plugin tests :) - if (project!=null) { + if (project != null) { if (!project.getCompileSourceRoots().contains(outJava.getPath())) { project.addCompileSourceRoot(outJava.getPath()); } @@ -346,4 +402,23 @@ this.files = files; } + public boolean isForce() { + return force; + } + + public void setForce(boolean force) { + this.force = force; + } + + public boolean isI18nable() { + return i18nable; + } + + public void setI18nable(boolean i18nable) { + this.i18nable = i18nable; + } + + public MirroredFileUpdater getUpdater() { + return updater; + } } \ No newline at end of file Modified: lutinjaxx/trunk/maven-jaxx-plugin/src/test/java/org/codelutin/jaxx/AbstractCompilerTest.java =================================================================== --- lutinjaxx/trunk/maven-jaxx-plugin/src/test/java/org/codelutin/jaxx/AbstractCompilerTest.java 2008-10-07 18:59:01 UTC (rev 887) +++ lutinjaxx/trunk/maven-jaxx-plugin/src/test/java/org/codelutin/jaxx/AbstractCompilerTest.java 2008-10-07 18:59:47 UTC (rev 888) @@ -88,6 +88,8 @@ goal.setIncludes(new String[]{"**\\/" + PREFIX_PACKAGE + "\\/" + expression + "\\/*.jaxx"}); goal.setOptimize(anno.optimize()); goal.setVerbose(anno.verbose()); + goal.setForce(anno.force()); + goal.setI18nable(anno.i18nable()); //TODO Use the maven plexus TestCase inorder to include dependencies goal.setJavaOpts(anno.javaOpts()); log.info("setUp test " + getName()); Modified: lutinjaxx/trunk/maven-jaxx-plugin/src/test/java/org/codelutin/jaxx/CompileConfig.java =================================================================== --- lutinjaxx/trunk/maven-jaxx-plugin/src/test/java/org/codelutin/jaxx/CompileConfig.java 2008-10-07 18:59:01 UTC (rev 887) +++ lutinjaxx/trunk/maven-jaxx-plugin/src/test/java/org/codelutin/jaxx/CompileConfig.java 2008-10-07 18:59:47 UTC (rev 888) @@ -38,5 +38,9 @@ public boolean verbose() default false; + public boolean force() default true; + + public boolean i18nable() default false; + public String javaOpts() default ""; } Modified: lutinjaxx/trunk/maven-jaxx-plugin/src/test/java/org/codelutin/jaxx/CompilerTest.java =================================================================== --- lutinjaxx/trunk/maven-jaxx-plugin/src/test/java/org/codelutin/jaxx/CompilerTest.java 2008-10-07 18:59:01 UTC (rev 887) +++ lutinjaxx/trunk/maven-jaxx-plugin/src/test/java/org/codelutin/jaxx/CompilerTest.java 2008-10-07 18:59:47 UTC (rev 888) @@ -3,6 +3,8 @@ import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; +import java.io.File; + public class CompilerTest extends AbstractCompilerTest { @CompileConfig(packageName = "InnerClasses", runJavac = true) @@ -23,7 +25,7 @@ goal.execute(); } - @CompileConfig(packageName = "CSSTests", verbose = false) + @CompileConfig(packageName = "CSSTests", verbose = false, i18nable = false) public void testCssTests() throws MojoExecutionException, MojoFailureException { goal.execute(); } @@ -67,5 +69,43 @@ goal.execute(); } + @CompileConfig(packageName = "force", verbose = false) + public void testForce() throws MojoExecutionException, MojoFailureException, InterruptedException { + // first round, with force option so will generate theonly JButton.jaxx file + goal.execute(); + String[] files = goal.getFiles(); + assertEquals(1, files.length); + File srcFile = new File(goal.getSrc(), files[0]); + + File dstFile = goal.getUpdater().getMirrorFile(srcFile); + + long oldTime = dstFile.lastModified(); + // second round, no force so will not the file + goal.setForce(false); + goal.execute(); + files = goal.getFiles(); + assertEquals(0, files.length); + + Thread.sleep(1000); + + assertEquals(oldTime, goal.getUpdater().getMirrorFile(srcFile).lastModified()); + + // three round : modify a source with no force option + srcFile.setLastModified(System.currentTimeMillis()); + + goal.execute(); + files = goal.getFiles(); + assertEquals(1, files.length); + + assertTrue(goal.getUpdater().getMirrorFile(srcFile).lastModified() > oldTime); + + // last round, reforce file generation for an no modify source + goal.setForce(true); + goal.execute(); + files = goal.getFiles(); + assertEquals(1, files.length); + assertTrue(goal.getUpdater().getMirrorFile(srcFile).lastModified() > oldTime); + } + } \ No newline at end of file Modified: lutinjaxx/trunk/maven-jaxx-plugin/src/test/java/org/codelutin/jaxx/I18nTest.java =================================================================== --- lutinjaxx/trunk/maven-jaxx-plugin/src/test/java/org/codelutin/jaxx/I18nTest.java 2008-10-07 18:59:01 UTC (rev 887) +++ lutinjaxx/trunk/maven-jaxx-plugin/src/test/java/org/codelutin/jaxx/I18nTest.java 2008-10-07 18:59:47 UTC (rev 888) @@ -9,28 +9,43 @@ public class I18nTest extends AbstractCompilerTest { - @CompileConfig(packageName = "i18n.text", verbose = false) + @CompileConfig(packageName = "i18n.text", verbose = false, i18nable = false) + public void testI18nTextNoI1_nabel() throws MojoExecutionException, MojoFailureException, IOException { + goal.execute(); + checkPattern("testId.setText(_(\"test.text\"));", false); + } + + @CompileConfig(packageName = "i18n.title", verbose = false, i18nable = false) + public void testI18nTitleNoI18nable() throws MojoExecutionException, MojoFailureException, IOException { + goal.execute(); + checkPattern("testId.setTitle(_(\"test.title\"));", false); + } + + @CompileConfig(packageName = "i18n.tooltiptext", verbose = false, i18nable = false) + public void testI18nToolTipTextNoI18nable() throws MojoExecutionException, MojoFailureException, IOException { + goal.execute(); + checkPattern("testId.setToolTipText(_(\"test.toolTipText\"));", false); + } + + @CompileConfig(packageName = "i18n.text", verbose = false, i18nable = true) public void testI18nText() throws MojoExecutionException, MojoFailureException, IOException { goal.execute(); - assertTrue(true); - checkPattern("testId.setText(_(\"test.text\"));"); + checkPattern("testId.setText(_(\"test.text\"));", true); } - @CompileConfig(packageName = "i18n.title", verbose = false) + @CompileConfig(packageName = "i18n.title", verbose = false, i18nable = true) public void testI18nTitle() throws MojoExecutionException, MojoFailureException, IOException { goal.execute(); - assertTrue(true); - checkPattern("testId.setTitle(_(\"test.title\"));"); + checkPattern("testId.setTitle(_(\"test.title\"));", true); } - @CompileConfig(packageName = "i18n.tooltiptext", verbose = false) + @CompileConfig(packageName = "i18n.tooltiptext", verbose = false, i18nable = true) public void testI18nToolTipText() throws MojoExecutionException, MojoFailureException, IOException { goal.execute(); - assertTrue(true); - checkPattern("testId.setToolTipText(_(\"test.toolTipText\"));"); + checkPattern("testId.setToolTipText(_(\"test.toolTipText\"));", true); } - protected void checkPattern(String pattern) throws IOException { + protected void checkPattern(String pattern, boolean required) throws IOException { String[] files = goal.getFiles(); for (String file : files) { // check we have a the required testId.setTitle(_("test.title")); @@ -39,7 +54,7 @@ assertTrue("generated file " + f + " was not found...", f.exists()); String content = FileUtil.readAsString(f); - assertTrue("could not find the pattern : " + pattern + " in file " + f, content.contains(pattern)); + assertEquals("could not find the pattern : " + pattern + " in file " + f, required, content.contains(pattern)); } }