Skip to content

Commit

Permalink
[sarlc] Refactoring for using only Bootique injection.
Browse files Browse the repository at this point in the history
All the configurable elements/variables are now injected with Bootique
modules. This version of sarlc uses the Bootique modules that are
provided by AFC libraries (printconfig, synopsishelp...)

Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Jul 24, 2018
1 parent f8b5e07 commit 2c53567
Show file tree
Hide file tree
Showing 40 changed files with 1,550 additions and 1,453 deletions.
39 changes: 30 additions & 9 deletions products/sarlc/pom.xml
@@ -1,4 +1,5 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

Expand All @@ -23,8 +24,24 @@
<artifactId>io.sarl.maven.batchcompiler</artifactId>
</dependency>
<dependency>
<groupId>io.sarl.maven</groupId>
<artifactId>io.sarl.maven.bqextension</artifactId>
<groupId>org.arakhne.afc.bootique</groupId>
<artifactId>bootique-applicationdata2</artifactId>
</dependency>
<dependency>
<groupId>org.arakhne.afc.bootique</groupId>
<artifactId>bootique-synopsishelp</artifactId>
</dependency>
<dependency>
<groupId>org.arakhne.afc.bootique</groupId>
<artifactId>bootique-printconfig</artifactId>
</dependency>
<dependency>
<groupId>org.arakhne.afc.bootique</groupId>
<artifactId>bootique-variables</artifactId>
</dependency>
<dependency>
<groupId>org.arakhne.afc.bootique</groupId>
<artifactId>bootique-log4j</artifactId>
</dependency>
</dependencies>

