From 209de0bca945ae8294076b4df42775784d280305 Mon Sep 17 00:00:00 2001 From: Eyal Kaspi Date: Thu, 14 Jun 2012 17:48:36 -0700 Subject: [PATCH] command line (maven less) interface --- .gitignore | 6 ++ commandLine/pom.xml | 43 +++++++++++ .../org/stjs/command/line/CommandLine.java | 73 +++++++++++++++++++ .../stjs/command/line/ProjectCommandLine.java | 50 +++++++++++++ 4 files changed, 172 insertions(+) create mode 100644 .gitignore create mode 100644 commandLine/pom.xml create mode 100644 commandLine/src/main/java/org/stjs/command/line/CommandLine.java create mode 100644 commandLine/src/main/java/org/stjs/command/line/ProjectCommandLine.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..edb6464c --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +*.class +*.classpath +*.settings* +*.project +*.properties +*/target/* diff --git a/commandLine/pom.xml b/commandLine/pom.xml new file mode 100644 index 00000000..969800b3 --- /dev/null +++ b/commandLine/pom.xml @@ -0,0 +1,43 @@ + + + 4.0.0 + + commandLine + jar + Strongly-typed Javascript:Generator + + + org.st-js + stjs + 1.2.2-SNAPSHOT + + + + + + + + + org.st-js + generator + ${project.version} + + + + org.st-js + js-lib + ${project.version} + provided + + + + org.st-js + jquery + ${project.version} + provided + + + + + \ No newline at end of file diff --git a/commandLine/src/main/java/org/stjs/command/line/CommandLine.java b/commandLine/src/main/java/org/stjs/command/line/CommandLine.java new file mode 100644 index 00000000..5ad7c654 --- /dev/null +++ b/commandLine/src/main/java/org/stjs/command/line/CommandLine.java @@ -0,0 +1,73 @@ +package org.stjs.command.line; + +import java.io.File; +import java.io.IOException; +import java.net.URISyntaxException; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.Collections; +import java.util.List; + +import javax.tools.JavaCompiler; +import javax.tools.JavaFileObject; +import javax.tools.StandardJavaFileManager; +import javax.tools.ToolProvider; + +import org.stjs.generator.GenerationDirectory; +import org.stjs.generator.Generator; +import org.stjs.generator.GeneratorConfiguration; +import org.stjs.generator.GeneratorConfigurationBuilder; + +import com.google.common.base.Throwables; + +public class CommandLine { + + public static void main(String[] args) throws URISyntaxException, IOException, ClassNotFoundException { + if (args.length != 2) { + System.err.println("Usate: CommandLine "); + return; + } + String path = args[0]; + String fileName = args[1]; + File sourceFile = new File(path+"/"+fileName.replaceAll("\\.", "/")+".java"); + compile(path, Collections.singletonList(sourceFile)); + generate(path, fileName, path); + } + + static void generate(final String path, final String className, String outputDir) { + try { + ClassLoader builtProjectClassLoader = new URLClassLoader(new URL[]{new File(path).toURI().toURL()}, Thread.currentThread().getContextClassLoader()); + File sourceFolder = new File(path); + GenerationDirectory targetFolder = new GenerationDirectory(new File(outputDir), null, null); + File generationFolder = targetFolder.getAbsolutePath(); + + GeneratorConfigurationBuilder configBuilder = new GeneratorConfigurationBuilder(); + configBuilder.allowedPackage(builtProjectClassLoader.loadClass(className).getPackage().getName()); + GeneratorConfiguration configuration = configBuilder.build(); + new Generator().generateJavascript(builtProjectClassLoader, + className, sourceFolder, targetFolder, + generationFolder, configuration); + } catch (Exception e) { + throw Throwables.propagate(e); + } + } + + static void compile(final String path, final List sourceFiles) { + try { + JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); + StandardJavaFileManager fileManager = compiler.getStandardFileManager( + null, null, null); + + Iterable compilationUnits1 = fileManager + .getJavaFileObjectsFromFiles(sourceFiles); + compiler.getTask(null, fileManager, null, null, null, compilationUnits1) + .call(); + + fileManager.close(); + } catch (Exception e) { + throw Throwables.propagate(e); + } + + } + +} diff --git a/commandLine/src/main/java/org/stjs/command/line/ProjectCommandLine.java b/commandLine/src/main/java/org/stjs/command/line/ProjectCommandLine.java new file mode 100644 index 00000000..64e64f53 --- /dev/null +++ b/commandLine/src/main/java/org/stjs/command/line/ProjectCommandLine.java @@ -0,0 +1,50 @@ +package org.stjs.command.line; + +import static com.google.common.collect.Lists.newArrayList; + +import java.io.File; +import java.util.List; + +public class ProjectCommandLine { + + public static void main(String[] args) { + if (args.length != 2) { + System.err.println("Usate: CommandLine "); + return; + } + String path = args[0]; + if (!path.endsWith("/")) { + path += "/"; + } + String outputDir = args[1]; + List classNames = listJavaFiles(new File(path)); + CommandLine.compile(path, classNames); + generate(path, classNames, outputDir); + + } + + private static void generate(String path, List files, String outputDir) { + for (File file : files) { + CommandLine.generate(path, file.getAbsolutePath().replace(path, "").replace(".java", "").replaceAll("/", "."), outputDir); + } + } + + + private static List listJavaFiles(File srcDir) { + List files = newArrayList(); + listJavaFiles0(srcDir, files); + return files; + } + + private static void listJavaFiles0(File srcDir, List output) { + for (File file : srcDir.listFiles()) { + if (file.isDirectory()) { + listJavaFiles0(file, output); + } else { + if (file.getName().endsWith(".java")) { + output.add(file); + } + } + } + } +}