Adds CLI tool to run TeaVM

This commit is contained in:
konsoletyper 2014-06-22 22:31:09 +04:00
parent c459f3779a
commit 0e8b3d23bb
7 changed files with 271 additions and 1 deletions

View File

@ -77,6 +77,7 @@
<module>teavm-jso</module>
<module>teavm-html4j</module>
<module>teavm-samples</module>
<module>teavm-cli</module>
</modules>
<dependencyManagement>

4
teavm-cli/.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
/target
/.settings
/.classpath
/.project

55
teavm-cli/pom.xml Normal file
View File

@ -0,0 +1,55 @@
<!--
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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.teavm</groupId>
<artifactId>teavm</artifactId>
<version>0.2-SNAPSHOT</version>
</parent>
<artifactId>teavm-cli</artifactId>
<name>TeaVM CLI</name>
<description>TeaVM command line tools</description>
<dependencies>
<dependency>
<groupId>org.teavm</groupId>
<artifactId>teavm-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@ -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 <konsoletyper@gmail.com>
*/
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);
}
}

View File

@ -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 <konsoletyper@gmail.com>
*/
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);
}
}

View File

@ -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");