Expand Down Expand Up @@ -112,12 +129,16 @@
<target>
<property name="sarlc.linux.input"
value="${project.basedir}/src-templates/sarlc.sh" />
<property name="sarlc.linux.output" value="${project.build.directory}/sarlc.tmp" />
<property name="sarlc.linux.output.full" value="${project.build.directory}/sarlc" />
<copy overwrite="true" file="${sarlc.linux.input}" tofile="${sarlc.linux.output}" />
<replace file="${sarlc.linux.output}" token="{cliCompilerMainClass}"
value="${cliCompilerMainClass}" />
<replace file="${sarlc.linux.output}" token="{project.build.finalName}"
<property name="sarlc.linux.output"
value="${project.build.directory}/sarlc.tmp" />
<property name="sarlc.linux.output.full"
value="${project.build.directory}/sarlc" />
<copy overwrite="true" file="${sarlc.linux.input}"
tofile="${sarlc.linux.output}" />
<replace file="${sarlc.linux.output}"
token="{cliCompilerMainClass}" value="${cliCompilerMainClass}" />
<replace file="${sarlc.linux.output}"
token="{project.build.finalName}"
value="${project.build.finalName}" />
<concat dest="${sarlc.linux.output.full}" binary="yes">
<fileset file="${sarlc.linux.output}" />
Expand Down
130 changes: 130 additions & 0 deletions products/sarlc/src/main/java/io/sarl/lang/sarlc/BootiqueSarlcMain.java
@@ -0,0 +1,130 @@
/*
* $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;

import java.util.List;

import com.google.common.base.Throwables;
import com.google.inject.Injector;
import com.google.inject.ProvisionException;
import io.bootique.BQRuntime;
import io.bootique.Bootique;
import io.bootique.command.CommandOutcome;
import io.bootique.help.HelpOption;
import io.bootique.help.HelpOptions;
import io.bootique.meta.application.ApplicationMetadata;
import org.apache.log4j.Logger;

import io.sarl.lang.SARLStandaloneSetup;
import io.sarl.lang.sarlc.modules.commands.CompilerCommandModuleProvider;
import io.sarl.lang.sarlc.modules.commands.VersionCommandModuleProvider;
import io.sarl.lang.sarlc.modules.configs.CompilerConfigModuleProvider;
import io.sarl.lang.sarlc.modules.configs.SarlcConfigModuleProvider;
import io.sarl.lang.sarlc.modules.configs.ValidatorConfigModuleProvider;
import io.sarl.lang.sarlc.modules.general.SARLRuntimeModuleProvider;
import io.sarl.lang.sarlc.modules.general.SarlBatchCompilerModuleProvider;
import io.sarl.lang.sarlc.modules.general.SarlcApplicationModuleProvider;
import io.sarl.lang.sarlc.modules.general.SarlcDefaultCommandModuleProvider;

/** Class that implements the standard main function for running a SARL application
* with bootique modules.
*
* @author $Author: sgalland$
* @version $FullVersion$
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
* @since 0.8
*/
public class BootiqueSarlcMain {

/** Create the compiler runtime.
*
* @param args the command line arguments.
* @return the runtime.
*/
@SuppressWarnings("static-method")
protected BQRuntime createRuntime(String... args) {
SARLStandaloneSetup.doPreSetup();
Bootique bootique = Bootique.app(args);
// Configuration modules
bootique.module(new SarlcConfigModuleProvider())
.module(new CompilerConfigModuleProvider())
.module(new ValidatorConfigModuleProvider());
// Command modules
bootique.module(new VersionCommandModuleProvider())
.module(new CompilerCommandModuleProvider());
// Core modules
bootique.module(new SARLRuntimeModuleProvider())
.module(new SarlBatchCompilerModuleProvider())
.module(new SarlcDefaultCommandModuleProvider())
.module(new SarlcApplicationModuleProvider());
bootique = bootique.autoLoadModules();
final BQRuntime runtime = bootique.createRuntime();
SARLStandaloneSetup.doPostSetup(runtime.getInstance(Injector.class));
return runtime;
}

/** Run the batch compiler.
*
* <p>This function runs the compiler and exits with the return code.
*
* @param args the command line arguments.
* @return the exit code.
*/
public int runCompiler(String... args) {
try {
final BQRuntime runtime = createRuntime(args);
final CommandOutcome outcome = runtime.run();
return outcome.getExitCode();
} catch (ProvisionException exception) {
final Throwable ex = Throwables.getRootCause(exception);
if (ex != null) {
Logger.getRootLogger().error(ex.getLocalizedMessage());
} else {
Logger.getRootLogger().error(exception.getLocalizedMessage());
}
} catch (Throwable exception) {
Logger.getRootLogger().error(exception.getLocalizedMessage());
}
return Constants.ERROR_CODE;
}

/** Replies the options of the program.
*
* @return the options of the program.
*/
public List<HelpOption> getOptions() {
final BQRuntime runtime = createRuntime();
final ApplicationMetadata application = runtime.getInstance(ApplicationMetadata.class);
final HelpOptions helpOptions = new HelpOptions();

application.getCommands().forEach(c -> {
helpOptions.add(c.asOption());
c.getOptions().forEach(o -> helpOptions.add(o));
});

application.getOptions().forEach(o -> helpOptions.add(o));

return helpOptions.getOptions();
}

}
54 changes: 54 additions & 0 deletions products/sarlc/src/main/java/io/sarl/lang/sarlc/Constants.java
@@ -0,0 +1,54 @@
/*
* $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;

/** Constants for sarlc.
*
* @author $Author: sgalland$
* @version $FullVersion$
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
* @since 0.8
*/
public final class Constants {

/** Return code when failure.
*/
public static final int ERROR_CODE = 255;

/** Default name of the sarlc program.
*/
public static final String PROGRAM_NAME = "sarlc"; //$NON-NLS-1$

/** Name of the option for defining the output directory from SARL to Java, without the {@code -} and the {@code /} prefixes.
*/
public static final String SARL_OUTPUT_DIRECTORY_OPTION = "directory"; //$NON-NLS-1$

/** Name of the option for defining the output directory for the Java class files, without the {@code -} and the {@code /} prefixes.
*/
public static final String JAVA_OUTPUT_DIRECTORY_OPTION = "outputdir"; //$NON-NLS-1$

private Constants() {
//
}

}
114 changes: 10 additions & 104 deletions products/sarlc/src/main/java/io/sarl/lang/sarlc/Main.java
Expand Up @@ -23,31 +23,7 @@

import java.util.List;

import com.google.common.base.Throwables;
import com.google.inject.Injector;
import com.google.inject.ProvisionException;
import io.bootique.BQRuntime;
import io.bootique.Bootique;
import io.bootique.help.HelpOption;
import io.bootique.help.HelpOptions;
import io.bootique.meta.application.ApplicationMetadata;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;

