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:
+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>
|
||||
|
||||
-104
@@ -1,104 +0,0 @@
|
||||
package org.jetbrains.kotlin.maven;
|
||||
|
||||
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 org.jetbrains.jet.lang.psi.JetFile;
|
||||
import org.jetbrains.k2js.config.Config;
|
||||
import org.jetbrains.k2js.config.EcmaVersion;
|
||||
import org.jetbrains.k2js.utils.JetFileUtils;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.URL;
|
||||
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 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) {
|
||||
super(project, version);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private List<JetFile> initLibFiles(@NotNull Project project) {
|
||||
List<JetFile> libFiles = new ArrayList<JetFile>();
|
||||
Set<URL> urlsLoaded = new HashSet<URL>();
|
||||
try {
|
||||
Enumeration<URL> resources = getClass().getClassLoader().getResources(LIBRARY_SOURCES_FILES);
|
||||
loadLibFiles(resources, project, urlsLoaded, libFiles);
|
||||
resources = Thread.currentThread().getContextClassLoader().getResources(LIBRARY_SOURCES_FILES);
|
||||
loadLibFiles(resources, project, urlsLoaded, libFiles);
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
return libFiles;
|
||||
}
|
||||
|
||||
private void loadLibFiles(Enumeration<URL> resources, @NotNull Project project, Set<URL> urlsLoaded, List<JetFile> libFiles) throws IOException {
|
||||
while (resources != null && resources.hasMoreElements()) {
|
||||
URL url = resources.nextElement();
|
||||
if (url != null) {
|
||||
if (urlsLoaded.add(url)) {
|
||||
System.out.println("Loading Kotlin JS library file: " + url);
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
|
||||
try {
|
||||
while (true) {
|
||||
String line = reader.readLine();
|
||||
if (line == null) {
|
||||
break;
|
||||
} else {
|
||||
line = line.trim();
|
||||
if (line.length() == 0 || line.startsWith("#")) continue;
|
||||
// lets try to discover the file
|
||||
InputStream stream = loadClasspathResource(line);
|
||||
if (stream == null) {
|
||||
System.out.println("WARNING: failed to find JS source file: " + line + " on the classpath");
|
||||
} else {
|
||||
//System.out.println("Loading JS library file: " + line);
|
||||
String text = FileUtil.loadTextAndClose(stream);
|
||||
JetFile file = JetFileUtils.createPsiFile(line, text, project);
|
||||
if (file != null) {
|
||||
libFiles.add(file);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
reader.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Tries to load the given resource name on the classpath
|
||||
*/
|
||||
public static InputStream loadClasspathResource(String resourceName) {
|
||||
InputStream answer = JsLibrarySourceConfig.class.getClassLoader().getResourceAsStream(resourceName);
|
||||
if (answer == null) {
|
||||
answer = Thread.currentThread().getContextClassLoader().getResourceAsStream(resourceName);
|
||||
}
|
||||
return answer;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public List<JetFile> generateLibFiles() {
|
||||
if (jsLibFiles == null) {
|
||||
jsLibFiles = initLibFiles(getProject());
|
||||
}
|
||||
return jsLibFiles;
|
||||
}
|
||||
}
|
||||
+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