Skip to content

Commit

Permalink
[sarlc] Add an information message about the numbers of encountered e…
Browse files Browse the repository at this point in the history
…rrors and warnings.

Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Jul 25, 2018
1 parent 4e5ab85 commit 20edf06
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
Expand Up @@ -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();
Expand Down
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -105,10 +109,17 @@ public CommandOutcome run(Cli cli) {
}

final OutParameter<String> 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();
Expand All @@ -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$
Expand Down
Expand Up @@ -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 {
Expand Down
@@ -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

0 comments on commit 20edf06

Please sign in to comment.