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
This commit is contained in:
@@ -47,4 +47,6 @@ public abstract class CompilerArguments {
|
||||
public abstract boolean isTags();
|
||||
public abstract boolean isVersion();
|
||||
public abstract boolean isVerbose();
|
||||
|
||||
public abstract String getSrc();
|
||||
}
|
||||
|
||||
@@ -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<K2JSCompilerArguments, K2JSCompile
|
||||
private static JetCoreEnvironment getEnvironment(K2JSCompilerArguments arguments, Disposable rootDisposable) {
|
||||
final JetCoreEnvironment environmentForJS = JetCoreEnvironment.getCoreEnvironmentForJS(rootDisposable);
|
||||
environmentForJS.addSources(arguments.srcdir);
|
||||
System.out.println("Compiling source files: " + environmentForJS.getSourceFiles());
|
||||
return environmentForJS;
|
||||
}
|
||||
|
||||
@@ -97,6 +99,9 @@ public class K2JSCompiler extends CLICompiler<K2JSCompilerArguments, K2JSCompile
|
||||
catch (Exception e) {
|
||||
messageCollector.report(CompilerMessageSeverity.ERROR, "Exception while translating:\n" + e.getMessage(),
|
||||
CompilerMessageLocation.NO_LOCATION);
|
||||
// TODO we should report the exception nicely to the collector so it can report
|
||||
// for example inside a mvn plugin we need to see the stack trace
|
||||
e.printStackTrace();
|
||||
return ExitCode.INTERNAL_ERROR;
|
||||
}
|
||||
return ExitCode.OK;
|
||||
@@ -118,7 +123,9 @@ public class K2JSCompiler extends CLICompiler<K2JSCompilerArguments, K2JSCompile
|
||||
private static Config getConfig(@NotNull K2JSCompilerArguments arguments, @NotNull Project project) {
|
||||
EcmaVersion ecmaVersion = EcmaVersion.fromString(arguments.target);
|
||||
if (arguments.libzip == null) {
|
||||
return Config.getEmptyConfig(project, ecmaVersion);
|
||||
// lets discover the JS library source on the classpath
|
||||
return new ClassPathLibrarySourcesConfig(project, ecmaVersion);
|
||||
//return Config.getEmptyConfig(project, ecmaVersion);
|
||||
}
|
||||
return new ZippedLibrarySourcesConfig(project, arguments.libzip, ecmaVersion);
|
||||
}
|
||||
|
||||
@@ -72,4 +72,9 @@ public class K2JSCompilerArguments extends CompilerArguments {
|
||||
public boolean isVerbose() {
|
||||
return verbose;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSrc() {
|
||||
return srcdir;
|
||||
}
|
||||
}
|
||||
|
||||
+22
-6
@@ -1,9 +1,25 @@
|
||||
package org.jetbrains.kotlin.maven;
|
||||
/*
|
||||
* 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.k2js.config;
|
||||
|
||||
import com.intellij.openapi.util.io.FileUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.internal.com.intellij.openapi.project.Project;
|
||||
import org.jetbrains.jet.internal.com.intellij.openapi.util.io.FileUtil;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import org.jetbrains.jet.lang.psi.JetFile;
|
||||
import org.jetbrains.k2js.config.Config;
|
||||
import org.jetbrains.k2js.config.EcmaVersion;
|
||||
@@ -19,13 +35,13 @@ import java.util.*;
|
||||
/**
|
||||
* A Config implementation which is configured with a directory to find the standard library names from
|
||||
*/
|
||||
public class JsLibrarySourceConfig extends Config {
|
||||
public class ClassPathLibrarySourcesConfig extends Config {
|
||||
|
||||
public static final String LIBRARY_SOURCES_FILES = "META-INF/services/org.jetbrains.kotlin.js.librarySource";
|
||||
@Nullable
|
||||
private /*var*/ List<JetFile> 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);
|
||||
}
|
||||
@@ -1,6 +1,11 @@
|
||||
package sample
|
||||
|
||||
class Hello {
|
||||
public fun main(args: Array<String>): Unit {
|
||||
val hello = Hello()
|
||||
hello.doSomething()
|
||||
}
|
||||
|
||||
public class Hello {
|
||||
var x = 0
|
||||
|
||||
fun doSomething(): Unit {
|
||||
|
||||
+1
-1
@@ -67,9 +67,9 @@
|
||||
|
||||
<!--
|
||||
TODO temporary disabled until we can get the kotlin/dom code compiled as JS
|
||||
<module>examples/js-example</module>
|
||||
<module>examples/browser-example</module>
|
||||
-->
|
||||
<module>examples/js-example</module>
|
||||
</modules>
|
||||
|
||||
<dependencies>
|
||||
|
||||
+5
-2
@@ -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;
|
||||
|
||||
@@ -30,8 +30,15 @@
|
||||
<delete dir="${basedir}/target/generated-js-library" failonerror="false" />
|
||||
<mkdir dir="${basedir}/target/generated-js-library"/>
|
||||
<copy todir="${basedir}/target/generated-js-library">
|
||||
<fileset dir="${kotlin-js-lib-srcdir}"/>
|
||||
<fileset dir="${kotlin-js-lib-srcdir}">
|
||||
<exclude name="core/dom/core.kt"/>
|
||||
</fileset>
|
||||
</copy>
|
||||
<!--
|
||||
|
||||
TODO this should compile soon hopefully :)
|
||||
disabled for now as it crashes the mvn plugin for JS goal
|
||||
|
||||
<copy todir="${basedir}/target/generated-js-library/kotlin">
|
||||
<fileset dir="${basedir}/../../stdlib/src/kotlin/dom">
|
||||
<include name="**/*.kt"/>
|
||||
@@ -41,10 +48,11 @@
|
||||
<include name="**/*.kt"/>
|
||||
<exclude name="**/*JVM.kt"/>
|
||||
</fileset>
|
||||
<fileset file="${basedir}/../../stdlib/src/kotlin">
|
||||
<fileset dir="${basedir}/../../stdlib/src/kotlin">
|
||||
<include name="Preconditions.kt"/>
|
||||
</fileset>
|
||||
</copy>
|
||||
-->
|
||||
<copy tofile="${basedir}/target/generated-js-library/kotlin-lib.js"
|
||||
file="${kotlin-js-lib-srcdir}/../../js.translator/testFiles/kotlin_lib.js"/>
|
||||
</target>
|
||||
|
||||
+23
-6
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
-93
@@ -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<JetFile> 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<InputStream>() {
|
||||
@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;
|
||||
}
|
||||
}
|
||||
+5
-2
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+8
-6
@@ -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<String> sources, List<String> classpath, String output) throws MojoExecutionException {
|
||||
|
||||
+7
-4
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user