import io.sarl.lang.SARLRuntimeModule;
import io.sarl.lang.SARLStandaloneSetup;
import io.sarl.lang.sarlc.configs.SarlcConfig;
import io.sarl.lang.sarlc.modules.CompilerModule;
import io.sarl.lang.sarlc.modules.LoggingModule;
import io.sarl.lang.sarlc.modules.PrintConfigModule;
import io.sarl.lang.sarlc.modules.SarlcModule;
import io.sarl.lang.sarlc.modules.ValidatorModule;
import io.sarl.lang.sarlc.modules.VersionModule;
import io.sarl.maven.bqextension.Constants;
import io.sarl.maven.bqextension.modules.ApplicationMetadataModule;
import io.sarl.maven.bqextension.modules.ApplicationMetadataUpdater;
import io.sarl.maven.bqextension.modules.DocumentedModuleProvider;

/** Main entry point for the SARL batch compiler.
*
Expand All @@ -64,106 +40,36 @@ private Main() {
}

/** Main program of the batch compiler.
*
* <p>This function never returns. It invokes {@link #runCompiler(String...)}
* and stop the JVM with the replied exist code.
*
* @param args the command line arguments.
* @see #runCompiler(String...)
*/
public static void main(String[] args) {
final int retCode = runCompiler(args);
final int retCode = createMainObject().runCompiler(args);
System.exit(retCode);
}

/** Create the compiler runtime.
*
* @param args the command line arguments.
* @return the runtime.
*/
protected static BQRuntime createRuntime(String... args) {
SARLStandaloneSetup.doPreSetup();
Bootique bootique = Bootique.app(args);
bootique = DocumentedModuleProvider.module(bootique,
SARLRuntimeModule.class, "The core of SARL runtime."); //$NON-NLS-1$
bootique = DocumentedModuleProvider.modules(bootique,
LoggingModule.class, PrintConfigModule.class,
VersionModule.class, ApplicationMetadataModule.class,
SarlcModule.class, CompilerModule.class, ValidatorModule.class);
bootique = bootique.autoLoadModules();
final BQRuntime runtime = bootique.createRuntime();
forceApplicationName(runtime);
SARLStandaloneSetup.doPostSetup(runtime.getInstance(Injector.class));
return runtime;
}

private static void forceApplicationName(BQRuntime runtime) {
final ApplicationMetadataUpdater updater = runtime.getInstance(ApplicationMetadataUpdater.class);
final ApplicationMetadata metadata = runtime.getInstance(ApplicationMetadata.class);
final SarlcConfig sarlcConfig = runtime.getInstance(SarlcConfig.class);
updater.setName(metadata, sarlcConfig.getCompilerProgramName());
}

/** Run the batch compiler.
*
* <p>This function runs the compiler and exits with the return code.
/** Replies the default name of the program.
*
* @param args the command line arguments.
* @return the exit code.
* @see #main(String[])
* @return the default name of the program.
*/
public static int runCompiler(String... args) {
configureLogger();
try {
final BQRuntime runtime = createRuntime(args);
runtime.run();
} catch (ProvisionException exception) {
final Throwable ex = Throwables.getRootCause(exception);
if (ex != null) {
Logger.getRootLogger().error(ex.getLocalizedMessage());
} else {
Logger.getRootLogger().error(exception.getLocalizedMessage());
}
return Constants.ERROR_CODE;
} catch (Throwable exception) {
Logger.getRootLogger().error(exception.getLocalizedMessage());
return Constants.ERROR_CODE;
}
return Constants.SUCCESS_CODE;
}

private static void configureLogger() {
final Logger root = Logger.getRootLogger();
root.removeAllAppenders();
root.addAppender(new ConsoleAppender(
new PatternLayout(Constants.LOGGER_PATTERN)));
public static String getDefaultCompilerProgramName() {
return Constants.PROGRAM_NAME;
}

/** Replies the default name of the program.
/** Create the instance of the bootique main launcher.
*
* @return the default name of the program.
* @return the main launcher.
*/
public static String getDefaultCompilerProgramName() {
return SarlcConfig.COMPILER_PROGRAM_VALUE;
protected static BootiqueSarlcMain createMainObject() {
return new BootiqueSarlcMain();
}

/** Replies the options of the program.
*
* @return the options of the program.
*/
public static List<HelpOption> getOptions() {
final BQRuntime runtime = createRuntime();
final ApplicationMetadata application = runtime.getInstance(ApplicationMetadata.class);
final HelpOptions helpOptions = new HelpOptions();

application.getCommands().forEach(c -> {
helpOptions.add(c.asOption());
c.getOptions().forEach(o -> helpOptions.add(o));
});

application.getOptions().forEach(o -> helpOptions.add(o));

return helpOptions.getOptions();
return createMainObject().getOptions();
}

}

0 comments on commit 2c53567

Please sign in to comment.