Add new JS source map config options to Maven plugin

This commit is contained in:
Alexey Andreev
2017-06-13 15:05:10 +03:00
parent cfd3b137d8
commit 49b742ab3d
13 changed files with 123 additions and 27 deletions
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>test-js-sourceMap</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-js</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<plugins>
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<goals>
<goal>js</goal>
</goals>
</execution>
</executions>
<configuration>
<sourceMap>true</sourceMap>
<sourceMapPrefix>prefixprefix/</sourceMapPrefix>
</configuration>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,19 @@
/*
* Copyright 2010-2017 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
fun bar() = "OK"
@@ -0,0 +1,3 @@
source(new File(basedir, "../../../verify-common.bsh").getAbsolutePath());
assertFileContains("target/js/test-js-sourceMap.js.map", "\"prefixprefix/org/jetbrains/HelloWorld.kt\"")
@@ -1,6 +1,5 @@
String[] getBuildLogLines() {
File buildLog = new File(basedir, "build.log");
BufferedReader reader = new BufferedReader(new FileReader(buildLog));
String[] getFileLines(File file) {
BufferedReader reader = new BufferedReader(new FileReader(file));
List result = new ArrayList();
String line;
while ((line = reader.readLine()) != null){
@@ -11,8 +10,24 @@ String[] getBuildLogLines() {
return result.toArray(new String[0]);
}
String[] getBuildLogLines() {
return getFileLines(new File(basedir, "build.log"));
}
String[] buildLog = getBuildLogLines();
void assertFileContains(String relativePath, String content) {
File file = new File(basedir, relativePath);
if (!file.exists()) {
throw new Exception("File was not found: " + relativePath);
}
String[] lines = getFileLines(file);
int lineNumber = findLineThatContains(lines, content);
if (lineNumber < 0) {
throw new Exception("Expected file " + relativePath + " to contain: \"" + content + "\"");
}
}
void assertBuildLogHasLine(String expectedLine) {
for (int i = 0; i < buildLog.length; i++) {
@@ -40,8 +55,12 @@ void assertBuildLogHasNoLineThatContains(String content) {
}
int findBuildLogLineThatContains(String content) {
for (int i = 0; i < buildLog.length; i++) {
String line = buildLog[i];
return findLineThatContains(buildLog, content);
}
int findLineThatContains(String[] lines, String content) {
for (int i = 0; i < lines.length; i++) {
String line = lines[i];
if (line.contains(content)) {
return i;
}
@@ -17,7 +17,6 @@
package org.jetbrains.kotlin.maven;
import com.intellij.openapi.util.text.StringUtil;
import kotlin.text.StringsKt;
import org.apache.maven.artifact.DependencyResolutionRequiredException;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
@@ -29,16 +28,13 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments;
import org.jetbrains.kotlin.cli.js.K2JSCompiler;
import org.jetbrains.kotlin.utils.LibraryUtils;
import org.jetbrains.kotlin.utils.KotlinJavascriptMetadataUtils;
import org.jetbrains.kotlin.js.JavaScript;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
@@ -71,6 +67,9 @@ public class K2JSCompilerMojo extends KotlinCompileMojoBase<K2JSCompilerArgument
@Parameter(defaultValue = "false")
private boolean sourceMap;
@Parameter
private String sourceMapPrefix;
/**
* Main invocation behaviour. Possible values are <b>call</b> and <b>noCall</b>.
*/
@@ -93,7 +92,7 @@ public class K2JSCompilerMojo extends KotlinCompileMojoBase<K2JSCompilerArgument
private String moduleKind;
@Override
protected void configureSpecificCompilerArguments(@NotNull K2JSCompilerArguments arguments) throws MojoExecutionException {
protected void configureSpecificCompilerArguments(@NotNull K2JSCompilerArguments arguments, @NotNull List<File> sourceRoots) throws MojoExecutionException {
arguments.outputFile = outputFile;
arguments.noStdlib = true;
arguments.metaInfo = metaInfo;
@@ -110,6 +109,7 @@ public class K2JSCompilerMojo extends KotlinCompileMojoBase<K2JSCompilerArgument
arguments.libraries = StringUtil.join(libraries, File.pathSeparator);
arguments.sourceMap = sourceMap;
arguments.sourceMapPrefix = sourceMapPrefix;
if (outputFile != null) {
ConcurrentMap<String, List<String>> collector = getOutputDirectoriesCollector();
@@ -117,6 +117,17 @@ public class K2JSCompilerMojo extends KotlinCompileMojoBase<K2JSCompilerArgument
List<String> paths = collector.computeIfAbsent(key, k -> Collections.synchronizedList(new ArrayList<String>()));
paths.add(new File(outputFile).getParent());
}
StringBuilder sourceMapSourceRoots = new StringBuilder();
if (!sourceRoots.isEmpty()) {
sourceMapSourceRoots.append(sourceRoots.get(0).getAbsolutePath());
for (int i = 1; i < sourceRoots.size(); ++i) {
sourceMapSourceRoots.append(File.pathSeparator);
sourceMapSourceRoots.append(sourceRoots.get(i).getAbsolutePath());
}
}
arguments.sourceMapSourceRoots = sourceMapSourceRoots.toString();
}
protected List<String> getClassPathElements() throws DependencyResolutionRequiredException {
@@ -132,7 +132,7 @@ public class K2JVMCompileMojo extends KotlinCompileMojoBase<K2JVMCompilerArgumen
}
@Override
protected void configureSpecificCompilerArguments(@NotNull K2JVMCompilerArguments arguments) throws MojoExecutionException {
protected void configureSpecificCompilerArguments(@NotNull K2JVMCompilerArguments arguments, @NotNull List<File> sourceRoots) throws MojoExecutionException {
arguments.destination = output;
// don't include runtime, it should be in maven dependencies
@@ -211,7 +211,7 @@ public abstract class KotlinCompileMojoBase<A extends CommonCompilerArguments> e
List<File> sourceRoots = getSourceRoots();
configureCompilerArguments(arguments, compiler);
configureCompilerArguments(arguments, compiler, sourceRoots);
printCompilerArgumentsIfDebugEnabled(arguments, compiler);
MavenPluginLogMessageCollector messageCollector = new MavenPluginLogMessageCollector(getLog());
@@ -290,7 +290,7 @@ public abstract class KotlinCompileMojoBase<A extends CommonCompilerArguments> e
@NotNull
protected abstract A createCompilerArguments();
protected abstract void configureSpecificCompilerArguments(@NotNull A arguments) throws MojoExecutionException;
protected abstract void configureSpecificCompilerArguments(@NotNull A arguments, @NotNull List<File> sourceRoots) throws MojoExecutionException;
private List<String> getCompilerPluginClassPaths() {
ArrayList<String> result = new ArrayList<String>();
@@ -451,7 +451,7 @@ public abstract class KotlinCompileMojoBase<A extends CommonCompilerArguments> e
return sourceRoots;
}
private void configureCompilerArguments(@NotNull A arguments, @NotNull CLICompiler<A> compiler) throws MojoExecutionException {
private void configureCompilerArguments(@NotNull A arguments, @NotNull CLICompiler<A> compiler, @NotNull List<File> sourceRoots) throws MojoExecutionException {
if (getLog().isDebugEnabled()) {
arguments.verbose = true;
}
@@ -465,7 +465,7 @@ public abstract class KotlinCompileMojoBase<A extends CommonCompilerArguments> e
arguments.coroutinesState = experimentalCoroutines;
}
configureSpecificCompilerArguments(arguments);
configureSpecificCompilerArguments(arguments, sourceRoots);
try {
compiler.parseArguments(ArrayUtil.toStringArray(args), arguments);
@@ -28,6 +28,7 @@ import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments;
import org.jetbrains.kotlin.maven.kapt.AnnotationProcessingManager;
import org.jetbrains.kotlin.maven.kapt.KaptTestJvmCompilerMojo;
import java.io.File;
import java.util.List;
/**
@@ -90,11 +91,11 @@ public class KotlinTestCompileMojo extends K2JVMCompileMojo {
}
@Override
protected void configureSpecificCompilerArguments(@NotNull K2JVMCompilerArguments arguments) throws MojoExecutionException {
protected void configureSpecificCompilerArguments(@NotNull K2JVMCompilerArguments arguments, @NotNull List<File> sourceRoots) throws MojoExecutionException {
classpath = testClasspath;
arguments.friendPaths = new String[] { output };
output = testOutput;
super.configureSpecificCompilerArguments(arguments);
super.configureSpecificCompilerArguments(arguments, sourceRoots);
}
@Override
@@ -80,12 +80,12 @@ public class KotlinTestJSCompilerMojo extends K2JSCompilerMojo {
private boolean metaInfo;
@Override
protected void configureSpecificCompilerArguments(@NotNull K2JSCompilerArguments arguments) throws MojoExecutionException {
protected void configureSpecificCompilerArguments(@NotNull K2JSCompilerArguments arguments, @NotNull List<File> sourceRoots) throws MojoExecutionException {
List<String> friends = getOutputDirectoriesCollector().getOrDefault(project.getArtifactId(), Collections.emptyList());
arguments.friendModules = StringUtil.join(friends, File.pathSeparator);
output = testOutput;
super.configureSpecificCompilerArguments(arguments);
super.configureSpecificCompilerArguments(arguments, sourceRoots);
arguments.outputFile = outputFile;
arguments.metaInfo = metaInfo;
@@ -44,7 +44,7 @@ public class MetadataMojo extends KotlinCompileMojoBase<K2MetadataCompilerArgume
}
@Override
protected void configureSpecificCompilerArguments(@NotNull K2MetadataCompilerArguments arguments) throws MojoExecutionException {
protected void configureSpecificCompilerArguments(@NotNull K2MetadataCompilerArguments arguments, @NotNull List<File> sourceRoots) throws MojoExecutionException {
arguments.destination = output;
if (!arguments.multiPlatform) {
getLog().info("multiPlatform forced for metadata generation");
@@ -25,13 +25,13 @@ public class TestMetadataMojo extends MetadataMojo {
}
@Override
protected void configureSpecificCompilerArguments(@NotNull K2MetadataCompilerArguments arguments) throws MojoExecutionException {
protected void configureSpecificCompilerArguments(@NotNull K2MetadataCompilerArguments arguments, @NotNull List<File> sourceRoots) throws MojoExecutionException {
String productionOutput = output;
classpath = testClasspath;
// arguments.friendPaths = new String[] { productionOutput };
output = testOutput;
super.configureSpecificCompilerArguments(arguments);
super.configureSpecificCompilerArguments(arguments, sourceRoots);
if (arguments.classpath == null) {
arguments.classpath = productionOutput;
@@ -106,8 +106,8 @@ public class KaptJVMCompilerMojo extends K2JVMCompileMojo {
}
@Override
protected void configureSpecificCompilerArguments(@NotNull K2JVMCompilerArguments arguments) throws MojoExecutionException {
super.configureSpecificCompilerArguments(arguments);
protected void configureSpecificCompilerArguments(@NotNull K2JVMCompilerArguments arguments, @NotNull List<File> sourceRoots) throws MojoExecutionException {
super.configureSpecificCompilerArguments(arguments, sourceRoots);
AnnotationProcessingManager.ResolvedArtifacts resolvedArtifacts;
@@ -10,6 +10,7 @@ import org.apache.maven.project.MavenProject;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments;
import java.io.File;
import java.util.List;
/** Note! This file was majorly copied from {@link org.jetbrains.kotlin.maven.KotlinTestCompileMojo}.
@@ -64,11 +65,11 @@ public class KaptTestJvmCompilerMojo extends KaptJVMCompilerMojo {
}
@Override
protected void configureSpecificCompilerArguments(@NotNull K2JVMCompilerArguments arguments) throws MojoExecutionException {
protected void configureSpecificCompilerArguments(@NotNull K2JVMCompilerArguments arguments, @NotNull List<File> sourceRoots) throws MojoExecutionException {
classpath = testClasspath;
arguments.friendPaths = new String[] { output };
output = testOutput;
super.configureSpecificCompilerArguments(arguments);
super.configureSpecificCompilerArguments(arguments, sourceRoots);
}
@Override