diff --git a/pom.xml b/pom.xml index 3c262e655..d4fe61e0b 100644 --- a/pom.xml +++ b/pom.xml @@ -77,6 +77,7 @@ teavm-jso teavm-html4j teavm-samples + teavm-cli diff --git a/teavm-cli/.gitignore b/teavm-cli/.gitignore new file mode 100644 index 000000000..c708c363d --- /dev/null +++ b/teavm-cli/.gitignore @@ -0,0 +1,4 @@ +/target +/.settings +/.classpath +/.project diff --git a/teavm-cli/pom.xml b/teavm-cli/pom.xml new file mode 100644 index 000000000..cca0a2556 --- /dev/null +++ b/teavm-cli/pom.xml @@ -0,0 +1,55 @@ + + + 4.0.0 + + + org.teavm + teavm + 0.2-SNAPSHOT + + teavm-cli + + TeaVM CLI + TeaVM command line tools + + + + org.teavm + teavm-core + ${project.version} + + + commons-cli + commons-cli + 1.2 + + + + + + + org.apache.maven.plugins + maven-source-plugin + + + org.apache.maven.plugins + maven-javadoc-plugin + + + + \ No newline at end of file diff --git a/teavm-cli/src/main/java/org/teavm/cli/ConsoleTeaVMToolLog.java b/teavm-cli/src/main/java/org/teavm/cli/ConsoleTeaVMToolLog.java new file mode 100644 index 000000000..2105333ea --- /dev/null +++ b/teavm-cli/src/main/java/org/teavm/cli/ConsoleTeaVMToolLog.java @@ -0,0 +1,68 @@ +/* + * Copyright 2014 Alexey Andreev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.cli; + +import org.teavm.tooling.TeaVMToolLog; + +/** + * + * @author Alexey Andreev + */ +class ConsoleTeaVMToolLog implements TeaVMToolLog { + @Override + public void info(String text) { + System.out.println("INFO: " + text); + } + + @Override + public void debug(String text) { + System.out.println("DEBUG: " + text); + } + + @Override + public void warning(String text) { + System.out.println("WARNING: " + text); + } + + @Override + public void error(String text) { + System.out.println("ERROR: " + text); + } + + @Override + public void info(String text, Throwable e) { + System.out.println("INFO: " + text); + e.printStackTrace(System.out); + } + + @Override + public void debug(String text, Throwable e) { + System.out.println("DEBUG: " + text); + e.printStackTrace(System.out); + } + + @Override + public void warning(String text, Throwable e) { + System.out.println("WARNING: " + text); + e.printStackTrace(System.out); + } + + @Override + public void error(String text, Throwable e) { + System.out.println("ERROR: " + text); + e.printStackTrace(System.out); + } +} diff --git a/teavm-cli/src/main/java/org/teavm/cli/TeaVMRunner.java b/teavm-cli/src/main/java/org/teavm/cli/TeaVMRunner.java new file mode 100644 index 000000000..cbcfd9c7d --- /dev/null +++ b/teavm-cli/src/main/java/org/teavm/cli/TeaVMRunner.java @@ -0,0 +1,142 @@ +/* + * Copyright 2014 Alexey Andreev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.cli; + +import java.io.File; +import org.apache.commons.cli.*; +import org.teavm.tooling.RuntimeCopyOperation; +import org.teavm.tooling.TeaVMTool; + +/** + * + * @author Alexey Andreev + */ +public class TeaVMRunner { + @SuppressWarnings("static-access") + public static void main(String[] args) { + Options options = new Options(); + options.addOption(OptionBuilder + .withArgName("directory") + .hasArg() + .withDescription("a directory where to put generated files (current directory by default)") + .withLongOpt("targetdir") + .create('d')); + options.addOption(OptionBuilder + .withArgName("file") + .hasArg() + .withDescription("a file where to put decompiled classes (classes.js by default)") + .withLongOpt("targetfile") + .create('f')); + options.addOption(OptionBuilder + .withDescription("causes TeaVM to generate minimized JavaScript file") + .withLongOpt("minify") + .create("m")); + options.addOption(OptionBuilder + .hasArg() + .withDescription("how to attach runtime. Possible values are: separate|merge|none") + .withLongOpt("runtime") + .create("r")); + options.addOption(OptionBuilder + .withDescription("causes TeaVM to include default main page") + .create("mainpage")); + options.addOption(OptionBuilder + .withDescription("causes TeaVM to log bytecode") + .create("logbytecode")); + options.addOption(OptionBuilder + .hasArg() + .withDescription("how many threads should TeaVM run") + .withLongOpt("threads") + .create("t")); + + if (args.length == 0) { + printUsage(options); + return; + } + CommandLineParser parser = new PosixParser(); + CommandLine commandLine; + try { + commandLine = parser.parse(options, args); + } catch (ParseException e) { + printUsage(options); + return; + } + + TeaVMTool tool = new TeaVMTool(); + tool.setBytecodeLogging(commandLine.hasOption("logbytecode")); + if (commandLine.hasOption("d")) { + tool.setTargetDirectory(new File(commandLine.getOptionValue("d"))); + } + if (commandLine.hasOption("f")) { + tool.setTargetFileName(commandLine.getOptionValue("f")); + } + if (commandLine.hasOption("m")) { + tool.setMinifying(true); + } else { + tool.setMinifying(false); + } + if (commandLine.hasOption("r")) { + switch (commandLine.getOptionValue("r")) { + case "separate": + tool.setRuntime(RuntimeCopyOperation.SEPARATE); + break; + case "merge": + tool.setRuntime(RuntimeCopyOperation.MERGED); + break; + case "none": + tool.setRuntime(RuntimeCopyOperation.NONE); + break; + default: + System.err.println("Wrong parameter for -r option specified"); + printUsage(options); + return; + } + } + if (commandLine.hasOption("mainpage")) { + tool.setMainPageIncluded(true); + } + if (commandLine.hasOption("t")) { + try { + tool.setNumThreads(Integer.parseInt(commandLine.getOptionValue("t"))); + } catch (NumberFormatException e) { + System.err.println("Wrong parameter for -t option specified"); + printUsage(options); + return; + } + } + args = commandLine.getArgs(); + if (args.length > 1) { + System.err.println("Unexpected arguments"); + printUsage(options); + return; + } else if (args.length == 1) { + tool.setMainClass(args[0]); + } + tool.setLog(new ConsoleTeaVMToolLog()); + + try { + tool.generate(); + } catch (Exception e) { + e.printStackTrace(System.err); + System.exit(-2); + } + } + + private static void printUsage(Options options) { + HelpFormatter formatter = new HelpFormatter(); + formatter.printHelp("java " + TeaVMRunner.class.getName() + "[OPTIONS] [qualified.main.Class]", options); + System.exit(-1); + } +} diff --git a/teavm-core/src/main/java/org/teavm/tooling/TeaVMTool.java b/teavm-core/src/main/java/org/teavm/tooling/TeaVMTool.java index 61313ce88..75a909243 100644 --- a/teavm-core/src/main/java/org/teavm/tooling/TeaVMTool.java +++ b/teavm-core/src/main/java/org/teavm/tooling/TeaVMTool.java @@ -212,7 +212,7 @@ public class TeaVMTool { if (mainPageIncluded) { String text; try (Reader reader = new InputStreamReader(classLoader.getResourceAsStream( - "org/teavm/maven/main.html"), "UTF-8")) { + "org/teavm/tooling/main.html"), "UTF-8")) { text = IOUtils.toString(reader).replace("${classes.js}", targetFileName); } File mainPageFile = new File(targetDirectory, "main.html"); diff --git a/teavm-maven-plugin/src/main/resources/org/teavm/maven/main.html b/teavm-core/src/main/resources/org/teavm/tooling/main.html similarity index 100% rename from teavm-maven-plugin/src/main/resources/org/teavm/maven/main.html rename to teavm-core/src/main/resources/org/teavm/tooling/main.html