From 20edf061171611790d04bf2d56d58fe74cc78758 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Galland?= Date: Wed, 25 Jul 2018 10:58:26 +0200 Subject: [PATCH] [sarlc] Add an information message about the numbers of encountered errors and warnings. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Stéphane Galland --- .../io/sarl/lang/sarlc/BootiqueSarlcMain.java | 2 +- .../lang/sarlc/commands/CompilerCommand.java | 54 +++++++++++++++++++ .../io/sarl/lang/sarlc/commands/Messages.java | 8 +++ .../lang/sarlc/commands/messages.properties | 8 +++ 4 files changed, 71 insertions(+), 1 deletion(-) diff --git a/products/sarlc/src/main/java/io/sarl/lang/sarlc/BootiqueSarlcMain.java b/products/sarlc/src/main/java/io/sarl/lang/sarlc/BootiqueSarlcMain.java index f9e5cc7a0f..680902e0e8 100644 --- a/products/sarlc/src/main/java/io/sarl/lang/sarlc/BootiqueSarlcMain.java +++ b/products/sarlc/src/main/java/io/sarl/lang/sarlc/BootiqueSarlcMain.java @@ -71,7 +71,7 @@ public int runCompiler(String... args) { try { final BQRuntime runtime = createRuntime(args); final CommandOutcome outcome = runtime.run(); - if (!outcome.isSuccess()) { + if (!outcome.isSuccess() && outcome.getException() != null) { Logger.getRootLogger().error(outcome.getMessage(), outcome.getException()); } return outcome.getExitCode(); diff --git a/products/sarlc/src/main/java/io/sarl/lang/sarlc/commands/CompilerCommand.java b/products/sarlc/src/main/java/io/sarl/lang/sarlc/commands/CompilerCommand.java index ac8d3658fc..7e1061f0da 100644 --- a/products/sarlc/src/main/java/io/sarl/lang/sarlc/commands/CompilerCommand.java +++ b/products/sarlc/src/main/java/io/sarl/lang/sarlc/commands/CompilerCommand.java @@ -21,6 +21,9 @@ package io.sarl.lang.sarlc.commands; +import java.text.MessageFormat; +import java.util.concurrent.atomic.AtomicInteger; + import com.google.common.base.Strings; import com.google.inject.Provider; import io.bootique.cli.Cli; @@ -31,6 +34,7 @@ import me.tongfei.progressbar.ProgressBarBuilder; import me.tongfei.progressbar.ProgressBarStyle; import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.xtext.diagnostics.Severity; import io.sarl.lang.compiler.batch.SarlBatchCompiler; import io.sarl.lang.sarlc.Constants; @@ -105,10 +109,17 @@ public CommandOutcome run(Cli cli) { } final OutParameter firstErrorMessage = new OutParameter<>(); + final AtomicInteger nbErrors = new AtomicInteger(0); + final AtomicInteger nbWarnings = new AtomicInteger(0); comp.addIssueMessageListener((issue, uri, message) -> { if (firstErrorMessage.get() == null) { firstErrorMessage.set(message); } + if (issue.isSyntaxError() || issue.getSeverity() == Severity.ERROR) { + nbErrors.incrementAndGet(); + } else if (issue.getSeverity() == Severity.WARNING) { + nbWarnings.incrementAndGet(); + } }); final ProgressBarConfig commandConfig = this.commandConfig.get(); @@ -119,11 +130,54 @@ public CommandOutcome run(Cli cli) { compilationResult = comp.compile(); } if (!compilationResult) { + showErrorAndWarningCount(comp, nbErrors.longValue(), nbWarnings.longValue()); return CommandOutcome.failed(Constants.ERROR_CODE, Strings.nullToEmpty(firstErrorMessage.get())); } + showWarningCount(comp, nbWarnings.longValue()); return CommandOutcome.succeeded(); } + private static void showErrorAndWarningCount(SarlBatchCompiler comp, Number errs, Number warns) { + final long errValue = errs.longValue(); + if (errValue > 0) { + final long warnValue = warns.longValue(); + final String msg; + if (errValue > 1) { + if (warnValue > 1) { + msg = Messages.CompilerCommand_2; + } else if (warnValue == 1) { + msg = Messages.CompilerCommand_3; + } else { + msg = Messages.CompilerCommand_4; + } + } else { + if (warnValue > 1) { + msg = Messages.CompilerCommand_5; + } else if (warnValue == 1) { + msg = Messages.CompilerCommand_6; + } else { + msg = Messages.CompilerCommand_7; + } + } + comp.getLogger().info(MessageFormat.format(msg, errValue, warnValue)); + } else { + showWarningCount(comp, warns); + } + } + + private static void showWarningCount(SarlBatchCompiler comp, Number warns) { + final long value = warns.longValue(); + if (value > 0) { + final String msg; + if (value > 1) { + msg = Messages.CompilerCommand_8; + } else { + msg = Messages.CompilerCommand_9; + } + comp.getLogger().info(MessageFormat.format(msg, value)); + } + } + /** Progress monitor that outputs on the console. * * @author $Author: sgalland$ diff --git a/products/sarlc/src/main/java/io/sarl/lang/sarlc/commands/Messages.java b/products/sarlc/src/main/java/io/sarl/lang/sarlc/commands/Messages.java index 0cd6b9f68c..8e1de2907c 100644 --- a/products/sarlc/src/main/java/io/sarl/lang/sarlc/commands/Messages.java +++ b/products/sarlc/src/main/java/io/sarl/lang/sarlc/commands/Messages.java @@ -36,6 +36,14 @@ public class Messages extends NLS { private static final String BUNDLE_NAME = Messages.class.getPackage().getName() + ".messages"; //$NON-NLS-1$ public static String CompilerCommand_0; public static String CompilerCommand_1; + public static String CompilerCommand_2; + public static String CompilerCommand_3; + public static String CompilerCommand_4; + public static String CompilerCommand_5; + public static String CompilerCommand_6; + public static String CompilerCommand_7; + public static String CompilerCommand_8; + public static String CompilerCommand_9; public static String VersionCommand_0; public static String VersionCommand_1; static { diff --git a/products/sarlc/src/main/resources/io/sarl/lang/sarlc/commands/messages.properties b/products/sarlc/src/main/resources/io/sarl/lang/sarlc/commands/messages.properties index 37bbe9ecbe..2e48e53706 100644 --- a/products/sarlc/src/main/resources/io/sarl/lang/sarlc/commands/messages.properties +++ b/products/sarlc/src/main/resources/io/sarl/lang/sarlc/commands/messages.properties @@ -1,4 +1,12 @@ CompilerCommand_0 = Run sarlc. CompilerCommand_1 = Not enough arguments. Source folders must be provided as arguments. +CompilerCommand_2 = Found {0} errors and {1} warnings +CompilerCommand_3 = Found {0} errors and {1} warning +CompilerCommand_4 = Found {0} errors +CompilerCommand_5 = Found {0} error and {1} warnings +CompilerCommand_6 = Found {0} error and {1} warning +CompilerCommand_7 = Found {0} error +CompilerCommand_8 = Found {0} warnings +CompilerCommand_9 = Found {0} warning VersionCommand_0 = Prints release information. VersionCommand_1 = SARL Version: {0}\nSARL Specification Version: {1}\nJava Version: {2}\n