create a standard jar of the kotlin JS source that needs to be compiled when generating JS code so its easy to reuse using regular maven dependencies in build tools; updated the examples to reuse this so that its easy to build JS code without needing to know where on the file system a kotlin checkout lives
This commit is contained in:
@@ -31,9 +31,13 @@
|
|||||||
<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>
|
<dependencies>
|
||||||
<jsLibrarySourceDir>${kotlin-js-lib-srcdir}</jsLibrarySourceDir>
|
<dependency>
|
||||||
</configuration>
|
<groupId>org.jetbrains.kotlin</groupId>
|
||||||
|
<artifactId>kotlin-js-library</artifactId>
|
||||||
|
<version>${project.version}</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
<executions>
|
<executions>
|
||||||
<execution>
|
<execution>
|
||||||
<id>js</id>
|
<id>js</id>
|
||||||
|
|||||||
+8981
File diff suppressed because it is too large
Load Diff
@@ -31,9 +31,13 @@
|
|||||||
<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>
|
<dependencies>
|
||||||
<jsLibrarySourceDir>${kotlin-js-lib-srcdir}</jsLibrarySourceDir>
|
<dependency>
|
||||||
</configuration>
|
<groupId>org.jetbrains.kotlin</groupId>
|
||||||
|
<artifactId>kotlin-js-library</artifactId>
|
||||||
|
<version>${project.version}</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
<executions>
|
<executions>
|
||||||
<execution>
|
<execution>
|
||||||
<id>js</id>
|
<id>js</id>
|
||||||
|
|||||||
+2
-5
@@ -51,6 +51,7 @@
|
|||||||
<module>tools/kotlin-install</module>
|
<module>tools/kotlin-install</module>
|
||||||
<module>tools/runtime</module>
|
<module>tools/runtime</module>
|
||||||
<module>tools/kotlin-maven-plugin</module>
|
<module>tools/kotlin-maven-plugin</module>
|
||||||
|
<module>tools/kotlin-js-library</module>
|
||||||
<module>tools/kdoc</module>
|
<module>tools/kdoc</module>
|
||||||
<module>tools/kdoc-maven-plugin</module>
|
<module>tools/kdoc-maven-plugin</module>
|
||||||
|
|
||||||
@@ -63,12 +64,8 @@
|
|||||||
<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>
|
||||||
-->
|
<module>examples/browser-example</module>
|
||||||
</modules>
|
</modules>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
|||||||
@@ -0,0 +1,39 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
|
||||||
|
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||||
|
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>org.jetbrains.kotlin</groupId>
|
||||||
|
<artifactId>kotlin-project</artifactId>
|
||||||
|
<version>0.1-SNAPSHOT</version>
|
||||||
|
<relativePath>../../pom.xml</relativePath>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<artifactId>kotlin-js-library</artifactId>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.jetbrains.kotlin</groupId>
|
||||||
|
<artifactId>kotlin-maven-plugin</artifactId>
|
||||||
|
<version>${project.version}</version>
|
||||||
|
<configuration>
|
||||||
|
<librarySourceDir>${kotlin-js-lib-srcdir}</librarySourceDir>
|
||||||
|
</configuration>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>jslib</id>
|
||||||
|
<goals>
|
||||||
|
<goal>jslib</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
||||||
+87
@@ -0,0 +1,87 @@
|
|||||||
|
package org.jetbrains.kotlin.maven;
|
||||||
|
|
||||||
|
import com.google.common.io.Files;
|
||||||
|
import org.apache.maven.plugin.AbstractMojo;
|
||||||
|
import org.apache.maven.plugin.MojoExecutionException;
|
||||||
|
import org.apache.maven.plugin.MojoFailureException;
|
||||||
|
import org.codehaus.plexus.util.FileUtils;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileWriter;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.PrintWriter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copies Kotlin source into a JAR to be used when compiling Kotlin to JavaScript
|
||||||
|
*
|
||||||
|
* @goal jslib
|
||||||
|
* @phase compile
|
||||||
|
*/
|
||||||
|
public class JSSourceJarMojo extends AbstractMojo {
|
||||||
|
/**
|
||||||
|
* The Kotlin JavaScript library source code
|
||||||
|
*
|
||||||
|
* @required
|
||||||
|
* @parameter expression="${jsLibrarySourceDir}"
|
||||||
|
*/
|
||||||
|
private File librarySourceDir;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The directory for the copied code
|
||||||
|
*
|
||||||
|
* @parameter default-value="${project.build.outputDirectory}"
|
||||||
|
* @required
|
||||||
|
* @readonly
|
||||||
|
*/
|
||||||
|
public File outputDir;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute() throws MojoExecutionException, MojoFailureException {
|
||||||
|
if (!librarySourceDir.exists()) {
|
||||||
|
getLog().warn("Source directory does not exist: " + librarySourceDir);
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
File metaInfFile = new File(outputDir, "META-INF/services/org.jetbrains.kotlin.js.librarySource");
|
||||||
|
metaInfFile.getParentFile().mkdirs();
|
||||||
|
|
||||||
|
FileUtils.copyDirectoryStructure(librarySourceDir, outputDir);
|
||||||
|
|
||||||
|
// lets copy the standard JS library source too
|
||||||
|
FileUtils.copyFile(new File(librarySourceDir, "../../js.translator/testFiles/kotlin_lib.js"), new File(outputDir, "kotlin-lib.js"));
|
||||||
|
|
||||||
|
// now lets generate the META-INF/services/org.jetbrains.kotlin.js.librarySource file
|
||||||
|
PrintWriter writer = new PrintWriter(new FileWriter(metaInfFile));
|
||||||
|
appendSourceFiles(writer, outputDir.getCanonicalPath(), outputDir);
|
||||||
|
writer.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new MojoFailureException(e.getMessage(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void appendSourceFiles(PrintWriter writer, String rootPath, File currentDir) throws IOException {
|
||||||
|
File[] files = currentDir.listFiles();
|
||||||
|
if (files != null) {
|
||||||
|
for (File file : files) {
|
||||||
|
if (file.isDirectory()) {
|
||||||
|
appendSourceFiles(writer, rootPath, file);
|
||||||
|
} else {
|
||||||
|
String name = file.getName();
|
||||||
|
if (name.toLowerCase().endsWith(".kt")) {
|
||||||
|
// lets get the canonical name after removing the root dir
|
||||||
|
String fullPath = file.getCanonicalPath();
|
||||||
|
if (fullPath.startsWith(rootPath)) {
|
||||||
|
String relativePath = fullPath.substring(rootPath.length());
|
||||||
|
if (relativePath.startsWith("/")) {
|
||||||
|
relativePath = relativePath.substring(1);
|
||||||
|
}
|
||||||
|
writer.println(relativePath);
|
||||||
|
} else {
|
||||||
|
getLog().warn("Could not remove the root path " + rootPath + " from file: " + fullPath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+63
-25
@@ -9,52 +9,90 @@ import org.jetbrains.k2js.config.Config;
|
|||||||
import org.jetbrains.k2js.config.EcmaVersion;
|
import org.jetbrains.k2js.config.EcmaVersion;
|
||||||
import org.jetbrains.k2js.utils.JetFileUtils;
|
import org.jetbrains.k2js.utils.JetFileUtils;
|
||||||
|
|
||||||
import java.io.FileInputStream;
|
import java.io.BufferedReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.util.ArrayList;
|
import java.io.InputStreamReader;
|
||||||
import java.util.List;
|
import java.net.URL;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A Config implementation which is configured with a directory to find the standard library names from
|
* A Config implementation which is configured with a directory to find the standard library names from
|
||||||
*/
|
*/
|
||||||
public class JsLibrarySourceConfig extends Config {
|
public class JsLibrarySourceConfig extends Config {
|
||||||
|
|
||||||
|
public static final String LIBRARY_SOURCES_FILES = "META-INF/services/org.jetbrains.kotlin.js.librarySource";
|
||||||
@Nullable
|
@Nullable
|
||||||
private /*var*/ List<JetFile> jsLibFiles = null;
|
private /*var*/ List<JetFile> jsLibFiles = null;
|
||||||
@NotNull
|
|
||||||
private String librarySourceDir;
|
|
||||||
|
|
||||||
public JsLibrarySourceConfig(@NotNull Project project, @NotNull EcmaVersion version, @NotNull String librarySourceDir) {
|
public JsLibrarySourceConfig(@NotNull Project project, @NotNull EcmaVersion version) {
|
||||||
super(project, version);
|
super(project, version);
|
||||||
this.librarySourceDir = librarySourceDir;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private List<JetFile> initLibFiles(@NotNull Project project) {
|
private List<JetFile> initLibFiles(@NotNull Project project) {
|
||||||
List<JetFile> libFiles = new ArrayList<JetFile>();
|
List<JetFile> libFiles = new ArrayList<JetFile>();
|
||||||
for (String libFileName : LIB_FILE_NAMES) {
|
Set<URL> urlsLoaded = new HashSet<URL>();
|
||||||
JetFile file = null;
|
try {
|
||||||
try {
|
Enumeration<URL> resources = getClass().getClassLoader().getResources(LIBRARY_SOURCES_FILES);
|
||||||
@SuppressWarnings("IOResourceOpenedButNotSafelyClosed")
|
loadLibFiles(resources, project, urlsLoaded, libFiles);
|
||||||
InputStream stream = new FileInputStream(librarySourceDir + libFileName);
|
resources = Thread.currentThread().getContextClassLoader().getResources(LIBRARY_SOURCES_FILES);
|
||||||
try {
|
loadLibFiles(resources, project, urlsLoaded, libFiles);
|
||||||
String text = FileUtil.loadTextAndClose(stream);
|
} catch (IOException e) {
|
||||||
file = JetFileUtils.createPsiFile(libFileName, text, project);
|
throw new IllegalStateException(e);
|
||||||
}
|
|
||||||
catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
libFiles.add(file);
|
|
||||||
}
|
|
||||||
catch (Exception e) {
|
|
||||||
//TODO: throw generic exception
|
|
||||||
throw new IllegalStateException(e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return libFiles;
|
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
|
@Override
|
||||||
@NotNull
|
@NotNull
|
||||||
public List<JetFile> generateLibFiles() {
|
public List<JetFile> generateLibFiles() {
|
||||||
|
|||||||
+1
-9
@@ -35,19 +35,11 @@ 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(jsLibrarySourceDir);
|
K2JSCompilerPlugin plugin = new K2JSCompilerPlugin();
|
||||||
plugin.setOutFile(outFile);
|
plugin.setOutFile(outFile);
|
||||||
arguments.getCompilerPlugins().add(plugin);
|
arguments.getCompilerPlugins().add(plugin);
|
||||||
|
|
||||||
|
|||||||
+22
-19
@@ -17,6 +17,7 @@
|
|||||||
package org.jetbrains.kotlin.maven;
|
package org.jetbrains.kotlin.maven;
|
||||||
|
|
||||||
import com.google.common.io.Files;
|
import com.google.common.io.Files;
|
||||||
|
import com.google.common.io.InputSupplier;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.jet.cli.common.CompilerPlugin;
|
import org.jetbrains.jet.cli.common.CompilerPlugin;
|
||||||
import org.jetbrains.jet.cli.common.CompilerPluginContext;
|
import org.jetbrains.jet.cli.common.CompilerPluginContext;
|
||||||
@@ -30,19 +31,18 @@ import org.jetbrains.k2js.facade.MainCallParameters;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
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;
|
public static final String KOTLIN_JS_LIB = "kotlin-lib.js";
|
||||||
private String outFile = "target/js/program.js";
|
private String outFile = "target/js/program.js";
|
||||||
|
|
||||||
public K2JSCompilerPlugin(String jsLibrarySourceDir) {
|
public K2JSCompilerPlugin() {
|
||||||
this.jsLibrarySourceDir = jsLibrarySourceDir;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -52,24 +52,28 @@ 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 config = new JsLibrarySourceConfig(project, EcmaVersion.defaultVersion());
|
||||||
if (jsLibrarySourceDir != null) {
|
|
||||||
config = new JsLibrarySourceConfig(project, EcmaVersion.defaultVersion(), jsLibrarySourceDir);
|
|
||||||
|
|
||||||
// lets copy the kotlin library into the output directory
|
// lets copy the kotlin library into the output directory
|
||||||
try {
|
try {
|
||||||
File parentFile = new File(outFile).getParentFile();
|
File parentFile = new File(outFile).getParentFile();
|
||||||
parentFile.mkdirs();
|
parentFile.mkdirs();
|
||||||
Files.copy(new File(jsLibrarySourceDir, "../../js.translator/testFiles/kotlin_lib.js"), new File(parentFile, "kotlin-lib.js"));
|
final InputStream inputStream = JsLibrarySourceConfig.loadClasspathResource(KOTLIN_JS_LIB);
|
||||||
} catch (IOException e) {
|
if (inputStream == null) {
|
||||||
throw new RuntimeException(e);
|
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"));
|
||||||
}
|
}
|
||||||
} else {
|
} catch (IOException e) {
|
||||||
config = Config.getEmptyConfig(project);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
K2JSTranslator translator = new K2JSTranslator(config);
|
K2JSTranslator translator = new K2JSTranslator(config);
|
||||||
|
|
||||||
final String code = translator.generateProgramCode(sources, MainCallParameters.noCall());
|
final String code = translator.generateProgramCode(sources, MainCallParameters.noCall());
|
||||||
@@ -77,8 +81,7 @@ public class K2JSCompilerPlugin implements CompilerPlugin {
|
|||||||
File file = new File(outFile);
|
File file = new File(outFile);
|
||||||
Files.createParentDirs(file);
|
Files.createParentDirs(file);
|
||||||
Files.write(code, file, Charset.forName("UTF-8"));
|
Files.write(code, file, Charset.forName("UTF-8"));
|
||||||
}
|
} catch (Exception e) {
|
||||||
catch (Exception e) {
|
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user