From f6a8c5e88619c2fe0063e63429809c0135c2344b Mon Sep 17 00:00:00 2001 From: Andrey Breslav Date: Thu, 31 Jan 2013 18:03:09 +0400 Subject: [PATCH] Exit code is COMPILATION_ERROR whenever an error was reported through a message collector --- .../cli/common/messages/PrintingMessageCollector.java | 11 +++++++++++ .../src/org/jetbrains/jet/cli/js/K2JSCompiler.java | 6 ++++-- .../src/org/jetbrains/jet/cli/jvm/K2JVMCompiler.java | 9 ++++----- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/compiler/cli/cli-common/src/org/jetbrains/jet/cli/common/messages/PrintingMessageCollector.java b/compiler/cli/cli-common/src/org/jetbrains/jet/cli/common/messages/PrintingMessageCollector.java index d9c79a860c3..67e6653a42f 100644 --- a/compiler/cli/cli-common/src/org/jetbrains/jet/cli/common/messages/PrintingMessageCollector.java +++ b/compiler/cli/cli-common/src/org/jetbrains/jet/cli/common/messages/PrintingMessageCollector.java @@ -18,10 +18,13 @@ package org.jetbrains.jet.cli.common.messages; import com.google.common.collect.LinkedHashMultimap; import com.google.common.collect.Multimap; +import com.google.common.collect.Sets; import org.jetbrains.annotations.NotNull; import java.io.PrintStream; +import java.util.Arrays; import java.util.Collection; +import java.util.Set; public class PrintingMessageCollector implements MessageCollector { @@ -32,6 +35,8 @@ public class PrintingMessageCollector implements MessageCollector { // File path (nullable) -> error message private final Multimap groupedMessages = LinkedHashMultimap.create(); + private final Set reportedSeverities = Sets.newHashSet(); + public PrintingMessageCollector(PrintStream errStream, MessageRenderer messageRenderer, boolean verbose) { @@ -44,6 +49,8 @@ public class PrintingMessageCollector implements MessageCollector { public void report(@NotNull CompilerMessageSeverity severity, @NotNull String message, @NotNull CompilerMessageLocation location) { + reportedSeverities.add(severity); + String text = messageRenderer.render(severity, message, location); if (severity == CompilerMessageSeverity.LOGGING || severity == CompilerMessageSeverity.OUTPUT) { if (!verbose) { @@ -74,4 +81,8 @@ public class PrintingMessageCollector implements MessageCollector { public void setVerbose(boolean verbose) { this.verbose = verbose; } + + public boolean anyReported(@NotNull CompilerMessageSeverity... severities) { + return reportedSeverities.containsAll(Arrays.asList(severities)); + } } diff --git a/compiler/cli/src/org/jetbrains/jet/cli/js/K2JSCompiler.java b/compiler/cli/src/org/jetbrains/jet/cli/js/K2JSCompiler.java index bd13d85b0bd..54d353101fd 100644 --- a/compiler/cli/src/org/jetbrains/jet/cli/js/K2JSCompiler.java +++ b/compiler/cli/src/org/jetbrains/jet/cli/js/K2JSCompiler.java @@ -48,6 +48,8 @@ import java.io.File; import java.util.Arrays; import java.util.List; +import static org.jetbrains.jet.cli.common.ExitCode.COMPILATION_ERROR; +import static org.jetbrains.jet.cli.common.ExitCode.OK; import static org.jetbrains.jet.cli.common.messages.CompilerMessageLocation.NO_LOCATION; public class K2JSCompiler extends CLICompiler { @@ -87,7 +89,7 @@ public class K2JSCompiler extends CLICompiler { Config config = getConfig(arguments, project); if (analyzeAndReportErrors(messageCollector, environmentForJS.getSourceFiles(), config)) { - return ExitCode.COMPILATION_ERROR; + return COMPILATION_ERROR; } String outputFile = arguments.outputFile; @@ -132,7 +134,7 @@ public class K2JSCompiler extends CLICompiler { // for example inside a mvn plugin we need to see the stack trace return ExitCode.INTERNAL_ERROR; } - return ExitCode.OK; + return messageCollector.anyReported(CompilerMessageSeverity.ERROR) ? COMPILATION_ERROR : OK; } private static boolean analyzeAndReportErrors(@NotNull PrintingMessageCollector messageCollector, diff --git a/compiler/cli/src/org/jetbrains/jet/cli/jvm/K2JVMCompiler.java b/compiler/cli/src/org/jetbrains/jet/cli/jvm/K2JVMCompiler.java index 2b28a484d11..4b989bd2bbf 100644 --- a/compiler/cli/src/org/jetbrains/jet/cli/jvm/K2JVMCompiler.java +++ b/compiler/cli/src/org/jetbrains/jet/cli/jvm/K2JVMCompiler.java @@ -118,27 +118,26 @@ public class K2JVMCompiler extends CLICompiler { File jar = arguments.jar != null ? new File(arguments.jar) : null; File outputDir = arguments.outputDir != null ? new File(arguments.outputDir) : null; - boolean noErrors; if (arguments.module != null) { boolean oldVerbose = messageCollector.isVerbose(); messageCollector.setVerbose(false); List modules = CompileEnvironmentUtil.loadModuleScript(paths, arguments.module, messageCollector); messageCollector.setVerbose(oldVerbose); File directory = new File(arguments.module).getParentFile(); - noErrors = KotlinToJVMBytecodeCompiler.compileModules(configuration, modules, + KotlinToJVMBytecodeCompiler.compileModules(configuration, modules, directory, jar, outputDir, arguments.includeRuntime); } else if (arguments.script) { List scriptArgs = arguments.freeArgs.subList(1, arguments.freeArgs.size()); JetCoreEnvironment environment = new JetCoreEnvironment(rootDisposable, configuration); - noErrors = KotlinToJVMBytecodeCompiler.compileAndExecuteScript(paths, environment, scriptArgs); + KotlinToJVMBytecodeCompiler.compileAndExecuteScript(paths, environment, scriptArgs); } else { JetCoreEnvironment environment = new JetCoreEnvironment(rootDisposable, configuration); - noErrors = KotlinToJVMBytecodeCompiler.compileBunchOfSources(environment, jar, outputDir, arguments.includeRuntime); + KotlinToJVMBytecodeCompiler.compileBunchOfSources(environment, jar, outputDir, arguments.includeRuntime); } - return noErrors ? OK : COMPILATION_ERROR; + return messageCollector.anyReported(CompilerMessageSeverity.ERROR) ? COMPILATION_ERROR : OK; } catch (CompilationException e) { messageCollector.report(CompilerMessageSeverity.EXCEPTION, MessageRenderer.PLAIN.renderException(e),