use of com.bluenimble.platform.icli.mgm.utils.JsTool in project serverless by bluenimble.
the class MacroSourceCommand method execute.
@Override
public CommandResult execute(final Tool tool, Map<String, CommandOption> options) throws CommandExecutionException {
InputStream input = null;
SimpleBindings bindings = new SimpleBindings();
String command = (String) tool.currentContext().get(ToolContext.CommandLine);
if (!Lang.isNullOrEmpty(command)) {
bindings.put("Command", command);
}
bindings.put("Home", BlueNimble.Work);
bindings.put("Config", BlueNimble.Config);
bindings.put("Tool", new JsTool(tool));
bindings.put(JavaClass, new Function<String, Class<?>>() {
@Override
public Class<?> apply(String type) {
try {
return MacroSourceCommand.class.getClassLoader().loadClass(type);
} catch (ClassNotFoundException cnfe) {
throw new RuntimeException(cnfe);
}
}
});
bindings.put("Vars", tool.getContext(Tool.ROOT_CTX).get(ToolContext.VARS));
Keys keys = BlueNimble.keys();
if (keys != null) {
bindings.put("Keys", keys.json());
}
try {
input = new FileInputStream(script);
List<InputStream> blocks = new ArrayList<InputStream>();
blocks.add(new ByteArrayInputStream(Native.getBytes()));
blocks.add(input);
Engine.eval(new InputStreamReader(new SequenceInputStream(Collections.enumeration(blocks))), bindings);
} catch (Exception e) {
throw new CommandExecutionException(e.getMessage(), e);
} finally {
IOUtils.closeQuietly(input);
}
return null;
}
Aggregations