JPS plugin: add support external compilation for JS modules.

This commit is contained in:
Zalim Bashorov
2013-10-03 12:47:47 +04:00
parent cfb7104ae9
commit 2b9b563efb
6 changed files with 258 additions and 38 deletions
@@ -24,8 +24,12 @@ import com.intellij.openapi.projectRoots.JdkUtil;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.projectRoots.SimpleJavaSdkType;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.ArrayUtil;
import com.intellij.util.Function;
import com.intellij.util.StringBuilderSpinAllocator;
import com.intellij.util.SystemProperties;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.cli.common.messages.CompilerMessageLocation;
import org.jetbrains.jet.cli.common.messages.CompilerMessageSeverity;
import org.jetbrains.jet.cli.common.messages.MessageCollector;
@@ -35,40 +39,57 @@ import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.*;
public class KotlinCompilerRunner {
public static void runCompiler(
private static final String K2JVM_COMPILER = "org.jetbrains.jet.cli.jvm.K2JVMCompiler";
private static final String K2JS_COMPILER = "org.jetbrains.jet.cli.js.K2JSCompiler";
public static void runK2JvmCompiler(
MessageCollector messageCollector,
CompilerEnvironment environment,
File moduleFile,
OutputItemsCollector collector,
boolean runOutOfProcess
) {
String[] arguments = createArgumentsForJvmCompiler(environment.getOutput(), moduleFile);
if (runOutOfProcess) {
runOutOfProcess(messageCollector, collector, environment, moduleFile);
runOutOfProcess(K2JVM_COMPILER, arguments, messageCollector, collector, environment);
}
else {
runInProcess(messageCollector, collector, environment, moduleFile);
runInProcess(K2JVM_COMPILER, arguments, messageCollector, collector, environment);
}
}
private static void runInProcess(final MessageCollector messageCollector,
public static void runK2JsCompiler(
MessageCollector messageCollector,
CompilerEnvironment environment,
OutputItemsCollector collector,
final CompilerEnvironment environment,
final File moduleFile) {
List<File> sourceFiles,
Set<String> libraryFiles,
File outputFile
) {
String[] arguments = createArgumentsForJsCompiler(outputFile, sourceFiles, libraryFiles);
runInProcess(K2JS_COMPILER, arguments, messageCollector, collector, environment);
}
private static void runInProcess(
final String compilerClassName,
final String[] arguments,
final MessageCollector messageCollector,
OutputItemsCollector collector,
final CompilerEnvironment environment) {
CompilerRunnerUtil.outputCompilerMessagesAndHandleExitCode(messageCollector, collector, new Function<PrintStream, Integer>() {
@Override
public Integer fun(PrintStream stream) {
return execInProcess(environment, moduleFile, stream, messageCollector);
return execInProcess(compilerClassName, arguments, environment, stream, messageCollector);
}
});
}
private static int execInProcess(CompilerEnvironment environment, File moduleFile, PrintStream out, MessageCollector messageCollector) {
private static int execInProcess(String compilerClassName, String[] arguments, CompilerEnvironment environment, PrintStream out, MessageCollector messageCollector) {
try {
String compilerClassName = "org.jetbrains.jet.cli.jvm.K2JVMCompiler";
String[] arguments = commandLineArguments(environment.getOutput(), moduleFile);
messageCollector.report(CompilerMessageSeverity.INFO,
"Using kotlinHome=" + environment.getKotlinPaths().getHomePath(),
CompilerMessageLocation.NO_LOCATION);
@@ -87,7 +108,7 @@ public class KotlinCompilerRunner {
}
}
private static String[] commandLineArguments(File outputDir, File moduleFile) {
private static String[] createArgumentsForJvmCompiler(File outputDir, File moduleFile) {
return new String[]{
"-module", moduleFile.getAbsolutePath(),
"-output", outputDir.getPath(),
@@ -96,17 +117,53 @@ public class KotlinCompilerRunner {
"-noStdlib", "-noJdkAnnotations", "-noJdk"};
}
private static String[] createArgumentsForJsCompiler(
File outputFile,
List<File> sourceFiles,
Set<String> libraryFiles
) {
List<String> args = new ArrayList<String>();
Collections.addAll(args, "-tags", "-verbose", "-version", "-sourcemap");
args.add("-sourceFiles");
args.add(convertSourceFilesListToString(sourceFiles));
args.add("-output");
args.add(outputFile.getPath());
args.add("-libraryFiles");
args.add(StringUtil.join(libraryFiles, ","));
return ArrayUtil.toStringArray(args);
}
@NotNull
private static String convertSourceFilesListToString(@NotNull List<File> sourceFiles) {
StringBuilder sb = StringBuilderSpinAllocator.alloc();
for (File file : sourceFiles) {
sb.append(file.getPath()).append(',');
}
String result = sb.substring(0, sb.length() - 1);
StringBuilderSpinAllocator.dispose(sb);
return result;
}
private static void runOutOfProcess(
String compilerClassName,
String[] arguments,
final MessageCollector messageCollector,
final OutputItemsCollector itemCollector,
CompilerEnvironment environment,
File moduleFile
CompilerEnvironment environment
) {
SimpleJavaParameters params = new SimpleJavaParameters();
params.setJdk(new SimpleJavaSdkType().createJdk("tmp", SystemProperties.getJavaHome()));
params.setMainClass("org.jetbrains.jet.cli.jvm.K2JVMCompiler");
params.setMainClass(compilerClassName);
for (String arg : commandLineArguments(environment.getOutput(), moduleFile)) {
for (String arg : arguments) {
params.getProgramParametersList().add(arg);
}