From 4926dd7189791d57dc00e1faeb2a4ebd86acddcf Mon Sep 17 00:00:00 2001 From: Andrey Breslav Date: Thu, 8 Nov 2012 20:47:18 +0400 Subject: [PATCH] Code that runs the compiler is migrated onto MessageCollector CompilerEnvironment migrated to use io.Files instead of VirtualFiles This prepares the move of this code to the separate module, to be re-used by external build --- idea/idea.iml | 1 + .../plugin/compiler/CompilerEnvironment.java | 27 +++--- .../jet/plugin/compiler/CompilerUtils.java | 89 +++++++++++-------- .../jet/plugin/compiler/JetCompiler.java | 89 ++++++++++++------- .../jet/plugin/compiler/K2JSCompiler.java | 41 +++++---- .../compiler/MessageCollectorAdapter.java | 63 +++++++++++++ 6 files changed, 210 insertions(+), 100 deletions(-) create mode 100644 idea/src/org/jetbrains/jet/plugin/compiler/MessageCollectorAdapter.java diff --git a/idea/idea.iml b/idea/idea.iml index 423f3f24465..b015dd10620 100644 --- a/idea/idea.iml +++ b/idea/idea.iml @@ -30,6 +30,7 @@ + diff --git a/idea/src/org/jetbrains/jet/plugin/compiler/CompilerEnvironment.java b/idea/src/org/jetbrains/jet/plugin/compiler/CompilerEnvironment.java index a8b00bf467b..cb6faad0bec 100644 --- a/idea/src/org/jetbrains/jet/plugin/compiler/CompilerEnvironment.java +++ b/idea/src/org/jetbrains/jet/plugin/compiler/CompilerEnvironment.java @@ -16,17 +16,15 @@ package org.jetbrains.jet.plugin.compiler; -import com.intellij.openapi.compiler.CompileContext; -import com.intellij.openapi.module.Module; -import com.intellij.openapi.vfs.VirtualFile; -import jet.Function1; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.jetbrains.jet.cli.common.messages.MessageCollector; import org.jetbrains.jet.utils.PathUtil; -import java.io.*; +import java.io.File; -import static com.intellij.openapi.compiler.CompilerMessageCategory.ERROR; +import static org.jetbrains.jet.cli.common.messages.CompilerMessageLocation.NO_LOCATION; +import static org.jetbrains.jet.cli.common.messages.CompilerMessageSeverity.ERROR; /** * @author Pavel Talanov @@ -34,9 +32,8 @@ import static com.intellij.openapi.compiler.CompilerMessageCategory.ERROR; public final class CompilerEnvironment { @NotNull - public static CompilerEnvironment getEnvironmentFor(@NotNull CompileContext compileContext, @NotNull Module module, boolean tests) { - VirtualFile mainOutput = compileContext.getModuleOutputDirectory(module); - final VirtualFile outputDir = tests ? compileContext.getModuleOutputDirectoryForTests(module) : mainOutput; + public static CompilerEnvironment getEnvironmentFor(boolean tests, File mainOutput, File outputDirectoryForTests) { + final File outputDir = tests ? outputDirectoryForTests : mainOutput; File kotlinHome = PathUtil.getDefaultCompilerPath(); return new CompilerEnvironment(kotlinHome, outputDir); } @@ -44,9 +41,9 @@ public final class CompilerEnvironment { @Nullable private final File kotlinHome; @Nullable - private final VirtualFile output; + private final File output; - public CompilerEnvironment(@Nullable File home, @Nullable VirtualFile output) { + public CompilerEnvironment(@Nullable File home, @Nullable File output) { this.kotlinHome = home; this.output = output; } @@ -62,17 +59,17 @@ public final class CompilerEnvironment { } @NotNull - public VirtualFile getOutput() { + public File getOutput() { assert output != null; return output; } - public void reportErrorsTo(@NotNull CompileContext compileContext) { + public void reportErrorsTo(@NotNull MessageCollector messageCollector) { if (output == null) { - compileContext.addMessage(ERROR, "[Internal Error] No output directory", "", -1, -1); + messageCollector.report(ERROR, "[Internal Error] No output directory", NO_LOCATION); } if (kotlinHome == null) { - compileContext.addMessage(ERROR, "Cannot find kotlinc home. Make sure plugin is properly installed", "", -1, -1); + messageCollector.report(ERROR, "Cannot find kotlinc home. Make sure plugin is properly installed", NO_LOCATION); } } diff --git a/idea/src/org/jetbrains/jet/plugin/compiler/CompilerUtils.java b/idea/src/org/jetbrains/jet/plugin/compiler/CompilerUtils.java index e3e5a24cea0..f76c5deb80c 100644 --- a/idea/src/org/jetbrains/jet/plugin/compiler/CompilerUtils.java +++ b/idea/src/org/jetbrains/jet/plugin/compiler/CompilerUtils.java @@ -19,15 +19,18 @@ package org.jetbrains.jet.plugin.compiler; import com.google.common.collect.ImmutableMap; import com.intellij.compiler.impl.javaCompiler.OutputItemImpl; import com.intellij.openapi.compiler.CompileContext; -import com.intellij.openapi.compiler.CompilerMessageCategory; import com.intellij.openapi.compiler.TranslatingCompiler; import com.intellij.openapi.diagnostic.Logger; +import com.intellij.openapi.module.Module; import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.vfs.LocalFileSystem; import com.intellij.openapi.vfs.VirtualFile; import jet.Function1; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.jetbrains.jet.cli.common.messages.CompilerMessageLocation; +import org.jetbrains.jet.cli.common.messages.CompilerMessageSeverity; +import org.jetbrains.jet.cli.common.messages.MessageCollector; import org.xml.sax.Attributes; import org.xml.sax.InputSource; import org.xml.sax.SAXException; @@ -44,7 +47,8 @@ import java.net.URL; import java.net.URLClassLoader; import java.util.*; -import static com.intellij.openapi.compiler.CompilerMessageCategory.*; +import static org.jetbrains.jet.cli.common.messages.CompilerMessageLocation.NO_LOCATION; +import static org.jetbrains.jet.cli.common.messages.CompilerMessageSeverity.*; /** * @author Pavel Talanov @@ -56,12 +60,11 @@ public final class CompilerUtils { private CompilerUtils() { } - public static List kompilerClasspath(File kotlinHome, CompileContext context) { + public static List kompilerClasspath(File kotlinHome, MessageCollector messageCollector) { File libs = new File(kotlinHome, "lib"); if (!libs.exists() || libs.isFile()) { - context.addMessage(ERROR, "Broken compiler at '" + libs.getAbsolutePath() + "'. Make sure plugin is properly installed", "", -1, - -1); + messageCollector.report(ERROR, "Broken compiler at '" + libs.getAbsolutePath() + "'. Make sure plugin is properly installed", NO_LOCATION); return Collections.emptyList(); } @@ -70,17 +73,17 @@ public final class CompilerUtils { return answer; } - public static URLClassLoader getOrCreateClassLoader(File kotlinHome, CompileContext context) { + public static URLClassLoader getOrCreateClassLoader(File kotlinHome, MessageCollector messageCollector) { URLClassLoader answer = ourClassLoaderRef.get(); if (answer == null) { - answer = createClassloader(kotlinHome, context); + answer = createClassloader(kotlinHome, messageCollector); ourClassLoaderRef = new SoftReference(answer); } return answer; } - private static URLClassLoader createClassloader(File kotlinHome, CompileContext context) { - List jars = kompilerClasspath(kotlinHome, context); + private static URLClassLoader createClassloader(File kotlinHome, MessageCollector messageCollector) { + List jars = kompilerClasspath(kotlinHome, messageCollector); URL[] urls = new URL[jars.size()]; for (int i = 0; i < urls.length; i++) { try { @@ -93,13 +96,13 @@ public final class CompilerUtils { return new URLClassLoader(urls, null); } - static void handleProcessTermination(int exitCode, CompileContext compileContext) { + static void handleProcessTermination(int exitCode, MessageCollector messageCollector) { if (exitCode != 0 && exitCode != 1) { - compileContext.addMessage(ERROR, "Compiler terminated with exit code: " + exitCode, "", -1, -1); + messageCollector.report(ERROR, "Compiler terminated with exit code: " + exitCode, NO_LOCATION); } } - public static void parseCompilerMessagesFromReader(CompileContext compileContext, final Reader reader, OutputItemsCollector collector) { + public static void parseCompilerMessagesFromReader(MessageCollector messageCollector, final Reader reader, OutputItemsCollector collector) { // Sometimes the compiler can't output valid XML // Example: error in command line arguments passed to the compiler // having no -tags key (arguments are not parsed), the compiler doesn't know @@ -130,7 +133,7 @@ public final class CompilerUtils { try { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = factory.newSAXParser(); - parser.parse(new InputSource(wrappingReader), new CompilerOutputSAXHandler(compileContext, collector)); + parser.parse(new InputSource(wrappingReader), new CompilerOutputSAXHandler(messageCollector, collector)); } catch (Throwable e) { @@ -145,7 +148,7 @@ public final class CompilerUtils { String message = stringBuilder.toString(); LOG.error(message); LOG.error(e); - compileContext.addMessage(ERROR, message, null, -1, -1); + messageCollector.report(ERROR, message, NO_LOCATION); } finally { try { @@ -168,24 +171,37 @@ public final class CompilerUtils { public static Object invokeExecMethod(CompilerEnvironment environment, PrintStream out, - CompileContext context, String[] arguments, String name) throws Exception { - URLClassLoader loader = getOrCreateClassLoader(environment.getKotlinHome(), context); + MessageCollector messageCollector, String[] arguments, String name) throws Exception { + URLClassLoader loader = getOrCreateClassLoader(environment.getKotlinHome(), messageCollector); Class kompiler = Class.forName(name, true, loader); Method exec = kompiler.getMethod("exec", PrintStream.class, String[].class); return exec.invoke(kompiler.newInstance(), out, arguments); } + @NotNull + public static CompilerEnvironment getEnvironmentFor(@NotNull CompileContext compileContext, @NotNull Module module, boolean tests) { + VirtualFile mainOutput = compileContext.getModuleOutputDirectory(module); + VirtualFile outputDirectoryForTests = compileContext.getModuleOutputDirectoryForTests(module); + return CompilerEnvironment.getEnvironmentFor(tests, toIoFile(mainOutput), toIoFile(outputDirectoryForTests)); + } + + @Nullable + public static File toIoFile(@Nullable VirtualFile file) { + if (file == null) return null; + return new File(file.getPath()); + } + private static class CompilerOutputSAXHandler extends DefaultHandler { - private static final Map CATEGORIES = ImmutableMap.builder() - .put("error", CompilerMessageCategory.ERROR) - .put("warning", CompilerMessageCategory.WARNING) - .put("logging", CompilerMessageCategory.STATISTICS) - .put("exception", CompilerMessageCategory.ERROR) - .put("info", CompilerMessageCategory.INFORMATION) - .put("messages", CompilerMessageCategory.INFORMATION) // Root XML element + private static final Map CATEGORIES = ImmutableMap.builder() + .put("error", ERROR) + .put("warning", WARNING) + .put("logging", LOGGING) + .put("exception", ERROR) + .put("info", INFO) + .put("messages", INFO) // Root XML element .build(); - private final CompileContext compileContext; + private final MessageCollector messageCollector; private final OutputItemsCollector collector; private final StringBuilder message = new StringBuilder(); @@ -194,8 +210,8 @@ public final class CompilerUtils { private int line; private int column; - public CompilerOutputSAXHandler(CompileContext compileContext, OutputItemsCollector collector) { - this.compileContext = compileContext; + public CompilerOutputSAXHandler(MessageCollector messageCollector, OutputItemsCollector collector) { + this.messageCollector = messageCollector; this.collector = collector; } @@ -217,7 +233,7 @@ public final class CompilerUtils { // We're directly inside the root tag: String message = new String(ch, start, length); if (!message.trim().isEmpty()) { - compileContext.addMessage(ERROR, "Unhandled compiler output: " + message, null, -1, -1); + messageCollector.report(ERROR, "Unhandled compiler output: " + message, NO_LOCATION); } } else { @@ -232,10 +248,10 @@ public final class CompilerUtils { return; } String qNameLowerCase = qName.toLowerCase(); - CompilerMessageCategory category = CATEGORIES.get(qNameLowerCase); + CompilerMessageSeverity category = CATEGORIES.get(qNameLowerCase); if (category == null) { - compileContext.addMessage(ERROR, "Unknown compiler message tag: " + qName, null, -1, -1); - category = INFORMATION; + messageCollector.report(ERROR, "Unknown compiler message tag: " + qName, NO_LOCATION); + category = INFO; } String text = message.toString(); @@ -243,12 +259,11 @@ public final class CompilerUtils { LOG.error(text); } - if (category == STATISTICS) { - compileContext.getProgressIndicator().setText(text); + if (category == LOGGING) { collector.learn(text); } else { - compileContext.addMessage(category, text, path, line, column); + messageCollector.report(category, text, CompilerMessageLocation.create(path, line, column)); } tags.pop(); } @@ -305,8 +320,8 @@ public final class CompilerUtils { } } - public static void outputCompilerMessagesAndHandleExitCode(@NotNull CompileContext context, - @NotNull OutputItemsCollector collector, + public static void outputCompilerMessagesAndHandleExitCode(@NotNull MessageCollector messageCollector, + @NotNull OutputItemsCollector outputItemsCollector, @NotNull Function1 compilerRun) { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); PrintStream out = new PrintStream(outputStream); @@ -314,8 +329,8 @@ public final class CompilerUtils { int exitCode = compilerRun.invoke(out); BufferedReader reader = new BufferedReader(new StringReader(outputStream.toString())); - parseCompilerMessagesFromReader(context, reader, collector); - handleProcessTermination(exitCode, context); + parseCompilerMessagesFromReader(messageCollector, reader, outputItemsCollector); + handleProcessTermination(exitCode, messageCollector); } } diff --git a/idea/src/org/jetbrains/jet/plugin/compiler/JetCompiler.java b/idea/src/org/jetbrains/jet/plugin/compiler/JetCompiler.java index 7af40db1c5d..12866c6dc4c 100644 --- a/idea/src/org/jetbrains/jet/plugin/compiler/JetCompiler.java +++ b/idea/src/org/jetbrains/jet/plugin/compiler/JetCompiler.java @@ -40,6 +40,9 @@ import com.intellij.util.SystemProperties; import com.intellij.util.containers.ContainerUtil; import jet.Function1; 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; import org.jetbrains.jet.compiler.runner.KotlinModuleScriptGenerator; import org.jetbrains.jet.plugin.JetFileType; import org.jetbrains.jet.plugin.project.JsModuleDetector; @@ -50,9 +53,6 @@ import java.io.InputStreamReader; import java.io.PrintStream; import java.util.*; -import static com.intellij.openapi.compiler.CompilerMessageCategory.ERROR; -import static com.intellij.openapi.compiler.CompilerMessageCategory.INFORMATION; - /** * @author yole */ @@ -108,7 +108,8 @@ public class JetCompiler implements TranslatingCompiler { doCompile(compileContext, moduleChunk, testFiles, module, outputSink, true); } - private void doCompile(CompileContext compileContext, + private void doCompile( + final CompileContext compileContext, Chunk moduleChunk, List files, Module module, @@ -116,9 +117,11 @@ public class JetCompiler implements TranslatingCompiler { boolean tests) { if (files.isEmpty()) return; - CompilerEnvironment environment = CompilerEnvironment.getEnvironmentFor(compileContext, module, tests); + MessageCollector messageCollector = new MessageCollectorAdapter(compileContext); + + CompilerEnvironment environment = CompilerUtils.getEnvironmentFor(compileContext, module, tests); if (!environment.success()) { - environment.reportErrorsTo(compileContext); + environment.reportErrorsTo(messageCollector); return; } @@ -129,19 +132,31 @@ public class JetCompiler implements TranslatingCompiler { if (scriptFile == null) return; CompilerUtils.OutputItemsCollectorImpl collector = new CompilerUtils.OutputItemsCollectorImpl(environment.getOutput().getPath()); - runCompiler(compileContext, environment, scriptFile, collector); + runCompiler(messageCollector, environment, scriptFile, collector); outputSink.add(environment.getOutput().getPath(), collector.getOutputs(), collector.getSources().toArray(VirtualFile.EMPTY_ARRAY)); } - private void runCompiler(CompileContext compileContext, + private static void runCompiler( + MessageCollector messageCollector, CompilerEnvironment environment, File scriptFile, - CompilerUtils.OutputItemsCollectorImpl collector) { - if (RUN_OUT_OF_PROCESS) { - runOutOfProcess(compileContext, collector, environment, scriptFile); + CompilerUtils.OutputItemsCollectorImpl collector + ) { + runCompiler(messageCollector, environment, scriptFile, collector, RUN_OUT_OF_PROCESS); + } + + private static void runCompiler( + MessageCollector messageCollector, + CompilerEnvironment environment, + File scriptFile, + CompilerUtils.OutputItemsCollectorImpl collector, + boolean runOutOfProcess + ) { + if (runOutOfProcess) { + runOutOfProcess(messageCollector, collector, environment, scriptFile); } else { - runInProcess(compileContext, collector, environment, scriptFile); + runInProcess(messageCollector, collector, environment, scriptFile); } } @@ -150,7 +165,7 @@ public class JetCompiler implements TranslatingCompiler { Chunk moduleChunk, List files, Module module, - boolean tests, VirtualFile mainOutput, VirtualFile outputDir + boolean tests, VirtualFile mainOutput, File outputDir ) { ArrayList sourceFilePaths = ContainerUtil.newArrayList(paths(files)); ModuleChunk chunk = new ModuleChunk((CompileContextEx)compileContext, moduleChunk, Collections.>emptyMap()); @@ -171,7 +186,7 @@ public class JetCompiler implements TranslatingCompiler { outputDirectoriesToFilter ); - File scriptFile = new File(path(outputDir), "script.kts"); + File scriptFile = new File(outputDir, "script.kts"); try { FileUtil.writeToFile(scriptFile, script.toString()); } @@ -208,27 +223,29 @@ public class JetCompiler implements TranslatingCompiler { }; } - private static void runInProcess(final CompileContext compileContext, + private static void runInProcess(final MessageCollector messageCollector, OutputItemsCollector collector, final CompilerEnvironment environment, final File scriptFile) { - CompilerUtils.outputCompilerMessagesAndHandleExitCode(compileContext, collector, new Function1() { + CompilerUtils.outputCompilerMessagesAndHandleExitCode(messageCollector, collector, new Function1() { @Override public Integer invoke(PrintStream stream) { - return execInProcess(environment, scriptFile, stream, compileContext); + return execInProcess(environment, scriptFile, stream, messageCollector); } }); } - private static int execInProcess(CompilerEnvironment environment, File scriptFile, PrintStream out, CompileContext context) { + private static int execInProcess(CompilerEnvironment environment, File scriptFile, PrintStream out, MessageCollector messageCollector) { try { String compilerClassName = "org.jetbrains.jet.cli.jvm.K2JVMCompiler"; String[] arguments = commandLineArguments(environment.getOutput(), scriptFile); - context.addMessage(INFORMATION, "Using kotlinHome=" + environment.getKotlinHome(), "", -1, -1); - context.addMessage(INFORMATION, - "Invoking in-process compiler " + compilerClassName + " with arguments " + Arrays.asList(arguments), "", -1, - -1); - Object rc = CompilerUtils.invokeExecMethod(environment, out, context, arguments, compilerClassName); + messageCollector.report(CompilerMessageSeverity.INFO, + "Using kotlinHome=" + environment.getKotlinHome(), + CompilerMessageLocation.NO_LOCATION); + messageCollector.report(CompilerMessageSeverity.INFO, + "Invoking in-process compiler " + compilerClassName + " with arguments " + Arrays.asList(arguments), + CompilerMessageLocation.NO_LOCATION); + Object rc = CompilerUtils.invokeExecMethod(environment, out, messageCollector, arguments, compilerClassName); // exec() returns a K2JVMCompiler.ExitCode object, that class is not accessible here, // so we take it's contents through reflection return CompilerUtils.getReturnCodeFromObject(rc); @@ -239,18 +256,20 @@ public class JetCompiler implements TranslatingCompiler { } } - private static String[] commandLineArguments(VirtualFile outputDir, File scriptFile) { + private static String[] commandLineArguments(File outputDir, File scriptFile) { return new String[]{ "-module", scriptFile.getAbsolutePath(), - "-output", path(outputDir), + "-output", outputDir.getPath(), "-tags", "-verbose", "-version", "-noStdlib", "-noJdkAnnotations", "-noJdk"}; } - private static void runOutOfProcess(final CompileContext compileContext, - final OutputItemsCollector collector, + private static void runOutOfProcess( + final MessageCollector messageCollector, + final OutputItemsCollector itemCollector, CompilerEnvironment environment, - File scriptFile) { + File scriptFile + ) { final SimpleJavaParameters params = new SimpleJavaParameters(); params.setJdk(new SimpleJavaSdkType().createJdk("tmp", SystemProperties.getJavaHome())); params.setMainClass("org.jetbrains.jet.cli.jvm.K2JVMCompiler"); @@ -259,7 +278,7 @@ public class JetCompiler implements TranslatingCompiler { params.getProgramParametersList().add(arg); } - for (File jar : CompilerUtils.kompilerClasspath(environment.getKotlinHome(), compileContext)) { + for (File jar : CompilerUtils.kompilerClasspath(environment.getKotlinHome(), messageCollector)) { params.getClassPath().add(jar); } @@ -271,7 +290,9 @@ public class JetCompiler implements TranslatingCompiler { final GeneralCommandLine commandLine = JdkUtil.setupJVMCommandLine( ((JavaSdkType)sdk.getSdkType()).getVMExecutablePath(sdk), params, false); - compileContext.addMessage(INFORMATION, "Invoking out-of-process compiler with arguments: " + commandLine, "", -1, -1); + messageCollector.report(CompilerMessageSeverity.INFO, + "Invoking out-of-process compiler with arguments: " + commandLine, + CompilerMessageLocation.NO_LOCATION); try { final Process process = commandLine.createProcess(); @@ -280,7 +301,7 @@ public class JetCompiler implements TranslatingCompiler { @Override public void run() { CompilerUtils - .parseCompilerMessagesFromReader(compileContext, new InputStreamReader(process.getInputStream()), collector); + .parseCompilerMessagesFromReader(messageCollector, new InputStreamReader(process.getInputStream()), itemCollector); } }); @@ -297,10 +318,12 @@ public class JetCompiler implements TranslatingCompiler { }); int exitCode = process.waitFor(); - CompilerUtils.handleProcessTermination(exitCode, compileContext); + CompilerUtils.handleProcessTermination(exitCode, messageCollector); } catch (Exception e) { - compileContext.addMessage(ERROR, "[Internal Error] " + e.getLocalizedMessage(), "", -1, -1); + messageCollector.report(CompilerMessageSeverity.ERROR, + "[Internal Error] " + e.getLocalizedMessage(), + CompilerMessageLocation.NO_LOCATION); return; } } diff --git a/idea/src/org/jetbrains/jet/plugin/compiler/K2JSCompiler.java b/idea/src/org/jetbrains/jet/plugin/compiler/K2JSCompiler.java index 95758e7bdbd..0f098ba6b3f 100644 --- a/idea/src/org/jetbrains/jet/plugin/compiler/K2JSCompiler.java +++ b/idea/src/org/jetbrains/jet/plugin/compiler/K2JSCompiler.java @@ -26,6 +26,7 @@ import com.intellij.openapi.roots.ModuleOrderEntry; import com.intellij.openapi.roots.ModuleRootManager; import com.intellij.openapi.roots.OrderEntry; import com.intellij.openapi.util.Pair; +import com.intellij.openapi.vfs.LocalFileSystem; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.util.ArrayUtil; import com.intellij.util.Chunk; @@ -34,9 +35,13 @@ import gnu.trove.THashSet; import jet.Function1; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.jetbrains.jet.cli.common.messages.CompilerMessageLocation; +import org.jetbrains.jet.cli.common.messages.CompilerMessageSeverity; +import org.jetbrains.jet.cli.common.messages.MessageCollector; import org.jetbrains.jet.plugin.JetFileType; import org.jetbrains.jet.plugin.project.JsModuleDetector; +import java.io.File; import java.io.PrintStream; import java.util.ArrayList; import java.util.List; @@ -72,22 +77,24 @@ public final class K2JSCompiler implements TranslatingCompiler { return; } - CompilerEnvironment environment = CompilerEnvironment.getEnvironmentFor(context, module, /*tests = */ false); + MessageCollector messageCollector = new MessageCollectorAdapter(context); + + CompilerEnvironment environment = CompilerUtils.getEnvironmentFor(context, module, /*tests = */ false); if (!environment.success()) { - environment.reportErrorsTo(context); + environment.reportErrorsTo(messageCollector); return; } - doCompile(context, sink, module, environment); + doCompile(messageCollector, sink, module, environment); } - private static void doCompile(@NotNull final CompileContext context, @NotNull OutputSink sink, @NotNull final Module module, + private static void doCompile(@NotNull final MessageCollector messageCollector, @NotNull OutputSink sink, @NotNull final Module module, @NotNull final CompilerEnvironment environment) { CompilerUtils.OutputItemsCollectorImpl collector = new CompilerUtils.OutputItemsCollectorImpl(environment.getOutput().getPath()); - outputCompilerMessagesAndHandleExitCode(context, collector, new Function1() { + outputCompilerMessagesAndHandleExitCode(messageCollector, collector, new Function1() { @Override public Integer invoke(PrintStream stream) { - return execInProcess(context, environment, stream, module); + return execInProcess(messageCollector, environment, stream, module); } }); sink.add(environment.getOutput().getPath(), collector.getOutputs(), collector.getSources().toArray(VirtualFile.EMPTY_ARRAY)); @@ -103,27 +110,31 @@ public final class K2JSCompiler implements TranslatingCompiler { } @NotNull - private static Integer execInProcess(@NotNull CompileContext context, + private static Integer execInProcess(@NotNull MessageCollector messageCollector, @NotNull CompilerEnvironment environment, @NotNull PrintStream out, @NotNull Module module) { try { - return doExec(context, environment, out, module); + return doExec(messageCollector, environment, out, module); } catch (Throwable e) { - context.addMessage(CompilerMessageCategory.ERROR, "Exception while executing compiler:\n" + e.getMessage(), null, -1, -1); + messageCollector.report(CompilerMessageSeverity.ERROR, + "Exception while executing compiler:\n" + e.getMessage(), + CompilerMessageLocation.NO_LOCATION); } return -1; } @NotNull - private static Integer doExec(@NotNull CompileContext context, @NotNull CompilerEnvironment environment, @NotNull PrintStream out, + private static Integer doExec(@NotNull MessageCollector messageCollector, @NotNull CompilerEnvironment environment, @NotNull PrintStream out, @NotNull Module module) throws Exception { - VirtualFile outDir = context.getModuleOutputDirectory(module); - String outFile = outDir == null ? null : outDir.getPath() + "/" + module.getName() + ".js"; + File outDir = environment.getOutput(); + String outFile = outDir.getPath() + "/" + module.getName() + ".js"; String[] commandLineArgs = constructArguments(module, outFile); - Object rc = invokeExecMethod(environment, out, context, commandLineArgs, "org.jetbrains.jet.cli.js.K2JSCompiler"); + Object rc = invokeExecMethod(environment, out, messageCollector, commandLineArgs, "org.jetbrains.jet.cli.js.K2JSCompiler"); - if (outDir != null && !ApplicationManager.getApplication().isUnitTestMode()) { - outDir.refresh(false, true); + if (!ApplicationManager.getApplication().isUnitTestMode()) { + VirtualFile virtualFile = LocalFileSystem.getInstance().findFileByIoFile(outDir); + assert virtualFile != null : "Virtual file not found for module output: " + outDir; + virtualFile.refresh(false, true); } return CompilerUtils.getReturnCodeFromObject(rc); } diff --git a/idea/src/org/jetbrains/jet/plugin/compiler/MessageCollectorAdapter.java b/idea/src/org/jetbrains/jet/plugin/compiler/MessageCollectorAdapter.java new file mode 100644 index 00000000000..dba231f86ee --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/compiler/MessageCollectorAdapter.java @@ -0,0 +1,63 @@ +/* + * Copyright 2010-2012 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.jet.plugin.compiler; + +import com.intellij.openapi.compiler.CompileContext; +import com.intellij.openapi.compiler.CompilerMessageCategory; +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; + +import static com.intellij.openapi.compiler.CompilerMessageCategory.*; + +class MessageCollectorAdapter implements MessageCollector { + private final CompileContext compileContext; + + public MessageCollectorAdapter(CompileContext compileContext) { + this.compileContext = compileContext; + } + + @Override + public void report( + @NotNull CompilerMessageSeverity severity, + @NotNull String message, + @NotNull CompilerMessageLocation location + ) { + CompilerMessageCategory category = category(severity); + compileContext.addMessage(category, message, location.getPath(), location.getLine(), location.getColumn()); + if (category == STATISTICS) { + compileContext.getProgressIndicator().setText(message); + } + } + + private static CompilerMessageCategory category(CompilerMessageSeverity severity) { + switch (severity) { + case INFO: + return INFORMATION; + case ERROR: + return ERROR; + case WARNING: + return WARNING; + case EXCEPTION: + return ERROR; + case LOGGING: + return STATISTICS; + } + throw new IllegalArgumentException("Unknown severity: " + severity); + } +}