From d7573b49a6c4bd7c0f71490b70491d9f385a5e96 Mon Sep 17 00:00:00 2001 From: James Strachan Date: Wed, 30 May 2012 11:31:35 +0100 Subject: [PATCH] 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 --- .../jetbrains/jet/cli/js/K2JSCompiler.java | 33 +++++++++++++++---- .../jet/cli/js/K2JSCompilerArguments.java | 25 ++++++++++++++ .../k2js/test/semantics/StdLibToJSTest.java | 23 +++++++++---- .../src/org/jetbrains/k2js/config/Config.java | 9 ++--- .../jetbrains/k2js/facade/K2JSTranslator.java | 10 +++++- .../kotlin/maven/K2JSCompilerMojo.java | 14 ++++++-- 6 files changed, 95 insertions(+), 19 deletions(-) diff --git a/compiler/cli/src/org/jetbrains/jet/cli/js/K2JSCompiler.java b/compiler/cli/src/org/jetbrains/jet/cli/js/K2JSCompiler.java index fb1a6f9834e..31e0df16f0a 100644 --- a/compiler/cli/src/org/jetbrains/jet/cli/js/K2JSCompiler.java +++ b/compiler/cli/src/org/jetbrains/jet/cli/js/K2JSCompiler.java @@ -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 fileNames = Iterables.transform(environmentForJS.getSourceFiles(), new Function() { + @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(), diff --git a/compiler/cli/src/org/jetbrains/jet/cli/js/K2JSCompilerArguments.java b/compiler/cli/src/org/jetbrains/jet/cli/js/K2JSCompilerArguments.java index 4556f1473f4..054a3a2ee29 100644 --- a/compiler/cli/src/org/jetbrains/jet/cli/js/K2JSCompilerArguments.java +++ b/compiler/cli/src/org/jetbrains/jet/cli/js/K2JSCompilerArguments.java @@ -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 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()); + } + } + return MainCallParameters.noCall(); + } } diff --git a/js/js.tests/test/org/jetbrains/k2js/test/semantics/StdLibToJSTest.java b/js/js.tests/test/org/jetbrains/k2js/test/semantics/StdLibToJSTest.java index b79de1a6b82..654f6a9a68c 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/semantics/StdLibToJSTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/semantics/StdLibToJSTest.java @@ -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 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); + } } } diff --git a/js/js.translator/src/org/jetbrains/k2js/config/Config.java b/js/js.translator/src/org/jetbrains/k2js/config/Config.java index b12e6a35325..6c2b2c5aa32 100644 --- a/js/js.translator/src/org/jetbrains/k2js/config/Config.java +++ b/js/js.translator/src/org/jetbrains/k2js/config/Config.java @@ -51,7 +51,7 @@ public abstract class Config { } @NotNull - protected static final List LIB_FILE_NAMES = Arrays.asList( + public static final List 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; diff --git a/js/js.translator/src/org/jetbrains/k2js/facade/K2JSTranslator.java b/js/js.translator/src/org/jetbrains/k2js/facade/K2JSTranslator.java index acf9631371d..f951becaac0 100644 --- a/js/js.translator/src/org/jetbrains/k2js/facade/K2JSTranslator.java +++ b/js/js.translator/src/org/jetbrains/k2js/facade/K2JSTranslator.java @@ -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 files, @NotNull String outputPath, @NotNull Config config) throws Exception { + translateWithMainCallParametersAndSaveToFile(MainCallParameters.mainWithoutArguments(), files, outputPath, config); + } + + public static void translateWithMainCallParametersAndSaveToFile(MainCallParameters mainCall, + List 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); } diff --git a/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/K2JSCompilerMojo.java b/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/K2JSCompilerMojo.java index ff92c2fa0ed..c34c1475646 100644 --- a/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/K2JSCompilerMojo.java +++ b/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/K2JSCompilerMojo.java @@ -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);