Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[sarlc] Refactoring for using only Bootique injection.
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
1 parent
f8b5e07
commit 2c53567
Showing
40 changed files
with
1,550 additions
and
1,453 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
products/sarlc/src/main/java/io/sarl/lang/sarlc/BootiqueSarlcMain.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
54
products/sarlc/src/main/java/io/sarl/lang/sarlc/Constants.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() { | ||
// | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.