From 35ed6827149a0e02f2f136a82cf461d5b3547d5f Mon Sep 17 00:00:00 2001 From: Sergey Mashkov Date: Fri, 15 May 2015 21:21:11 +0300 Subject: [PATCH] Maven: Introduce test-js goal #KT-7757 Fixed Maven K2JS should be able to build tests too --- libraries/examples/js-example/pom.xml | 6 + .../src/test/kotlin/sample/SampleTest.kt | 6 +- libraries/tools/kotlin-maven-plugin/pom.xml | 8 +- .../kotlin/maven/K2JSCompilerMojo.java | 73 ++++++++++-- .../maven/KotlinTestJSCompilerMojo.java | 105 ++++++++++++++++++ 5 files changed, 183 insertions(+), 15 deletions(-) create mode 100644 libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/KotlinTestJSCompilerMojo.java diff --git a/libraries/examples/js-example/pom.xml b/libraries/examples/js-example/pom.xml index bc8fd962a3d..44b53e93837 100644 --- a/libraries/examples/js-example/pom.xml +++ b/libraries/examples/js-example/pom.xml @@ -39,6 +39,12 @@ js + + js tests + + test-js + + diff --git a/libraries/examples/js-example/src/test/kotlin/sample/SampleTest.kt b/libraries/examples/js-example/src/test/kotlin/sample/SampleTest.kt index 6920f00f726..188cf872975 100644 --- a/libraries/examples/js-example/src/test/kotlin/sample/SampleTest.kt +++ b/libraries/examples/js-example/src/test/kotlin/sample/SampleTest.kt @@ -1,9 +1,11 @@ package test.sample +import sample.Hello + import org.junit.Test class SampleTest { Test fun dummy(): Unit { - + Hello().doSomething() } -} \ No newline at end of file +} diff --git a/libraries/tools/kotlin-maven-plugin/pom.xml b/libraries/tools/kotlin-maven-plugin/pom.xml index eb5a0280675..d02b3182d14 100644 --- a/libraries/tools/kotlin-maven-plugin/pom.xml +++ b/libraries/tools/kotlin-maven-plugin/pom.xml @@ -6,7 +6,7 @@ 4.0.0 1.4.1 - 3.0.4 + 3.0.5 @@ -30,6 +30,12 @@ maven-plugin-api ${maven.version} + + org.apache.maven.plugin-tools + maven-plugin-annotations + 3.1 + provided + org.jetbrains.kotlin kotlin-compiler 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 d202b8e715a..a2c8f3f5126 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,8 +16,13 @@ package org.jetbrains.kotlin.maven; +import com.intellij.util.ArrayUtil; import org.apache.maven.artifact.Artifact; import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugins.annotations.LifecyclePhase; +import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; +import org.apache.maven.plugins.annotations.ResolutionScope; import org.jetbrains.annotations.NotNull; import org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments; import org.jetbrains.kotlin.cli.js.K2JSCompiler; @@ -26,52 +31,72 @@ import org.jetbrains.kotlin.utils.LibraryUtils; import java.io.File; import java.util.ArrayList; import java.util.List; +import java.util.Set; +import java.util.concurrent.ConcurrentSkipListSet; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; /** * Converts Kotlin to JavaScript code * - * @goal js - * @phase compile - * @requiresDependencyResolution compile * @noinspection UnusedDeclaration */ +@Mojo(name = "js", defaultPhase = LifecyclePhase.COMPILE, requiresDependencyResolution = ResolutionScope.COMPILE, threadSafe = true) public class K2JSCompilerMojo extends KotlinCompileMojoBase { + private static final String OUTPUT_DIRECTORIES_COLLECTOR_PROPERTY_NAME = "outputDirectoriesCollector"; + private static final Lock lock = new ReentrantLock(); + /** * The output JS file name - * - * @required - * @parameter default-value="${project.build.directory}/js/${project.artifactId}.js" */ + @Parameter(defaultValue = "${project.build.directory}/js/${project.artifactId}.js", required = true) private String outputFile; /** * The output metafile name - * - * @parameter default-value="${project.build.directory}/js/${project.artifactId}.meta.js" */ + @Parameter(defaultValue = "${project.build.directory}/js/${project.artifactId}.meta.js") private String metaFile; - @Override + /** + * Flags enables or disable source map generation + */ + @Parameter(defaultValue = "false") + private boolean sourceMap; + + @Override protected void configureSpecificCompilerArguments(@NotNull K2JSCompilerArguments arguments) throws MojoExecutionException { arguments.outputFile = outputFile; arguments.noStdlib = true; arguments.metaInfo = metaFile; List libraries = getKotlinJavascriptLibraryFiles(); - LOG.info("libraryFiles: " + libraries); - arguments.libraryFiles = libraries.toArray(new String[0]); + LOG.debug("libraryFiles: " + libraries); + arguments.libraryFiles = ArrayUtil.toStringArray(libraries); + + arguments.sourceMap = sourceMap; + + Set collector = getOutputDirectoriesCollector(); + + if (outputFile != null) { + collector.add(new File(outputFile).getParent()); + } + if (metaFile != null) { + collector.add(new File(metaFile).getParent()); + } } /** * 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()) { + for (Artifact artifact : project.getArtifacts()) { if (artifact.getScope().equals(Artifact.SCOPE_COMPILE)) { File file = artifact.getFile(); if (LibraryUtils.isKotlinJavascriptLibrary(file)) { @@ -83,6 +108,15 @@ public class K2JSCompilerMojo extends KotlinCompileMojoBase getOutputDirectoriesCollector() { + lock.lock(); + try { + Set collector = (Set) getPluginContext().get(OUTPUT_DIRECTORIES_COLLECTOR_PROPERTY_NAME); + if (collector == null) { + collector = new ConcurrentSkipListSet(); + getPluginContext().put(OUTPUT_DIRECTORIES_COLLECTOR_PROPERTY_NAME, collector); + } + + return collector; + } finally { + lock.unlock(); + } + } } diff --git a/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/KotlinTestJSCompilerMojo.java b/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/KotlinTestJSCompilerMojo.java new file mode 100644 index 00000000000..91c9697a5e4 --- /dev/null +++ b/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/KotlinTestJSCompilerMojo.java @@ -0,0 +1,105 @@ +/* + * Copyright 2010-2013 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.maven; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.model.Plugin; +import org.apache.maven.model.PluginExecution; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugins.annotations.LifecyclePhase; +import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; +import org.apache.maven.plugins.annotations.ResolutionScope; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments; +import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments; +import org.jetbrains.kotlin.cli.js.K2JSCompiler; +import org.jetbrains.kotlin.utils.LibraryUtils; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +/** + * Converts Kotlin to JavaScript code + * + * @noinspection UnusedDeclaration + */ +@Mojo(name = "test-js", + defaultPhase = LifecyclePhase.TEST_COMPILE, + requiresDependencyResolution = ResolutionScope.TEST +) +public class KotlinTestJSCompilerMojo extends K2JSCompilerMojo { + + /** + * Flag to allow test compilation to be skipped. + */ + @Parameter(property = "maven.test.skip", defaultValue = "false") + private boolean skip; + + /** + * The default source directories containing the sources to be compiled. + */ + @Parameter(defaultValue = "${project.testCompileSourceRoots}", required = true) + private List defaultSourceDirs; + + /** + * The source directories containing the sources to be compiled. + */ + @Parameter + private List sourceDirs; + + @Override + public List getSources() { + if (sourceDirs != null && !sourceDirs.isEmpty()) return sourceDirs; + return defaultSourceDirs; + } + + /** + * The output JS file name + */ + @Parameter(defaultValue = "${project.build.directory}/test-js/${project.artifactId}-tests.js", required = true) + private String outputFile; + + /** + * The output metafile name + */ + @Parameter(defaultValue = "${project.build.directory}/test-js/${project.artifactId}-tests.meta.js") + private String metaFile; + + @Override + protected void configureSpecificCompilerArguments(@NotNull K2JSCompilerArguments arguments) throws MojoExecutionException { + module = testModule; + output = testOutput; + + super.configureSpecificCompilerArguments(arguments); + + arguments.outputFile = outputFile; + arguments.metaInfo = metaFile; + } + + @Override + public void execute() throws MojoExecutionException, MojoFailureException { + if (skip) { + getLog().info("Test compilation is skipped"); + } + else { + super.execute(); + } + } +}