This Addon requires the following installation steps.
To use this addon, you must add it as a dependency in the pom.xml of your forge-addon classified artifact:
<dependency>
<groupId>org.jboss.forge.addon</groupId>
<artifactId>script</artifactId>
<classifier>forge-addon</classifier>
<version>${version}</version>
</dependency>- JSR-223 (javax.script) compatible
-
Invoke script using the javax.script API:
// Using ScriptEngineManager
ScriptEngineManager manager = new ScriptEngineManager(getClass().getClassLoader());
ScriptEngine engine = manager.getEngineByExtension("fsh");
// or fetching from the AddonRegistry
@Inject
ForgeScriptEngineFactory factory;
//and
ScriptEngine engine = factory.getScriptEngine();
// Ready to execute
ScriptContext scriptContext = ScriptContextBuilder.create().currentResource(currentResource).stdout(output.out()).stderr(output.err()).build();
Result result = (Result) engine.eval("touch foo.txt", scriptContext);- Support ScriptFileResource
-
You can use
ResourceFactoryto get a handle to aScriptFileResourceand evaluate it, be it a Forge script or any JSR-223 compatible implementation.
// it could be a JavaScript file or any other JSR-223 compatible engine.
ScriptFileResource scriptResource = resourceFactory.create(ScriptFileResource.class, new File("forge.fsh"));
ScriptContext context = ScriptContextBuilder.create().currentResource(tmpDir).build();
scriptResource.eval(context);