allow the JS compiler to reference the JS library source so that maven based JS compilation can work & use the new kotlin.browser API
This commit is contained in:
@@ -31,6 +31,9 @@
|
|||||||
<groupId>org.jetbrains.kotlin</groupId>
|
<groupId>org.jetbrains.kotlin</groupId>
|
||||||
<artifactId>kotlin-maven-plugin</artifactId>
|
<artifactId>kotlin-maven-plugin</artifactId>
|
||||||
<version>${project.version}</version>
|
<version>${project.version}</version>
|
||||||
|
<configuration>
|
||||||
|
<jsLibrarySourceDir>${kotlin-js-lib-srcdir}</jsLibrarySourceDir>
|
||||||
|
</configuration>
|
||||||
<executions>
|
<executions>
|
||||||
<execution>
|
<execution>
|
||||||
<id>js</id>
|
<id>js</id>
|
||||||
|
|||||||
@@ -0,0 +1,19 @@
|
|||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>Kotlin Sample</h1>
|
||||||
|
|
||||||
|
<div id="foo">
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<script type="text/javascript" src="target/js/kotlin-lib.js">
|
||||||
|
</script>
|
||||||
|
<script type="text/javascript" src="target/js/browser-example.js">
|
||||||
|
</script>
|
||||||
|
<script type="text/javascript">
|
||||||
|
</script>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -5,6 +5,6 @@ import kotlin.browser.document
|
|||||||
fun myApp() {
|
fun myApp() {
|
||||||
val element = document.getElementById("foo")
|
val element = document.getElementById("foo")
|
||||||
if (element != null) {
|
if (element != null) {
|
||||||
element.appendChild(document.createTextNode("Some Dynamically Created Contenet!!!"))
|
element.appendChild(document.createTextNode("Some Dynamically Created Content!!!"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,6 +31,9 @@
|
|||||||
<groupId>org.jetbrains.kotlin</groupId>
|
<groupId>org.jetbrains.kotlin</groupId>
|
||||||
<artifactId>kotlin-maven-plugin</artifactId>
|
<artifactId>kotlin-maven-plugin</artifactId>
|
||||||
<version>${project.version}</version>
|
<version>${project.version}</version>
|
||||||
|
<configuration>
|
||||||
|
<jsLibrarySourceDir>${kotlin-js-lib-srcdir}</jsLibrarySourceDir>
|
||||||
|
</configuration>
|
||||||
<executions>
|
<executions>
|
||||||
<execution>
|
<execution>
|
||||||
<id>js</id>
|
<id>js</id>
|
||||||
|
|||||||
@@ -20,6 +20,7 @@
|
|||||||
<junit-version>4.10</junit-version>
|
<junit-version>4.10</junit-version>
|
||||||
<kotlin-maven-plugin.version>0.2.3.8</kotlin-maven-plugin.version>
|
<kotlin-maven-plugin.version>0.2.3.8</kotlin-maven-plugin.version>
|
||||||
<kotlin-dist>${project-root}/dist</kotlin-dist>
|
<kotlin-dist>${project-root}/dist</kotlin-dist>
|
||||||
|
<kotlin-js-lib-srcdir>${project-root}/js/js.libraries/src</kotlin-js-lib-srcdir>
|
||||||
<kotlin-sdk>${kotlin-dist}/kotlinc</kotlin-sdk>
|
<kotlin-sdk>${kotlin-dist}/kotlinc</kotlin-sdk>
|
||||||
<maven.compiler.source>1.6</maven.compiler.source>
|
<maven.compiler.source>1.6</maven.compiler.source>
|
||||||
<maven.compiler.target>1.6</maven.compiler.target>
|
<maven.compiler.target>1.6</maven.compiler.target>
|
||||||
@@ -62,7 +63,12 @@
|
|||||||
<module>docs/website</module>
|
<module>docs/website</module>
|
||||||
|
|
||||||
<module>examples/kotlin-java-example</module>
|
<module>examples/kotlin-java-example</module>
|
||||||
|
<!--
|
||||||
|
TODO - lets comment out for now until we've found a simpler way
|
||||||
|
of finding the JS ilbrary source to compile
|
||||||
|
|
||||||
<module>examples/js-example</module>
|
<module>examples/js-example</module>
|
||||||
|
-->
|
||||||
</modules>
|
</modules>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
|||||||
+66
@@ -0,0 +1,66 @@
|
|||||||
|
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.FileInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Config implementation which is configured with a directory to find the standard library names from
|
||||||
|
*/
|
||||||
|
public class JsLibrarySourceConfig extends Config {
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private /*var*/ List<JetFile> jsLibFiles = null;
|
||||||
|
@NotNull
|
||||||
|
private String librarySourceDir;
|
||||||
|
|
||||||
|
public JsLibrarySourceConfig(@NotNull Project project, @NotNull EcmaVersion version, @NotNull String librarySourceDir) {
|
||||||
|
super(project, version);
|
||||||
|
this.librarySourceDir = librarySourceDir;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private List<JetFile> initLibFiles(@NotNull Project project) {
|
||||||
|
List<JetFile> libFiles = new ArrayList<JetFile>();
|
||||||
|
for (String libFileName : LIB_FILE_NAMES) {
|
||||||
|
JetFile file = null;
|
||||||
|
try {
|
||||||
|
@SuppressWarnings("IOResourceOpenedButNotSafelyClosed")
|
||||||
|
InputStream stream = new FileInputStream(librarySourceDir + libFileName);
|
||||||
|
try {
|
||||||
|
String text = FileUtil.loadTextAndClose(stream);
|
||||||
|
file = JetFileUtils.createPsiFile(libFileName, text, project);
|
||||||
|
}
|
||||||
|
catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
libFiles.add(file);
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
//TODO: throw generic exception
|
||||||
|
throw new IllegalStateException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return libFiles;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@NotNull
|
||||||
|
public List<JetFile> generateLibFiles() {
|
||||||
|
if (jsLibFiles == null) {
|
||||||
|
jsLibFiles = initLibFiles(getProject());
|
||||||
|
}
|
||||||
|
return jsLibFiles;
|
||||||
|
}
|
||||||
|
}
|
||||||
+9
-1
@@ -35,11 +35,19 @@ public class K2JSCompilerMojo extends KotlinCompileMojo {
|
|||||||
*/
|
*/
|
||||||
private String outFile;
|
private String outFile;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The Kotlin JavaScript library source code
|
||||||
|
*
|
||||||
|
* @required
|
||||||
|
* @parameter expression="${jsLibrarySourceDir}"
|
||||||
|
*/
|
||||||
|
private String jsLibrarySourceDir;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void configureCompilerArguments(K2JVMCompilerArguments arguments) throws MojoExecutionException {
|
protected void configureCompilerArguments(K2JVMCompilerArguments arguments) throws MojoExecutionException {
|
||||||
super.configureCompilerArguments(arguments);
|
super.configureCompilerArguments(arguments);
|
||||||
|
|
||||||
K2JSCompilerPlugin plugin = new K2JSCompilerPlugin();
|
K2JSCompilerPlugin plugin = new K2JSCompilerPlugin(jsLibrarySourceDir);
|
||||||
plugin.setOutFile(outFile);
|
plugin.setOutFile(outFile);
|
||||||
arguments.getCompilerPlugins().add(plugin);
|
arguments.getCompilerPlugins().add(plugin);
|
||||||
|
|
||||||
|
|||||||
+22
-1
@@ -24,10 +24,12 @@ import org.jetbrains.jet.internal.com.intellij.openapi.project.Project;
|
|||||||
import org.jetbrains.jet.lang.psi.JetFile;
|
import org.jetbrains.jet.lang.psi.JetFile;
|
||||||
import org.jetbrains.jet.lang.resolve.BindingContext;
|
import org.jetbrains.jet.lang.resolve.BindingContext;
|
||||||
import org.jetbrains.k2js.config.Config;
|
import org.jetbrains.k2js.config.Config;
|
||||||
|
import org.jetbrains.k2js.config.EcmaVersion;
|
||||||
import org.jetbrains.k2js.facade.K2JSTranslator;
|
import org.jetbrains.k2js.facade.K2JSTranslator;
|
||||||
import org.jetbrains.k2js.facade.MainCallParameters;
|
import org.jetbrains.k2js.facade.MainCallParameters;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -36,8 +38,13 @@ import java.util.List;
|
|||||||
* Compiles Kotlin code to JavaScript
|
* Compiles Kotlin code to JavaScript
|
||||||
*/
|
*/
|
||||||
public class K2JSCompilerPlugin implements CompilerPlugin {
|
public class K2JSCompilerPlugin implements CompilerPlugin {
|
||||||
|
private final String jsLibrarySourceDir;
|
||||||
private String outFile = "target/js/program.js";
|
private String outFile = "target/js/program.js";
|
||||||
|
|
||||||
|
public K2JSCompilerPlugin(String jsLibrarySourceDir) {
|
||||||
|
this.jsLibrarySourceDir = jsLibrarySourceDir;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void processFiles(@NotNull CompilerPluginContext context) {
|
public void processFiles(@NotNull CompilerPluginContext context) {
|
||||||
Project project = context.getProject();
|
Project project = context.getProject();
|
||||||
@@ -45,7 +52,21 @@ public class K2JSCompilerPlugin implements CompilerPlugin {
|
|||||||
List<JetFile> sources = context.getFiles();
|
List<JetFile> sources = context.getFiles();
|
||||||
|
|
||||||
if (bindingContext != null && sources != null && project != null) {
|
if (bindingContext != null && sources != null && project != null) {
|
||||||
Config config = Config.getEmptyConfig(project);
|
Config config;
|
||||||
|
if (jsLibrarySourceDir != null) {
|
||||||
|
config = new JsLibrarySourceConfig(project, EcmaVersion.defaultVersion(), jsLibrarySourceDir);
|
||||||
|
|
||||||
|
// lets copy the kotlin library into the output directory
|
||||||
|
try {
|
||||||
|
File parentFile = new File(outFile).getParentFile();
|
||||||
|
parentFile.mkdirs();
|
||||||
|
Files.copy(new File(jsLibrarySourceDir, "../../js.translator/testFiles/kotlin_lib.js"), new File(parentFile, "kotlin-lib.js"));
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
config = Config.getEmptyConfig(project);
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user