From 4921aeafc3f4f621f97f120c0f36c5211272b18c Mon Sep 17 00:00:00 2001 From: James Strachan Date: Tue, 29 May 2012 17:28:15 +0100 Subject: [PATCH] refactored the maven plugin to use the vanilla K2JSCompiler directly and avoid using the K2JVMCompiler when generating JS code; also included a default LibrarySourceConfig which detects JS library code on the classpath which works nicer in maven/ant style worlds where dependencies tend to be specified rather than file paths to zip files --- .../jet/cli/common/CompilerArguments.java | 2 + .../jetbrains/jet/cli/js/K2JSCompiler.java | 9 +- .../jet/cli/js/K2JSCompilerArguments.java | 5 + .../config/ClassPathLibrarySourcesConfig.java | 28 ++++-- .../src/main/kotlin/sample/Hello.kt | 7 +- libraries/pom.xml | 2 +- .../jetbrains/kotlin/maven/doc/KDocMojo.java | 7 +- libraries/tools/kotlin-js-library/pom.xml | 12 ++- .../kotlin/maven/K2JSCompilerMojo.java | 29 ++++-- .../kotlin/maven/K2JSCompilerPlugin.java | 93 ------------------- .../kotlin/maven/KotlinCompileMojo.java | 7 +- .../kotlin/maven/KotlinCompileMojoBase.java | 14 +-- .../kotlin/maven/KotlinTestCompileMojo.java | 11 ++- 13 files changed, 102 insertions(+), 124 deletions(-) rename libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/JsLibrarySourceConfig.java => js/js.translator/src/org/jetbrains/k2js/config/ClassPathLibrarySourcesConfig.java (79%) delete mode 100644 libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/K2JSCompilerPlugin.java diff --git a/compiler/cli/src/org/jetbrains/jet/cli/common/CompilerArguments.java b/compiler/cli/src/org/jetbrains/jet/cli/common/CompilerArguments.java index 69eb2e54ef8..7064c1ab13e 100644 --- a/compiler/cli/src/org/jetbrains/jet/cli/common/CompilerArguments.java +++ b/compiler/cli/src/org/jetbrains/jet/cli/common/CompilerArguments.java @@ -47,4 +47,6 @@ public abstract class CompilerArguments { public abstract boolean isTags(); public abstract boolean isVersion(); public abstract boolean isVerbose(); + + public abstract String getSrc(); } 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 a8e35b5981e..33d1e166dd8 100644 --- a/compiler/cli/src/org/jetbrains/jet/cli/js/K2JSCompiler.java +++ b/compiler/cli/src/org/jetbrains/jet/cli/js/K2JSCompiler.java @@ -32,6 +32,7 @@ import org.jetbrains.jet.cli.common.messages.PrintingMessageCollector; import org.jetbrains.jet.cli.jvm.compiler.JetCoreEnvironment; import org.jetbrains.jet.lang.psi.JetFile; import org.jetbrains.k2js.analyze.AnalyzerFacadeForJS; +import org.jetbrains.k2js.config.ClassPathLibrarySourcesConfig; import org.jetbrains.k2js.config.Config; import org.jetbrains.k2js.config.EcmaVersion; import org.jetbrains.k2js.config.ZippedLibrarySourcesConfig; @@ -85,6 +86,7 @@ public class K2JSCompiler extends CLICompiler jsLibFiles = null; - public JsLibrarySourceConfig(@NotNull Project project, @NotNull EcmaVersion version) { + public ClassPathLibrarySourcesConfig(@NotNull Project project, @NotNull EcmaVersion version) { super(project, version); } @@ -86,7 +102,7 @@ public class JsLibrarySourceConfig extends Config { * Tries to load the given resource name on the classpath */ public static InputStream loadClasspathResource(String resourceName) { - InputStream answer = JsLibrarySourceConfig.class.getClassLoader().getResourceAsStream(resourceName); + InputStream answer = ClassPathLibrarySourcesConfig.class.getClassLoader().getResourceAsStream(resourceName); if (answer == null) { answer = Thread.currentThread().getContextClassLoader().getResourceAsStream(resourceName); } diff --git a/libraries/examples/js-example/src/main/kotlin/sample/Hello.kt b/libraries/examples/js-example/src/main/kotlin/sample/Hello.kt index b98a9973ad5..2167d01ae73 100644 --- a/libraries/examples/js-example/src/main/kotlin/sample/Hello.kt +++ b/libraries/examples/js-example/src/main/kotlin/sample/Hello.kt @@ -1,6 +1,11 @@ package sample -class Hello { +public fun main(args: Array): Unit { + val hello = Hello() + hello.doSomething() +} + +public class Hello { var x = 0 fun doSomething(): Unit { diff --git a/libraries/pom.xml b/libraries/pom.xml index 93e80eca007..3325f44c424 100644 --- a/libraries/pom.xml +++ b/libraries/pom.xml @@ -67,9 +67,9 @@ + examples/js-example diff --git a/libraries/tools/kdoc-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/doc/KDocMojo.java b/libraries/tools/kdoc-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/doc/KDocMojo.java index d2dea0a4266..0424942ea0a 100644 --- a/libraries/tools/kdoc-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/doc/KDocMojo.java +++ b/libraries/tools/kdoc-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/doc/KDocMojo.java @@ -17,6 +17,7 @@ package org.jetbrains.kotlin.maven.doc; import org.apache.maven.plugin.MojoExecutionException; +import org.jetbrains.jet.cli.common.CompilerArguments; import org.jetbrains.jet.cli.jvm.K2JVMCompilerArguments; import org.jetbrains.jet.cli.jvm.K2JVMCompiler; import org.jetbrains.kotlin.doc.KDocArguments; @@ -174,8 +175,10 @@ public class KDocMojo extends KotlinCompileMojoBase { } @Override - protected void configureCompilerArguments(K2JVMCompilerArguments arguments) throws MojoExecutionException { - configureBaseCompilerArguments(getLog(), arguments, docModule, sources, classpath, output); + protected void configureCompilerArguments(CompilerArguments arguments) throws MojoExecutionException { + if (arguments instanceof K2JVMCompilerArguments) { + configureBaseCompilerArguments(getLog(), (K2JVMCompilerArguments) arguments, docModule, sources, classpath, output); + } if (arguments instanceof KDocArguments) { KDocArguments kdoc = (KDocArguments) arguments; diff --git a/libraries/tools/kotlin-js-library/pom.xml b/libraries/tools/kotlin-js-library/pom.xml index 7992b6ca9a6..e960d25b030 100644 --- a/libraries/tools/kotlin-js-library/pom.xml +++ b/libraries/tools/kotlin-js-library/pom.xml @@ -30,8 +30,15 @@ - + + + + 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 769a12ea9ba..ff92c2fa0ed 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 @@ -17,7 +17,10 @@ package org.jetbrains.kotlin.maven; import org.apache.maven.plugin.MojoExecutionException; -import org.jetbrains.jet.cli.jvm.K2JVMCompilerArguments; +import org.jetbrains.jet.cli.common.CLICompiler; +import org.jetbrains.jet.cli.common.CompilerArguments; +import org.jetbrains.jet.cli.js.K2JSCompiler; +import org.jetbrains.jet.cli.js.K2JSCompilerArguments; /** * Converts Kotlin to JavaScript code @@ -36,13 +39,27 @@ public class K2JSCompilerMojo extends KotlinCompileMojo { private String outFile; @Override - protected void configureCompilerArguments(K2JVMCompilerArguments arguments) throws MojoExecutionException { + protected void configureCompilerArguments(CompilerArguments arguments) throws MojoExecutionException { super.configureCompilerArguments(arguments); - K2JSCompilerPlugin plugin = new K2JSCompilerPlugin(); - plugin.setOutFile(outFile); - arguments.getCompilerPlugins().add(plugin); - + if (arguments instanceof K2JSCompilerArguments) { + K2JSCompilerArguments k2jsArgs = (K2JSCompilerArguments)arguments; + k2jsArgs.outputFile = outFile; + if (sources.size() > 0) { + // TODO K2JSCompilerArguments should allow more than one path/file + k2jsArgs.srcdir = sources.get(0); + } + } getLog().info("Compiling Kotlin src from " + arguments.getSrc() + " to JavaScript at: " + outFile); } + + @Override + protected CompilerArguments createCompilerArguments() { + return new K2JSCompilerArguments(); + } + + @Override + protected CLICompiler createCompiler() { + return new K2JSCompiler(); + } } diff --git a/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/K2JSCompilerPlugin.java b/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/K2JSCompilerPlugin.java deleted file mode 100644 index d8c22fcb1d4..00000000000 --- a/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/K2JSCompilerPlugin.java +++ /dev/null @@ -1,93 +0,0 @@ -/* - * Copyright 2010-2012 JetBrains s.r.o. - * - * 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.jetbrains.kotlin.maven; - -import com.google.common.io.Files; -import com.google.common.io.InputSupplier; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.jet.cli.common.CompilerPlugin; -import org.jetbrains.jet.cli.common.CompilerPluginContext; -import org.jetbrains.jet.internal.com.intellij.openapi.project.Project; -import org.jetbrains.jet.lang.psi.JetFile; -import org.jetbrains.jet.lang.resolve.BindingContext; -import org.jetbrains.k2js.config.Config; -import org.jetbrains.k2js.config.EcmaVersion; -import org.jetbrains.k2js.facade.K2JSTranslator; -import org.jetbrains.k2js.facade.MainCallParameters; - -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.nio.charset.Charset; -import java.util.List; - -/** - * Compiles Kotlin code to JavaScript - */ -public class K2JSCompilerPlugin implements CompilerPlugin { - public static final String KOTLIN_JS_LIB = "kotlin-lib.js"; - private String outFile = "target/js/program.js"; - - public K2JSCompilerPlugin() { - } - - @Override - public void processFiles(@NotNull CompilerPluginContext context) { - Project project = context.getProject(); - BindingContext bindingContext = context.getContext(); - List sources = context.getFiles(); - - if (bindingContext != null && sources != null && project != null) { - Config config = new JsLibrarySourceConfig(project, EcmaVersion.defaultVersion()); - - // lets copy the kotlin library into the output directory - try { - File parentFile = new File(outFile).getParentFile(); - parentFile.mkdirs(); - final InputStream inputStream = JsLibrarySourceConfig.loadClasspathResource(KOTLIN_JS_LIB); - if (inputStream == null) { - System.out.println("WARNING: Could not find " + KOTLIN_JS_LIB + " on the classpath!"); - } else { - Files.copy(new InputSupplier() { - @Override - public InputStream getInput() throws IOException { - return inputStream; - } - }, new File(parentFile, "kotlin-lib.js")); - } - } catch (IOException e) { - throw new RuntimeException(e); - } - - try { - K2JSTranslator translator = new K2JSTranslator(config); - - final String code = translator.generateProgramCode(sources, MainCallParameters.noCall()); - - File file = new File(outFile); - Files.createParentDirs(file); - Files.write(code, file, Charset.forName("UTF-8")); - } catch (Exception e) { - throw new RuntimeException(e); - } - } - } - - public void setOutFile(String outFile) { - this.outFile = outFile; - } -} diff --git a/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/KotlinCompileMojo.java b/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/KotlinCompileMojo.java index 2e2edbb5868..a4dc2576d3a 100644 --- a/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/KotlinCompileMojo.java +++ b/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/KotlinCompileMojo.java @@ -17,6 +17,7 @@ package org.jetbrains.kotlin.maven; import org.apache.maven.plugin.MojoExecutionException; +import org.jetbrains.jet.cli.common.CompilerArguments; import org.jetbrains.jet.cli.jvm.K2JVMCompilerArguments; /** @@ -29,8 +30,10 @@ import org.jetbrains.jet.cli.jvm.K2JVMCompilerArguments; */ public class KotlinCompileMojo extends KotlinCompileMojoBase { @Override - protected void configureCompilerArguments(K2JVMCompilerArguments arguments) throws MojoExecutionException { - configureBaseCompilerArguments(getLog(), arguments, module, sources, classpath, output); + protected void configureCompilerArguments(CompilerArguments arguments) throws MojoExecutionException { + if (arguments instanceof K2JVMCompilerArguments) { + configureBaseCompilerArguments(getLog(), (K2JVMCompilerArguments) arguments, module, sources, classpath, output); + } } } diff --git a/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/KotlinCompileMojoBase.java b/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/KotlinCompileMojoBase.java index c60d912797e..68c7b255e1a 100644 --- a/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/KotlinCompileMojoBase.java +++ b/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/KotlinCompileMojoBase.java @@ -23,6 +23,8 @@ import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugin.logging.Log; +import org.jetbrains.jet.cli.common.CLICompiler; +import org.jetbrains.jet.cli.common.CompilerArguments; import org.jetbrains.jet.cli.common.ExitCode; import org.jetbrains.jet.cli.jvm.K2JVMCompiler; import org.jetbrains.jet.cli.jvm.K2JVMCompilerArguments; @@ -105,11 +107,11 @@ public abstract class KotlinCompileMojoBase extends AbstractMojo { @Override public void execute() throws MojoExecutionException, MojoFailureException { - final K2JVMCompilerArguments arguments = createCompilerArguments(); + final CompilerArguments arguments = createCompilerArguments(); configureCompilerArguments(arguments); - final K2JVMCompiler compiler = createCompiler(); + final CLICompiler compiler = createCompiler(); printCompilerArgumentsIfDebugEnabled(arguments, compiler); @@ -124,7 +126,7 @@ public abstract class KotlinCompileMojoBase extends AbstractMojo { } } - private void printCompilerArgumentsIfDebugEnabled(K2JVMCompilerArguments arguments, K2JVMCompiler compiler) { + private void printCompilerArgumentsIfDebugEnabled(CompilerArguments arguments, CLICompiler compiler) { if (getLog().isDebugEnabled()) { getLog().debug("Invoking compiler " + compiler + " with arguments:"); try { @@ -143,7 +145,7 @@ public abstract class KotlinCompileMojoBase extends AbstractMojo { } } - protected K2JVMCompiler createCompiler() { + protected CLICompiler createCompiler() { return new K2JVMCompiler(); } @@ -151,14 +153,14 @@ public abstract class KotlinCompileMojoBase extends AbstractMojo { * Derived classes can create custom compiler argument implementations * such as for KDoc */ - protected K2JVMCompilerArguments createCompilerArguments() { + protected CompilerArguments createCompilerArguments() { return new K2JVMCompilerArguments(); } /** * Derived classes can register custom plugins or configurations */ - protected abstract void configureCompilerArguments(K2JVMCompilerArguments arguments) throws MojoExecutionException; + protected abstract void configureCompilerArguments(CompilerArguments arguments) throws MojoExecutionException; protected void configureBaseCompilerArguments(Log log, K2JVMCompilerArguments arguments, String module, List sources, List classpath, String output) throws MojoExecutionException { diff --git a/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/KotlinTestCompileMojo.java b/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/KotlinTestCompileMojo.java index 03a00389bf3..0253ad02bea 100644 --- a/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/KotlinTestCompileMojo.java +++ b/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/KotlinTestCompileMojo.java @@ -18,6 +18,7 @@ package org.jetbrains.kotlin.maven; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; +import org.jetbrains.jet.cli.common.CompilerArguments; import org.jetbrains.jet.cli.jvm.K2JVMCompilerArguments; /** @@ -47,9 +48,11 @@ public class KotlinTestCompileMojo extends KotlinCompileMojoBase { } @Override - protected void configureCompilerArguments(K2JVMCompilerArguments arguments) throws MojoExecutionException { - configureBaseCompilerArguments( - getLog(), arguments, - testModule, testSources, testClasspath, testOutput); + protected void configureCompilerArguments(CompilerArguments arguments) throws MojoExecutionException { + if (arguments instanceof K2JVMCompilerArguments) { + configureBaseCompilerArguments( + getLog(), (K2JVMCompilerArguments) arguments, + testModule, testSources, testClasspath, testOutput); + } } }