From e4f6164f4e2d43cf16eb9104b378c4d40c4f8bc7 Mon Sep 17 00:00:00 2001 From: Jamie Doornbos Date: Tue, 10 Jun 2014 13:53:57 -0700 Subject: [PATCH] Option for ignoring unchanged flump libs. Also, as I was driving by, made it a maven plugin. I think this is the right thing to do and that no one else is using binary flump libs. But, if it does bother anyone, we could have a library with the command line interface and the maven plugin could depend on that. NOTE for IDE users: the artifact id changed, which sometimes means you need to reimport. --- tools/pom.xml | 45 +++++++- .../tools/ConvertFlumpLibsMojo.java | 106 ++++++++++++++++++ .../tools/ConvertFlumpLibsToBinary.java | 87 -------------- 3 files changed, 149 insertions(+), 89 deletions(-) create mode 100644 tools/src/main/java/tripleplay/tools/ConvertFlumpLibsMojo.java delete mode 100644 tools/src/main/java/tripleplay/tools/ConvertFlumpLibsToBinary.java diff --git a/tools/pom.xml b/tools/pom.xml index 25937124..2439853c 100644 --- a/tools/pom.xml +++ b/tools/pom.xml @@ -7,8 +7,8 @@ 1.9-SNAPSHOT - tripleplay-tools - jar + tripleplay-maven-plugin + maven-plugin Triple Play Tools Build-time tools for use with Triple Play. @@ -20,5 +20,46 @@ ${project.version} compile + + + + org.apache.maven + maven-plugin-api + 2.0 + + + org.apache.maven + maven-core + 2.0 + + + org.apache.maven.plugin-tools + maven-plugin-annotations + 3.1 + + + + + + org.apache.maven.plugins + maven-plugin-plugin + 3.1 + + true + + java-annotations + + + + + mojo-descriptor + + descriptor + + + + + + diff --git a/tools/src/main/java/tripleplay/tools/ConvertFlumpLibsMojo.java b/tools/src/main/java/tripleplay/tools/ConvertFlumpLibsMojo.java new file mode 100644 index 00000000..13667d0b --- /dev/null +++ b/tools/src/main/java/tripleplay/tools/ConvertFlumpLibsMojo.java @@ -0,0 +1,106 @@ +// +// Triple Play - utilities for use in PlayN-based games +// Copyright (c) 2011-2014, Three Rings Design, Inc. - All rights reserved. +// http://github.com/threerings/tripleplay/blob/master/LICENSE + +package tripleplay.tools; + +import java.io.DataOutputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.Scanner; + +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugins.annotations.LifecyclePhase; +import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; +import org.apache.maven.project.MavenProject; + +import playn.core.json.JsonImpl; + +import tripleplay.flump.LibraryData; + +@Mojo(name="flump", defaultPhase=LifecyclePhase.PROCESS_RESOURCES) +public class ConvertFlumpLibsMojo extends AbstractMojo +{ + @Parameter(required=true, defaultValue="${project}") + public MavenProject project; + + /** The root location for resources. */ + @Parameter(required=true, defaultValue="src/main/resources") + public File resourceRoot; + + /** The root location to save the converted binary files. By default, uses the build output. */ + @Parameter + public File outputRoot; + + /** The path within {@link #sourceRoot} to look for json files. */ + @Parameter(defaultValue="assets/flump", property="flump.path") + public String path; + + /** Specifies whether the conversion should skip source files with older modification times + * than their binary counterparts. */ + @Parameter(defaultValue="true", property="flump.useModificationTimes") + public boolean useModificationTimes; + + @Override public void execute () + throws MojoExecutionException, MojoFailureException { + if (outputRoot == null) outputRoot = new File(project.getBuild().getOutputDirectory()); + try { + int count = convert(path); + getLog().info("Converted " + count + " out of date libraries"); + } catch (IOException ex) { + throw new MojoExecutionException("", ex); + } + } + + protected int convert (String root) + throws IOException { + int count = 0; + File rootDir = new File(resourceRoot, root); + for (String item : rootDir.list()) { + String itemPath = root + File.separatorChar + item; + File child = new File(rootDir, item); + if (child.isDirectory()) { + count += convert(itemPath); + continue; + } + if (item.equals("library.json")) { + count += convert(child, itemPath); + } + } + return count; + } + + protected LibraryData readLib (File jsonFile) + throws IOException { + Scanner scanner = new Scanner(jsonFile); + try { + return new LibraryData(new JsonImpl().parse(scanner.useDelimiter("\\Z").next())); + } finally { + scanner.close(); + } + } + + protected int convert (File jsonFile, String jsonPath) + throws IOException { + File bin = new File(outputRoot, jsonPath.replace(".json", ".bin")); + if (useModificationTimes && bin.exists() && bin.lastModified() > jsonFile.lastModified()) { + getLog().debug("Skipping up to date file " + bin); + return 0; + } + + getLog().debug("Converting " + bin); + bin.getParentFile().mkdirs(); + DataOutputStream ostream = new DataOutputStream(new FileOutputStream(bin)); + try { + readLib(jsonFile).write(ostream); + } finally { + ostream.close(); + } + return 1; + } +} diff --git a/tools/src/main/java/tripleplay/tools/ConvertFlumpLibsToBinary.java b/tools/src/main/java/tripleplay/tools/ConvertFlumpLibsToBinary.java deleted file mode 100644 index ab238908..00000000 --- a/tools/src/main/java/tripleplay/tools/ConvertFlumpLibsToBinary.java +++ /dev/null @@ -1,87 +0,0 @@ -// -// Triple Play - utilities for use in PlayN-based games -// Copyright (c) 2011-2014, Three Rings Design, Inc. - All rights reserved. -// http://github.com/threerings/tripleplay/blob/master/LICENSE - -package tripleplay.tools; - -import java.io.DataOutputStream; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import java.util.Scanner; - -import playn.core.Json; -import playn.core.json.JsonImpl; - -import tripleplay.flump.LibraryData; - -/** - * A tool intended to convert from the standard Json flump library format to a faster-to-parse and - * smaller binary format. - */ -public class ConvertFlumpLibsToBinary -{ - /** - * Converts the Json flump libraries from the sourceDir into a binary version in the outputDir. - */ - public static void convert (String sourceDir, String outputDir) - throws IOException - { - List jsonFiles = getJsonFiles(new File(sourceDir, "/assets/flump")); - - for (File jsonFile : jsonFiles) { - String jsonTxt = new Scanner(jsonFile).useDelimiter("\\Z").next(); - Json.Object json = new JsonImpl().parse(jsonTxt); - - LibraryData lib = new LibraryData(json); - - File binFile = new File(jsonFile.getAbsolutePath().replace(sourceDir, outputDir). - replace(".json", ".bin")); - - // Ensure all our parent directories are there. - binFile.getParentFile().mkdirs(); - - DataOutputStream ostream = new DataOutputStream(new FileOutputStream(binFile)); - try { - lib.write(ostream); - } finally { - ostream.close(); - } - } - } - - public static void main (String[] args) - throws IOException - { - if (args.length < 2) { - throw new IllegalArgumentException( - "Usage: java ConvertFlumpLibsToBinary "); - } - - convert(args[0], args[1]); - } - - protected static List getJsonFiles (File root) - { - if (root.isFile()) { - List result = new ArrayList(); - result.add(root); - return result; - } else { - List result = new ArrayList(); - for (final File file : root.listFiles()) { - if (file.isDirectory()) { - // Descend... - result.addAll(getJsonFiles(file)); - } else if (file.getName().equals("library.json")) { - // This is one we're looking for... - result.add(file); - } - } - return result; - } - } -}