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:
+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;
|
||||
|
||||
/**
|
||||
* 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);
|
||||
|
||||
|
||||
+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.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<JetFile> 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 {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user