r223 - in maven-jrst-plugin/trunk: . src/main/java/org/codelutin/jrst/plugin
Author: tchemit Date: 2008-10-08 21:42:51 +0000 (Wed, 08 Oct 2008) New Revision: 223 Modified: maven-jrst-plugin/trunk/pom.xml maven-jrst-plugin/trunk/src/main/java/org/codelutin/jrst/plugin/JRstPlugin.java Log: introduce updater idiom in the plugin (only generate if required) introduce property force to force generation introduce property resourceDirectoryOut : where to copy resources Modified: maven-jrst-plugin/trunk/pom.xml =================================================================== --- maven-jrst-plugin/trunk/pom.xml 2008-10-08 15:18:59 UTC (rev 222) +++ maven-jrst-plugin/trunk/pom.xml 2008-10-08 21:42:51 UTC (rev 223) @@ -23,13 +23,19 @@ <version>[0.8,)</version> <scope>compile</scope> </dependency> + <dependency> + <groupId>org.codelutin</groupId> + <artifactId>lutinpluginutil</artifactId> + <version>0.2</version> + <scope>compile</scope> + </dependency> </dependencies> <!-- ************************************************************* --> <!-- *** Project Information ************************************* --> <!-- ************************************************************* --> <name>Plugin maven JRst</name> - <version>0.8.2-SNAPHSOT</version> + <version>0.8.2</version> <description> Plugin pour maven2 pour la génération de xdoc à partir de fichiers RST. </description> Modified: maven-jrst-plugin/trunk/src/main/java/org/codelutin/jrst/plugin/JRstPlugin.java =================================================================== --- maven-jrst-plugin/trunk/src/main/java/org/codelutin/jrst/plugin/JRstPlugin.java 2008-10-08 15:18:59 UTC (rev 222) +++ maven-jrst-plugin/trunk/src/main/java/org/codelutin/jrst/plugin/JRstPlugin.java 2008-10-08 21:42:51 UTC (rev 223) @@ -33,8 +33,11 @@ import org.apache.maven.plugin.MojoExecutionException; import org.codehaus.plexus.util.DirectoryScanner; import org.codelutin.jrst.JRST; +import org.codelutin.util.FileUtil; +import org.codelutin.util.MirroredFileUpdater; import java.io.File; +import java.io.IOException; /** * Génére du xdoc à partir de fichiers Rst. @@ -58,7 +61,16 @@ * @phase pre-site */ public class JRstPlugin extends AbstractMojo { - //implements FileAction { + + /** La liste des ressource a copier si rien n'est specifie via la propriete includeResources */ + protected static final String[] DEFAULT_INCLUDE_RESOURCES = { + "**\\/*.png", + "**\\/*.jpeg", + "**\\/*.jpg", + "**\\/*.gif" + }; + + /** * Répertoire source des fichiers Rst * @@ -90,6 +102,14 @@ protected File directoryOut; /** + * Répertoire cible des fichiers resources générée + * + * @parameter default-value="target/generated-site/resources" + * @required + */ + protected File resourceDirectoryOut; + + /** * Arréte la génération en cas d'erreurs * * @parameter default-value="true" @@ -103,7 +123,11 @@ */ protected String overwrite = "ifnewer"; - /** @parameter default-value="fr" */ + /** + * La locale par defaut a utiliser. + * + * @parameter default-value="fr" + */ protected String defaultLocale = "fr"; /** @@ -113,8 +137,22 @@ */ protected boolean verbose; + /** + * Flag pour forcer la generation des rst et la copie des resources. + * + * @parameter expression="${jrst.force}" default-value="false" + */ + protected boolean force; + + /** + * La liste des ressources a recopier + * + * @parameter expression="${jrst.includeResources}" + */ + protected String[] includeResources; + /** Number of generated files. */ - private int numberFilesGenerates; + protected int numberFilesGenerates; /** * Retourne la langue par defaut precisé dans le pom.xml @@ -141,102 +179,141 @@ try { actionGenerate(); } catch (GenerationJRstException e) { - System.out.println("Error during generation for :\n" + getLog().error("Error during generation for :\n" + GenerationJRstException.getFilesErrors()); if (!ignoreErrors) { throw new MojoExecutionException("Erreur lors de l'execution"); } } - //actionCopy(); + actionCopy(); } private void actionGenerate() { getLog().info("Génération des fichier xdocs à partir des fichiers rst"); + + MirroredFileUpdater rstUpdater = new RstUpdater(directoryIn, directoryOut, defaultLocale); + numberFilesGenerates = 0; - DirectoryScanner ds; - ds = new DirectoryScanner(); + DirectoryScanner ds = new DirectoryScanner(); ds.setBasedir(directoryIn); ds.setIncludes(new String[]{"**\\/*.rst"}); - ds.scan(); String[] files = ds.getIncludedFiles(); for (String file : files) { - doAction(new File(directoryIn, file)); + File in = new File(directoryIn, file); + + if (!force && rstUpdater.isFileUpToDate(in)) { + if (verbose) { + getLog().info("skip up-to-date rst file : " + in); + } + continue; + } + + doAction(in, rstUpdater.getMirrorFile(in)); } - //FileUtil.walkAfter(new File(directoryIn), this); - getLog().info( - "Generating " + numberFilesGenerates + " files to " - + directoryOut); + getLog().info("Generating " + numberFilesGenerates + " files to " + directoryOut); } - public boolean doAction(File file) { - String fileIn = file.getAbsoluteFile().toString(); - String fileOut = null; - getLog().info("Using " + fileIn); - if (fileIn.matches(".*[/\\\\]rst[/\\\\].*\\.rst")) { - fileOut = fileIn.replace(directoryIn.getAbsolutePath(), directoryOut.getAbsolutePath()).replace(".rst", - ".xml").replaceFirst("([/\\\\])rst([/\\\\])", "$1xdoc$2"); + public boolean doAction(File in, File out) { - if (defaultLocale != null && !"".equals(defaultLocale)) { - fileOut = fileOut.replaceFirst("([/\\\\])" + defaultLocale - + "([/\\\\])", "$1"); - } + getLog().info("Using " + in); - try { - JRST.generate(JRST.TYPE_XDOC, new File(fileIn), inputEncoding, - new File(fileOut), outputEncoding, getOverwrite()); - numberFilesGenerates++; - } catch (Exception e) { - getLog().error(e); - if (!ignoreErrors) { - throw new GenerationJRstException(fileIn); - } + try { + JRST.generate(JRST.TYPE_XDOC, in, inputEncoding, + out, outputEncoding, getOverwrite()); + numberFilesGenerates++; + } catch (Exception e) { + getLog().error(e); + if (!ignoreErrors) { + throw new GenerationJRstException(in.getAbsolutePath()); } } - if (fileOut != null && verbose) { - getLog().info("Using " + fileIn); - getLog().info("Generating " + fileOut); + if (out != null && verbose) { + //getLog().info("Using " + fileIn); + getLog().info("Generating " + out); } return true; } - /*protected void actionCopy() { + protected void actionCopy() { getLog().info("Copie des fichiers images"); - // Execution de la tâche ant copy - try { + MirroredFileUpdater resourceUpdater = new RessourceUpdater(directoryIn, resourceDirectoryOut, defaultLocale); + + DirectoryScanner ds = new DirectoryScanner(); + ds.setBasedir(directoryIn); + ds.setIncludes(includeResources == null || includeResources.length == 0 ? DEFAULT_INCLUDE_RESOURCES : includeResources); + ds.scan(); + + for (String file : ds.getIncludedFiles()) { + File in = new File(directoryIn, file); + if (!force && resourceUpdater.isFileUpToDate(in)) { + if (verbose) { + getLog().info("skip up-to-date resource file : " + in); + } + // do not copy the resource + continue; + } + File out = resourceUpdater.getMirrorFile(in); + if (verbose) { + getLog().info("copy resource " + in + " to " + out); + } + try { + FileUtil.copy(in, out); + } catch (IOException e) { + getLog().error(e); + } + } + } + + /** + * An updater for rst files + * + * @author chemit + */ + protected static class RstUpdater extends MirroredFileUpdater { + + protected String defaultLocale; + + protected RstUpdater(File sourceDirectory, File destinationDirectory, String defaultLocale) { + super(sourceDirectory, destinationDirectory); + this.defaultLocale = defaultLocale; + } + + public File getMirrorFile(File f) { + String file = f.getAbsolutePath().substring(prefixSourceDirecotory); + file = file.replace(".rst", ".xml").replaceFirst("([/\\\\])rst([/\\\\])", "$1xdoc$2"); if (defaultLocale != null && !"".equals(defaultLocale)) { - // copie de tous les fichiers non rst - FileUtil.copyAndRenameRecursively(directoryIn, - directoryOut, false, "([/\\\\])" - + defaultLocale + "([/\\\\])", "$1", true, - ".*[/\\\\]rst[/\\\\].*"); + file = file.replaceFirst("([/\\\\])" + defaultLocale + "([/\\\\])", "$1"); + } + return new File(destinationDirectory, file); + } + } - // copie des images du repertoire rst dans le build-site - FileUtil.copyAndRenameRecursively( - directoryIn, - directoryOut, - false, // on ne copie que le contenu de directoryIn - "([/\\\\])" + defaultLocale + "([/\\\\])rst([/\\\\])", - "$1resources$2", false, - ".*[/\\\\]rst[/\\\\].*(\\.png|\\.jpeg|\\.jpg|\\.gif)$"); - } else { - // copie de tous les fichiers non rst - FileUtil.copyAndRenameRecursively(directoryIn, - directoryOut, false, "", "", true, - ".*[/\\\\]rst[/\\\\].*"); + /** + * An updater for resources files + * + * @author chemit + */ + protected static class RessourceUpdater extends MirroredFileUpdater { - // copie des images du repertoire rst dans le build-site - FileUtil.copyAndRenameRecursively(directoryIn, - directoryOut, false, "([/\\\\])rst([/\\\\])", - "$1resources$2", false, - ".*[/\\\\]rst[/\\\\].*(\\.png|\\.jpeg|\\.jpg|\\.gif)$"); + protected String defaultLocale; + + protected RessourceUpdater(File sourceDirectory, File destinationDirectory, String defaultLocale) { + super(sourceDirectory, destinationDirectory); + this.defaultLocale = defaultLocale; + } + + public File getMirrorFile(File f) { + String file = f.getAbsolutePath().substring(prefixSourceDirecotory); + file = file.replaceFirst("([/\\\\])rst([/\\\\])", "$1$2"); + if (defaultLocale != null && !"".equals(defaultLocale)) { + file = file.replaceFirst("([/\\\\])" + defaultLocale + "([/\\\\])", "$1"); } - } catch (IOException eee) { - getLog().error(eee); + return new File(destinationDirectory, file); } - }*/ + } }
participants (1)
-
tchemit@users.labs.libre-entreprise.org