Skip to content

Commit

Permalink
[sarlc] Add a progress bar that could be enabled with --progress.
Browse files Browse the repository at this point in the history
Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Jul 24, 2018
1 parent 59baf8f commit 86af7ed
Show file tree
Hide file tree
Showing 10 changed files with 453 additions and 5 deletions.
5 changes: 5 additions & 0 deletions products/sarlc/pom.xml
Expand Up @@ -43,6 +43,11 @@
<groupId>org.arakhne.afc.bootique</groupId>
<artifactId>bootique-log4j</artifactId>
</dependency>
<dependency>
<groupId>me.tongfei</groupId>
<artifactId>progressbar</artifactId>
<version>0.7.0</version>
</dependency>
</dependencies>

<build>
Expand Down
Expand Up @@ -94,6 +94,9 @@ public int runCompiler(String... args) {
try {
final BQRuntime runtime = createRuntime(args);
final CommandOutcome outcome = runtime.run();
if (!outcome.isSuccess()) {
Logger.getRootLogger().error(outcome.getMessage(), outcome.getException());
}
return outcome.getExitCode();
} catch (ProvisionException exception) {
final Throwable ex = Throwables.getRootCause(exception);
Expand Down
Expand Up @@ -27,9 +27,14 @@
import io.bootique.command.CommandOutcome;
import io.bootique.command.CommandWithMetadata;
import io.bootique.meta.application.CommandMetadata;
import me.tongfei.progressbar.ProgressBar;
import me.tongfei.progressbar.ProgressBarBuilder;
import me.tongfei.progressbar.ProgressBarStyle;
import org.eclipse.core.runtime.IProgressMonitor;

import io.sarl.lang.compiler.batch.SarlBatchCompiler;
import io.sarl.lang.sarlc.Constants;
import io.sarl.lang.sarlc.configs.ProgressBarConfig;
import io.sarl.lang.sarlc.configs.SarlConfig;
import io.sarl.lang.util.OutParameter;

Expand All @@ -44,26 +49,34 @@
*/
public class CompilerCommand extends CommandWithMetadata {

/** Name of the option for enabling the progress bar.
*/
public static final String PROGRESS_OPTION_NAME = "progress"; //$NON-NLS-1$

private final Provider<SarlBatchCompiler> compiler;

private final Provider<SarlConfig> configuration;

private final Provider<PathDetector> pathDetector;

private final Provider<ProgressBarConfig> commandConfig;

/** Constructor.
*
* @param compiler the SARL batch compiler.
* @param configuration the configuration of the tool.
* @param pathDetector the detector of path.
* @param commandConfig the configuration of the command.
*/
public CompilerCommand(Provider<SarlBatchCompiler> compiler, Provider<SarlConfig> configuration,
Provider<PathDetector> pathDetector) {
Provider<PathDetector> pathDetector, Provider<ProgressBarConfig> commandConfig) {
super(CommandMetadata
.builder(CompilerCommand.class)
.description(Messages.CompilerCommand_0));
this.compiler = compiler;
this.configuration = configuration;
this.pathDetector = pathDetector;
this.commandConfig = commandConfig;
}

@Override
Expand Down Expand Up @@ -97,10 +110,98 @@ public CommandOutcome run(Cli cli) {
}
});

if (!comp.compile()) {
final ProgressBarConfig commandConfig = this.commandConfig.get();
final boolean compilationResult;
if (commandConfig.getEnable()) {
compilationResult = comp.compile(new ConsoleProgressMonitor(commandConfig.getStyle()));
} else {
compilationResult = comp.compile();
}
if (!compilationResult) {
return CommandOutcome.failed(Constants.ERROR_CODE, Strings.nullToEmpty(firstErrorMessage.get()));
}
return CommandOutcome.succeeded();
}

/** Progress monitor that outputs on the console.
*
* @author $Author: sgalland$
* @version $FullVersion$
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
* @since 0.8
*/
private static class ConsoleProgressMonitor implements IProgressMonitor {

private final ProgressBarBuilder builder;

private ProgressBar bar;

private boolean isCanceled;

/** Constructor.
*
* @param style the style of the progress bar.
*/
ConsoleProgressMonitor(io.sarl.lang.sarlc.configs.ProgressBarStyle style) {
this.builder = new ProgressBarBuilder().setStyle(style.toBackgroundStyle());
}

private void ensureBar() {
if (this.bar == null) {
this.bar = this.builder.build();
}
}

@Override
public void beginTask(String name, int totalWorkUnits) {
if (!Strings.isNullOrEmpty(name)) {
this.builder.setTaskName(name);
}
this.builder.setStyle(ProgressBarStyle.COLORFUL_UNICODE_BLOCK).setInitialMax(totalWorkUnits);
}

@Override
public void done() {
ensureBar();
this.bar.stepTo(this.bar.getMax());
this.bar.close();
}

@Override
public void internalWorked(double workUnits) {
ensureBar();
this.bar.setExtraMessage("").stepTo((long) workUnits); //$NON-NLS-1$
}

@Override
public boolean isCanceled() {
return this.isCanceled;
}

@Override
public void setCanceled(boolean cancel) {
this.isCanceled = cancel;
}

@Override
public void setTaskName(String name) {
if (!Strings.isNullOrEmpty(name)) {
this.builder.setTaskName(name);
}
}

@Override
public void subTask(String name) {
//
}

@Override
public void worked(int workUnits) {
ensureBar();
this.bar.setExtraMessage("").stepTo(workUnits); //$NON-NLS-1$
}

}

}
@@ -0,0 +1,138 @@
/*
* $Id$
*
* SARL is an general-purpose agent programming language.
* More details on http://www.sarl.io
*
* Copyright (C) 2014-2018 the original authors or authors.
*
* 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 io.sarl.lang.sarlc.configs;

import io.bootique.annotation.BQConfig;
import io.bootique.annotation.BQConfigProperty;
import io.bootique.config.ConfigurationFactory;
import org.arakhne.afc.bootique.log4j.configs.Level;

/**
* Configuration for the compiler command.
*
* @author $Author: sgalland$
* @version $FullVersion$
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
* @since 0.8
*/
@BQConfig("Configuration of the SARLC tool")
public class ProgressBarConfig {

/**
* Prefix for the configuration entries of the compiler command.
*/
public static final String PREFIX = "progressBar"; //$NON-NLS-1$

/**
* Name of the property that indicates if the progress bar is active or not.
*/
public static final String ENABLE = PREFIX + ".enable"; //$NON-NLS-1$

/**
* Name of the property that contains the logger level when the progress bar is active.
*/
public static final String LEVEL = PREFIX + ".level"; //$NON-NLS-1$

/**
* Default logger level when the progress bar is active.
*/
public static final Level DEFAULT_LEVEL = Level.ERROR;

/**
* Name of the property that contains the output path for the Java byte code.
*/
public static final String STYLE = PREFIX + ".style"; //$NON-NLS-1$

/**
* Default progress bar style.
*/
public static final ProgressBarStyle DEFAULT_STYLE = ProgressBarStyle.COLORED_UNICODE;

private boolean enable;

private Level level = DEFAULT_LEVEL;

private ProgressBarStyle style = DEFAULT_STYLE;

/** Replies the configuration factory for the logging.
*
* @param configFactory the general configuration factory.
* @return the logging configuration factory.
*/
public static ProgressBarConfig getConfiguration(ConfigurationFactory configFactory) {
assert configFactory != null;
return configFactory.config(ProgressBarConfig.class, PREFIX);
}

/** Replies if the progress bar is active or not..
*
* @return {@code true} if the progress bar is active.
*/
public boolean getEnable() {
return this.enable;
}

/** Set if the progress bar is active or not..
*
* @param enable {@code true} if the progress bar is active.
*/
@BQConfigProperty("Enable or disable the progress bar.")
public void setEnable(boolean enable) {
this.enable = enable;
}

/** Replies the logger level that should be used when the progress bar is active.
*
* @return the logger level.
*/
public Level getLevel() {
return this.level;
}

/** Set the logger level that should be used when the progress bar is active.
*
* @param level the progress bar level. If {@code null}, the {@link #DEFAULT_LEVEL default level} is assumed.
*/
@BQConfigProperty("Specify the logger level that should be used when the progress bar is active. Default is ERROR.")
public void setLevel(Level level) {
this.level = level == null ? DEFAULT_LEVEL : level;
}

/** Replies the style of the progress bar.
*
* @return the style.
*/
public ProgressBarStyle getStyle() {
return this.style;
}

/** Set the style of the progress bar.
*
* @param style the style. If {@code null}, the {@link #DEFAULT_STYLE default style} is assumed.
*/
@BQConfigProperty("Specify the style of the progress bar. Default is COLORED_UNICODE.")
public void setStyle(ProgressBarStyle style) {
this.style = style == null ? DEFAULT_STYLE : style;
}

}

0 comments on commit 86af7ed

Please sign in to comment.