提交 d7573b49 编写于 作者: J James Strachan

allow the JS compiler to take a list of files, verbose flag and a flag to...

allow the JS compiler to take a list of files, verbose flag and a flag to indicate if a main call is required; plus added a test case to ensure we can compile some of the kotlin standard library using the K2JSCompiler directly
上级 782773fc
......@@ -16,12 +16,16 @@
package org.jetbrains.jet.cli.js;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Predicates;
import com.google.common.collect.Iterables;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiFile;
import jet.Function0;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.analyzer.AnalyzeExhaust;
import org.jetbrains.jet.cli.common.CLICompiler;
import org.jetbrains.jet.cli.common.ExitCode;
......@@ -37,6 +41,7 @@ import org.jetbrains.k2js.config.Config;
import org.jetbrains.k2js.config.EcmaVersion;
import org.jetbrains.k2js.config.ZippedLibrarySourcesConfig;
import org.jetbrains.k2js.facade.K2JSTranslator;
import org.jetbrains.k2js.facade.MainCallParameters;
import java.util.List;
......@@ -62,7 +67,7 @@ public class K2JSCompiler extends CLICompiler<K2JSCompilerArguments, K2JSCompile
@Override
protected ExitCode doExecute(K2JSCompilerArguments arguments, PrintingMessageCollector messageCollector, Disposable rootDisposable) {
if (arguments.srcdir == null) {
if (arguments.srcdir == null && arguments.sourceFiles == null) {
messageCollector.report(CompilerMessageSeverity.ERROR, "Specify sources location via -srcdir", NO_LOCATION);
return ExitCode.INTERNAL_ERROR;
}
......@@ -79,24 +84,40 @@ public class K2JSCompiler extends CLICompiler<K2JSCompilerArguments, K2JSCompile
return ExitCode.INTERNAL_ERROR;
}
return translateAndGenerateOutputFile(messageCollector, environmentForJS, config, outputFile);
MainCallParameters mainCallParameters = arguments.createMainCallParameters();
return translateAndGenerateOutputFile(mainCallParameters, messageCollector, environmentForJS, config, outputFile);
}
@NotNull
private static JetCoreEnvironment getEnvironment(K2JSCompilerArguments arguments, Disposable rootDisposable) {
final JetCoreEnvironment environmentForJS = JetCoreEnvironment.getCoreEnvironmentForJS(rootDisposable);
environmentForJS.addSources(arguments.srcdir);
if (arguments.srcdir != null) {
environmentForJS.addSources(arguments.srcdir);
}
if (arguments.sourceFiles != null) {
for (String sourceFile : arguments.sourceFiles) {
environmentForJS.addSources(sourceFile);
}
}
if (arguments.isVerbose()) {
System.out.println("Compiling source files: " + environmentForJS.getSourceFiles());
Iterable<String> fileNames = Iterables.transform(environmentForJS.getSourceFiles(), new Function<JetFile, String>() {
@Override
public String apply(@Nullable JetFile file) {
return file.getName();
}
});
System.out.println("Compiling source files: " + Joiner.on(", ").join(fileNames));
}
return environmentForJS;
}
@NotNull
private static ExitCode translateAndGenerateOutputFile(@NotNull PrintingMessageCollector messageCollector,
private static ExitCode translateAndGenerateOutputFile(@NotNull MainCallParameters mainCall,
@NotNull PrintingMessageCollector messageCollector,
@NotNull JetCoreEnvironment environmentForJS, @NotNull Config config, @NotNull String outputFile) {
try {
K2JSTranslator.translateWithCallToMainAndSaveToFile(environmentForJS.getSourceFiles(), outputFile, config);
K2JSTranslator.translateWithMainCallParametersAndSaveToFile(mainCall, environmentForJS.getSourceFiles(), outputFile, config);
}
catch (Exception e) {
messageCollector.report(CompilerMessageSeverity.ERROR, "Exception while translating:\n" + e.getMessage(),
......
......@@ -18,6 +18,10 @@ package org.jetbrains.jet.cli.js;
import com.sampullara.cli.Argument;
import org.jetbrains.jet.cli.common.CompilerArguments;
import org.jetbrains.k2js.facade.MainCallParameters;
import java.util.ArrayList;
import java.util.List;
/**
* @author Pavel Talanov
......@@ -50,9 +54,14 @@ public class K2JSCompilerArguments extends CompilerArguments {
@Argument(value = "version", description = "Display compiler version")
public boolean version;
@Argument(value = "mainCall", description = "Whether a main function should be invoked; either 'main' or 'mainWithArgs'")
public String mainCall;
@Argument(value = "help", alias = "h", description = "Show help")
public boolean help;
public List<String> sourceFiles;
@Override
public boolean isHelp() {
return help;
......@@ -75,6 +84,22 @@ public class K2JSCompilerArguments extends CompilerArguments {
@Override
public String getSrc() {
if (sourceFiles != null) {
return sourceFiles.toString();
}
return srcdir;
}
public MainCallParameters createMainCallParameters() {
if (mainCall != null) {
if (mainCall.equals("main")) {
return MainCallParameters.mainWithoutArguments();
}
if (mainCall.equals("mainWithArgs")) {
// TODO should we pass the arguments to the compiler?
return MainCallParameters.mainWithArguments(new ArrayList<String>());
}
}
return MainCallParameters.noCall();
}
}
......@@ -19,6 +19,8 @@ package org.jetbrains.k2js.test.semantics;
import com.google.common.collect.Lists;
import com.intellij.openapi.util.io.FileUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.cli.js.K2JSCompiler;
import org.jetbrains.jet.cli.js.K2JSCompilerArguments;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.k2js.config.Config;
import org.jetbrains.k2js.config.EcmaVersion;
......@@ -58,12 +60,6 @@ public final class StdLibToJSTest extends SingleFileTranslationTest {
String... stdLibFiles) throws Exception {
List<String> files = Lists.newArrayList();
// lets add the standard JS library files
/*
for (String libFileName : Config.LIB_FILE_NAMES) {
files.add(Config.LIBRARIES_LOCATION + libFileName);
}
*/
File stdlibDir = new File("libraries/stdlib/src");
assertTrue("Cannot find stdlib source: " + stdlibDir, stdlibDir.exists());
......@@ -76,5 +72,20 @@ public final class StdLibToJSTest extends SingleFileTranslationTest {
runRhinoTests(getOutputFilePaths(kotlinFilename, ecmaVersions),
new RhinoFunctionNativeObjectResultChecker("test.browser", "foo", "Some Dynamically Created Content!!!"));
*/
// lets add the standard JS library files
for (String libFileName : Config.LIB_FILE_NAMES) {
files.add(Config.LIBRARIES_LOCATION + libFileName);
}
// now lets try invoke the compiler
for (EcmaVersion version : ecmaVersions) {
System.out.println("Compiling with version: " + version);
K2JSCompiler compiler = new K2JSCompiler();
K2JSCompilerArguments arguments = new K2JSCompilerArguments();
arguments.outputFile = getOutputFilePath(getTestName(false) + ".compiler.kt", version);
arguments.sourceFiles = files;
arguments.verbose = true;
compiler.exec(System.out, arguments);
}
}
}
......@@ -51,7 +51,7 @@ public abstract class Config {
}
@NotNull
protected static final List<String> LIB_FILE_NAMES = Arrays.asList(
public static final List<String> LIB_FILE_NAMES = Arrays.asList(
"/core/annotations.kt",
"/jquery/common.kt",
"/jquery/ui.kt",
......@@ -67,11 +67,12 @@ public abstract class Config {
"/html5/image.kt",
"/stdlib/JUMaps.kt",
"/stdlib/browser.kt",
"/core/dom.kt",
"/core/dom/core.kt"
"/core/dom.kt"
// TODO
// "/core/dom/core.kt"
);
protected static final String LIBRARIES_LOCATION = "js/js.libraries/src";
public static final String LIBRARIES_LOCATION = "js/js.libraries/src";
@NotNull
private final Project project;
......
......@@ -30,6 +30,7 @@ import org.jetbrains.k2js.generate.CodeGenerator;
import org.jetbrains.k2js.translate.general.Translation;
import org.jetbrains.k2js.utils.JetFileUtils;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
......@@ -47,8 +48,15 @@ public final class K2JSTranslator {
public static void translateWithCallToMainAndSaveToFile(@NotNull List<JetFile> files,
@NotNull String outputPath,
@NotNull Config config) throws Exception {
translateWithMainCallParametersAndSaveToFile(MainCallParameters.mainWithoutArguments(), files, outputPath, config);
}
public static void translateWithMainCallParametersAndSaveToFile(MainCallParameters mainCall,
List<JetFile> files,
String outputPath,
Config config) throws TranslationException, IOException {
K2JSTranslator translator = new K2JSTranslator(config);
String programCode = translator.generateProgramCode(files, MainCallParameters.mainWithoutArguments()) + "\n";
String programCode = translator.generateProgramCode(files, mainCall) + "\n";
writeCodeToFile(outputPath, programCode);
}
......
......@@ -38,6 +38,14 @@ public class K2JSCompilerMojo extends KotlinCompileMojo {
*/
private String outFile;
/**
* Whether verbose logging is enabled or not.
*
* @parameter default-value="false"
* @parameter expression="${verbose}"
*/
private Boolean verbose;
@Override
protected void configureCompilerArguments(CompilerArguments arguments) throws MojoExecutionException {
super.configureCompilerArguments(arguments);
......@@ -45,9 +53,11 @@ public class K2JSCompilerMojo extends KotlinCompileMojo {
if (arguments instanceof K2JSCompilerArguments) {
K2JSCompilerArguments k2jsArgs = (K2JSCompilerArguments)arguments;
k2jsArgs.outputFile = outFile;
if (verbose != null) {
k2jsArgs.verbose = verbose;
}
if (sources.size() > 0) {
// TODO K2JSCompilerArguments should allow more than one path/file
k2jsArgs.srcdir = sources.get(0);
k2jsArgs.sourceFiles = sources;
}
}
getLog().info("Compiling Kotlin src from " + arguments.getSrc() + " to JavaScript at: " + outFile);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册