From 1254aa9e6f4eb4037fdc207eddd960b279de4df8 Mon Sep 17 00:00:00 2001 From: James Strachan Date: Sat, 26 May 2012 07:45:10 +0100 Subject: [PATCH] allow the JS compiler to reference the JS library source so that maven based JS compilation can work & use the new kotlin.browser API --- libraries/examples/browser-example/pom.xml | 3 + .../examples/browser-example/sample.html | 19 ++++++ .../src/main/kotlin/sample/Hello.kt | 2 +- libraries/examples/js-example/pom.xml | 3 + libraries/pom.xml | 6 ++ .../kotlin/maven/JsLibrarySourceConfig.java | 66 +++++++++++++++++++ .../kotlin/maven/K2JSCompilerMojo.java | 10 ++- .../kotlin/maven/K2JSCompilerPlugin.java | 23 ++++++- 8 files changed, 129 insertions(+), 3 deletions(-) create mode 100644 libraries/examples/browser-example/sample.html create mode 100644 libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/JsLibrarySourceConfig.java diff --git a/libraries/examples/browser-example/pom.xml b/libraries/examples/browser-example/pom.xml index b0bfa880694..29e4d6f3135 100644 --- a/libraries/examples/browser-example/pom.xml +++ b/libraries/examples/browser-example/pom.xml @@ -31,6 +31,9 @@ org.jetbrains.kotlin kotlin-maven-plugin ${project.version} + + ${kotlin-js-lib-srcdir} + js diff --git a/libraries/examples/browser-example/sample.html b/libraries/examples/browser-example/sample.html new file mode 100644 index 00000000000..79052238cda --- /dev/null +++ b/libraries/examples/browser-example/sample.html @@ -0,0 +1,19 @@ + + + + + +

Kotlin Sample

+ +
+
+ + + + + + + \ No newline at end of file diff --git a/libraries/examples/browser-example/src/main/kotlin/sample/Hello.kt b/libraries/examples/browser-example/src/main/kotlin/sample/Hello.kt index 22e8d821fbf..d0f3a0bbca8 100644 --- a/libraries/examples/browser-example/src/main/kotlin/sample/Hello.kt +++ b/libraries/examples/browser-example/src/main/kotlin/sample/Hello.kt @@ -5,6 +5,6 @@ import kotlin.browser.document fun myApp() { val element = document.getElementById("foo") if (element != null) { - element.appendChild(document.createTextNode("Some Dynamically Created Contenet!!!")) + element.appendChild(document.createTextNode("Some Dynamically Created Content!!!")) } } diff --git a/libraries/examples/js-example/pom.xml b/libraries/examples/js-example/pom.xml index 6eb41e36d62..3a667517efc 100644 --- a/libraries/examples/js-example/pom.xml +++ b/libraries/examples/js-example/pom.xml @@ -31,6 +31,9 @@ org.jetbrains.kotlin kotlin-maven-plugin ${project.version} + + ${kotlin-js-lib-srcdir} + js diff --git a/libraries/pom.xml b/libraries/pom.xml index 4d46e650692..7593309b7a8 100644 --- a/libraries/pom.xml +++ b/libraries/pom.xml @@ -20,6 +20,7 @@ 4.10 0.2.3.8 ${project-root}/dist + ${project-root}/js/js.libraries/src ${kotlin-dist}/kotlinc 1.6 1.6 @@ -62,7 +63,12 @@ docs/website examples/kotlin-java-example + diff --git a/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/JsLibrarySourceConfig.java b/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/JsLibrarySourceConfig.java new file mode 100644 index 00000000000..2e8edefdcda --- /dev/null +++ b/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/JsLibrarySourceConfig.java @@ -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 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 initLibFiles(@NotNull Project project) { + List libFiles = new ArrayList(); + 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 generateLibFiles() { + if (jsLibFiles == null) { + jsLibFiles = initLibFiles(getProject()); + } + return jsLibFiles; + } +} 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..6639131922c 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 @@ -35,11 +35,19 @@ public class K2JSCompilerMojo extends KotlinCompileMojo { */ private String outFile; + /** + * The Kotlin JavaScript library source code + * + * @required + * @parameter expression="${jsLibrarySourceDir}" + */ + private String jsLibrarySourceDir; + @Override protected void configureCompilerArguments(K2JVMCompilerArguments arguments) throws MojoExecutionException { super.configureCompilerArguments(arguments); - K2JSCompilerPlugin plugin = new K2JSCompilerPlugin(); + K2JSCompilerPlugin plugin = new K2JSCompilerPlugin(jsLibrarySourceDir); plugin.setOutFile(outFile); arguments.getCompilerPlugins().add(plugin); 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 index 7e68e648cad..493476b93f9 100644 --- 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 @@ -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.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.nio.charset.Charset; import java.util.ArrayList; import java.util.List; @@ -36,8 +38,13 @@ import java.util.List; * Compiles Kotlin code to JavaScript */ public class K2JSCompilerPlugin implements CompilerPlugin { + private final String jsLibrarySourceDir; private String outFile = "target/js/program.js"; + public K2JSCompilerPlugin(String jsLibrarySourceDir) { + this.jsLibrarySourceDir = jsLibrarySourceDir; + } + @Override public void processFiles(@NotNull CompilerPluginContext context) { Project project = context.getProject(); @@ -45,7 +52,21 @@ public class K2JSCompilerPlugin implements CompilerPlugin { List sources = context.getFiles(); 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 {