JS backend: maven: fix compiler mojo
This commit is contained in:
-112
@@ -1,112 +0,0 @@
|
||||
package org.jetbrains.kotlin.maven;
|
||||
|
||||
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 org.jetbrains.k2js.config.ClassPathLibraryDefintionsConfig;
|
||||
import org.jetbrains.k2js.config.ClassPathLibrarySourcesLoader;
|
||||
|
||||
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; the library code to be compiled to JavaScript
|
||||
*
|
||||
* @parameter expression="${jsLibrarySourceDir}"
|
||||
*/
|
||||
private File librarySourceDir;
|
||||
|
||||
/**
|
||||
* The Kotlin JavaScript definition source code; the kotlin code used to define APIs available in the kotlin-lib.js file
|
||||
*
|
||||
* @parameter expression="${jsDefinitionSourceDir}"
|
||||
*/
|
||||
private File definitionSourceDir;
|
||||
|
||||
/**
|
||||
* 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 != null) {
|
||||
if (!librarySourceDir.exists()) {
|
||||
getLog().warn("Source directory does not exist: " + librarySourceDir);
|
||||
} else {
|
||||
try {
|
||||
File metaInfFile = new File(outputDir, ClassPathLibrarySourcesLoader.META_INF_SERVICES_FILE);
|
||||
metaInfFile.getParentFile().mkdirs();
|
||||
|
||||
FileUtils.copyDirectoryStructure(librarySourceDir, outputDir);
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (definitionSourceDir != null) {
|
||||
if (!definitionSourceDir.exists()) {
|
||||
getLog().warn("Definition directory does not exist: " + definitionSourceDir);
|
||||
} else {
|
||||
try {
|
||||
File metaInfFile = new File(outputDir, ClassPathLibraryDefintionsConfig.META_INF_SERVICES_FILE);
|
||||
metaInfFile.getParentFile().mkdirs();
|
||||
|
||||
FileUtils.copyDirectoryStructure(definitionSourceDir, outputDir);
|
||||
|
||||
// now lets generate the META-INF/services/org.jetbrains.kotlin.js.libraryDefinitions file
|
||||
PrintWriter writer = new PrintWriter(new FileWriter(metaInfFile));
|
||||
appendSourceFiles(writer, definitionSourceDir.getCanonicalPath(), definitionSourceDir);
|
||||
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.startsWith(File.separator)) {
|
||||
relativePath = relativePath.substring(1).replace(File.separatorChar, '/');
|
||||
}
|
||||
writer.println(relativePath);
|
||||
} else {
|
||||
getLog().warn("Could not remove the root path " + rootPath + " from file: " + fullPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+33
-113
@@ -16,33 +16,26 @@
|
||||
|
||||
package org.jetbrains.kotlin.maven;
|
||||
|
||||
import com.google.common.io.Files;
|
||||
import com.google.common.io.InputSupplier;
|
||||
import com.intellij.openapi.util.io.FileUtil;
|
||||
import org.apache.maven.artifact.Artifact;
|
||||
import org.apache.maven.plugin.MojoExecutionException;
|
||||
import org.apache.maven.plugin.MojoFailureException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.cli.common.arguments.K2JSCompilerArguments;
|
||||
import org.jetbrains.jet.cli.js.K2JSCompiler;
|
||||
import org.jetbrains.k2js.config.MetaInfServices;
|
||||
import org.jetbrains.jet.utils.LibraryUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Converts Kotlin to JavaScript code
|
||||
*
|
||||
* @goal js
|
||||
* @phase compile
|
||||
* @requiresDependencyResolution compile
|
||||
* @noinspection UnusedDeclaration
|
||||
*/
|
||||
public class K2JSCompilerMojo extends KotlinCompileMojoBase<K2JSCompilerArguments> {
|
||||
public static final String KOTLIN_JS_MAPS = "kotlin-maps.js";
|
||||
public static final String KOTLIN_JS_LONG = "kotlin-long.js";
|
||||
public static final String KOTLIN_JS_LIB = "kotlin-lib.js";
|
||||
public static final String KOTLIN_JS_LIB_ECMA5 = "kotlin-lib-ecma5.js";
|
||||
|
||||
/**
|
||||
* The output JS file name
|
||||
@@ -52,110 +45,37 @@ public class K2JSCompilerMojo extends KotlinCompileMojoBase<K2JSCompilerArgument
|
||||
*/
|
||||
private String outputFile;
|
||||
|
||||
/**
|
||||
* The output Kotlin JS file
|
||||
*
|
||||
* @required
|
||||
* @parameter default-value="${project.build.directory}/js"
|
||||
* @parameter expression="${outputKotlinJSFile}"
|
||||
*/
|
||||
private File outputKotlinJSDir;
|
||||
|
||||
/**
|
||||
* Whether to copy the kotlin-lib.js file to the output directory
|
||||
*
|
||||
* @parameter default-value="true"
|
||||
* @parameter expression="${copyLibraryJS}"
|
||||
*/
|
||||
private Boolean copyLibraryJS;
|
||||
|
||||
/**
|
||||
* Whether to copy the kotlin-lib.js file to the output directory
|
||||
*
|
||||
* @parameter default-value="false"
|
||||
* @parameter expression="${appendLibraryJS}"
|
||||
*/
|
||||
private Boolean appendLibraryJS;
|
||||
|
||||
@Override
|
||||
public void execute() throws MojoExecutionException, MojoFailureException {
|
||||
super.execute();
|
||||
|
||||
if (appendLibraryJS != null && appendLibraryJS) {
|
||||
try {
|
||||
Charset charset = Charset.defaultCharset();
|
||||
File file = new File(outputFile);
|
||||
String text = Files.toString(file, charset);
|
||||
StringBuilder builder = new StringBuilder();
|
||||
appendFile(KOTLIN_JS_LIB_ECMA5, builder);
|
||||
appendFile(KOTLIN_JS_LIB, builder);
|
||||
appendFile(KOTLIN_JS_MAPS, builder);
|
||||
appendFile(KOTLIN_JS_LONG, builder);
|
||||
builder.append("\n");
|
||||
builder.append(text);
|
||||
Files.write(builder.toString(), file, charset);
|
||||
} catch (IOException e) {
|
||||
throw new MojoExecutionException(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
if (copyLibraryJS != null && copyLibraryJS) {
|
||||
LOG.info("Copying kotlin JS library to " + outputKotlinJSDir);
|
||||
|
||||
copyJsLibraryFile(KOTLIN_JS_MAPS);
|
||||
copyJsLibraryFile(KOTLIN_JS_LONG);
|
||||
copyJsLibraryFile(KOTLIN_JS_LIB);
|
||||
copyJsLibraryFile(KOTLIN_JS_LIB_ECMA5);
|
||||
}
|
||||
}
|
||||
|
||||
private void appendFile(String jsLib, StringBuilder builder) throws MojoExecutionException {
|
||||
// lets copy the kotlin library into the output directory
|
||||
try {
|
||||
final InputStream inputStream = MetaInfServices.loadClasspathResource(jsLib);
|
||||
if (inputStream == null) {
|
||||
LOG.warn("Could not find " + jsLib + " on the classpath!");
|
||||
} else {
|
||||
InputSupplier<InputStream> inputSupplier = new InputSupplier<InputStream>() {
|
||||
@Override
|
||||
public InputStream getInput() throws IOException {
|
||||
return inputStream;
|
||||
}
|
||||
};
|
||||
String text = "\n" + FileUtil.loadTextAndClose(inputStream);
|
||||
builder.append(text);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new MojoExecutionException(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
private void copyJsLibraryFile(String jsLib) throws MojoExecutionException {
|
||||
try {
|
||||
final InputStream inputStream = MetaInfServices.loadClasspathResource(jsLib);
|
||||
if (inputStream == null) {
|
||||
LOG.warn("Could not find " + jsLib + " on the classpath!");
|
||||
} else {
|
||||
if (!outputKotlinJSDir.exists() && !outputKotlinJSDir.mkdirs()) {
|
||||
throw new MojoExecutionException("Could not create output directory '" + outputKotlinJSDir + "'.");
|
||||
}
|
||||
|
||||
InputSupplier<InputStream> inputSupplier = new InputSupplier<InputStream>() {
|
||||
@Override
|
||||
public InputStream getInput() throws IOException {
|
||||
return inputStream;
|
||||
}
|
||||
};
|
||||
Files.copy(inputSupplier, new File(outputKotlinJSDir, jsLib));
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new MojoExecutionException(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@Override
|
||||
protected void configureSpecificCompilerArguments(@NotNull K2JSCompilerArguments arguments) throws MojoExecutionException {
|
||||
arguments.outputFile = outputFile;
|
||||
arguments.noStdlib = true;
|
||||
|
||||
List<String> libraries = getKotlinJavascriptLibraryFiles();
|
||||
LOG.info("libraryFiles: " + libraries);
|
||||
arguments.libraryFiles = libraries.toArray(new String[0]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all Kotlin Javascript dependencies that this project has, including transitive ones.
|
||||
* @return array of paths to kotlin javascript libraries
|
||||
*/
|
||||
@NotNull
|
||||
private List<String> getKotlinJavascriptLibraryFiles() {
|
||||
List<String> libraries = new ArrayList<String>();
|
||||
|
||||
for(Artifact artifact : project.getArtifacts()) {
|
||||
if (artifact.getScope().equals(Artifact.SCOPE_COMPILE)) {
|
||||
File file = artifact.getFile();
|
||||
if (LibraryUtils.isKotlinJavascriptLibrary(file)) {
|
||||
libraries.add(file.getAbsolutePath());
|
||||
}
|
||||
else {
|
||||
LOG.warn("artifact " + artifact + " is not a Kotlin Javascript Library");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return libraries;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
|
||||
Reference in New Issue
Block a user