diff --git a/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/JSSourceJarMojo.java b/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/JSSourceJarMojo.java deleted file mode 100644 index 8e2b80df083..00000000000 --- a/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/JSSourceJarMojo.java +++ /dev/null @@ -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); - } - } - } - } - } - } -} 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 4fc1bec6f4e..c712f697f7e 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 @@ -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 { - 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 inputSupplier = new InputSupplier() { - @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 inputSupplier = new InputSupplier() { - @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 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 getKotlinJavascriptLibraryFiles() { + List libraries = new ArrayList(); + + 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