support multiple source roots

This commit is contained in:
develar
2012-06-22 13:21:49 +04:00
parent 63e41b771f
commit 9443007dc3
4 changed files with 33 additions and 38 deletions
@@ -65,21 +65,15 @@ public class K2JSCompiler extends CLICompiler<K2JSCompilerArguments, K2JSCompile
@NotNull
@Override
protected ExitCode doExecute(K2JSCompilerArguments arguments, PrintingMessageCollector messageCollector, Disposable rootDisposable) {
if (arguments.srcdir == null && arguments.sourceFiles == null) {
messageCollector.report(CompilerMessageSeverity.ERROR, "Specify sources location via -srcdir", NO_LOCATION);
if (arguments.sourceFiles == null) {
messageCollector.report(CompilerMessageSeverity.ERROR, "Specify sources location via -sourceFiles", NO_LOCATION);
return ExitCode.INTERNAL_ERROR;
}
JetCoreEnvironment environmentForJS = JetCoreEnvironment.getCoreEnvironmentForJS(rootDisposable);
if (arguments.srcdir != null) {
environmentForJS.addSources(arguments.srcdir);
}
if (arguments.sourceFiles != null) {
for (String sourceFile : arguments.sourceFiles) {
environmentForJS.addSources(sourceFile);
}
for (String sourceFile : arguments.sourceFiles) {
environmentForJS.addSources(sourceFile);
}
Project project = environmentForJS.getProject();
@@ -21,7 +21,6 @@ import org.jetbrains.jet.cli.common.CompilerArguments;
import org.jetbrains.k2js.facade.MainCallParameters;
import java.util.ArrayList;
import java.util.List;
/**
* @author Pavel Talanov
@@ -29,7 +28,7 @@ import java.util.List;
/**
* NOTE: for now K2JSCompiler supports only minimal amount of parameters required to launch it from the plugin.
* You can specify only one source folder, path to the file where generated file will be stored, path to zipped library sources.
* You can specify path to the file where generated file will be stored, path to zipped library sources.
*/
public class K2JSCompilerArguments extends CompilerArguments {
@Argument(value = "output", description = "Output file path")
@@ -39,8 +38,8 @@ public class K2JSCompilerArguments extends CompilerArguments {
@Argument(value = "libraryFiles", description = "Path to zipped lib sources or kotlin files")
public String[] libraryFiles;
@Argument(value = "srcdir", description = "Sources directory")
public String srcdir;
@Argument(value = "sourceFiles", description = "Source files (dir or file)")
public String[] sourceFiles;
@Argument(value = "target", description = "Generate js files for specific ECMA version (3 or 5, default ECMA 3)")
public String target;
@@ -60,8 +59,6 @@ public class K2JSCompilerArguments extends CompilerArguments {
@Argument(value = "help", alias = "h", description = "Show help")
public boolean help;
public List<String> sourceFiles;
@Override
public boolean isHelp() {
return help;
@@ -84,10 +81,7 @@ public class K2JSCompilerArguments extends CompilerArguments {
@Override
public String getSrc() {
if (sourceFiles != null) {
return sourceFiles.toString();
}
return srcdir;
throw new IllegalStateException();
}
public MainCallParameters createMainCallParameters() {
@@ -117,16 +117,9 @@ public final class K2JSCompiler implements TranslatingCompiler {
@NotNull
private static Integer doExec(@NotNull CompileContext context, @NotNull CompilerEnvironment environment, @NotNull PrintStream out,
@NotNull Module module) throws Exception {
VirtualFile[] roots = ModuleRootManager.getInstance(module).getSourceRoots();
if (roots.length != 1) {
context.addMessage(CompilerMessageCategory.ERROR, "K2JSCompiler does not support multiple module source roots.", null, -1, -1);
return -1;
}
VirtualFile outDir = context.getModuleOutputDirectory(module);
String outFile = outDir == null ? null : outDir.getPath() + "/" + module.getName() + ".js";
String[] commandLineArgs = constructArguments(module, outFile, roots[0]);
String[] commandLineArgs = constructArguments(module, outFile);
Object rc = invokeExecMethod(environment, out, context, commandLineArgs, "org.jetbrains.jet.cli.js.K2JSCompiler");
if (outDir != null && !ApplicationManager.getApplication().isUnitTestMode()) {
@@ -136,15 +129,16 @@ public final class K2JSCompiler implements TranslatingCompiler {
}
@NotNull
private static String[] constructArguments(@NotNull Module module, @Nullable String outFile, @NotNull VirtualFile srcDir) {
private static String[] constructArguments(@NotNull Module module, @Nullable String outFile) {
ArrayList<String> args = Lists.newArrayList("-tags", "-verbose", "-version", "-mainCall", "mainWithArgs");
addPathToSourcesDir(args, srcDir);
addPathToSourcesDir(module, args);
addOutputPath(outFile, args);
addLibLocationAndTarget(module, args);
return ArrayUtil.toStringArray(args);
}
// we cannot use OrderEnumerator because it has critical bug — try https://gist.github.com/2953261, processor will never be called for module dependency
// we don't use context.getCompileScope().getAffectedModules() because we want to know about linkage type (well, we ignore scope right now, but in future...)
private static void collectModuleDependencies(Module dependentModule, Set<Module> modules) {
for (OrderEntry entry : ModuleRootManager.getInstance(dependentModule).getOrderEntries()) {
if (entry instanceof ModuleOrderEntry) {
@@ -165,6 +159,11 @@ public final class K2JSCompiler implements TranslatingCompiler {
}
}
private static VirtualFile[] getSourceFiles(@NotNull Module module) {
return CompilerManager.getInstance(module.getProject()).createModuleCompileScope(module, false)
.getFiles(JetFileType.INSTANCE, true);
}
private static void addLibLocationAndTarget(@NotNull Module module, @NotNull ArrayList<String> args) {
Pair<String[], String> libLocationAndTarget = JsModuleDetector.getLibLocationAndTargetForProject(module);
@@ -176,9 +175,8 @@ public final class K2JSCompiler implements TranslatingCompiler {
if (!modules.isEmpty()) {
for (Module dependency : modules) {
sb.append('@').append(dependency.getName()).append(',');
VirtualFile[] files = CompilerManager.getInstance(module.getProject()).createModuleCompileScope(dependency, false)
.getFiles(JetFileType.INSTANCE, true);
for (VirtualFile file : files) {
for (VirtualFile file : getSourceFiles(dependency)) {
sb.append(file.getPath()).append(',');
}
}
@@ -206,10 +204,19 @@ public final class K2JSCompiler implements TranslatingCompiler {
}
}
private static void addPathToSourcesDir(@NotNull ArrayList<String> args, @NotNull VirtualFile srcDir) {
String srcPath = srcDir.getPath();
args.add("-srcdir");
args.add(srcPath);
private static void addPathToSourcesDir(@NotNull Module module, @NotNull ArrayList<String> args) {
args.add("-sourceFiles");
StringBuilder sb = StringBuilderSpinAllocator.alloc();
try {
for (VirtualFile file : getSourceFiles(module)) {
sb.append(file.getPath()).append(',');
}
args.add(sb.substring(0, sb.length() - 1));
}
finally {
StringBuilderSpinAllocator.dispose(sb);
}
}
private static void addOutputPath(@Nullable String outFile, @NotNull ArrayList<String> args) {
@@ -79,7 +79,7 @@ abstract class StdLibTestSupport extends SingleFileTranslationTest {
K2JSCompiler compiler = new K2JSCompiler();
K2JSCompilerArguments arguments = new K2JSCompilerArguments();
arguments.outputFile = getOutputFilePath(getTestName(false) + ".compiler.kt", version);
arguments.sourceFiles = files;
arguments.sourceFiles = files.toArray(new String[files.size()]);
arguments.verbose = true;
System.out.println("Compiling with version: " + version + " to: " + arguments.outputFile);
ExitCode answer = compiler.exec(System.out, arguments);