Skip to content

Commit

Permalink
realese early and often
Browse files Browse the repository at this point in the history
  • Loading branch information
BuildTools committed Oct 3, 2021
1 parent 5871dd8 commit a565b76
Show file tree
Hide file tree
Showing 20 changed files with 784 additions and 65 deletions.
20 changes: 18 additions & 2 deletions scripted-jmelange/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,49 +55,65 @@
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
<!--optional>true</optional-->
</dependency>
<dependency>
<groupId>org.mozilla</groupId>
<artifactId>rhino</artifactId>
<version>1.7.11</version>
<version>${version.rhino}</version>
<!--optional>true</optional-->
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-jsr223</artifactId>
<version>${version.groovy}</version>
<!--optional>true</optional-->
</dependency>
<dependency>
<groupId>org.python</groupId>
<artifactId>jython-standalone</artifactId>
<version>${version.jython}</version>
<!--optional>true</optional-->
</dependency>
<!-- https://mvnrepository.com/artifact/org.jruby/jruby -->
<dependency>
<groupId>org.jruby</groupId>
<artifactId>jruby</artifactId>
<version>${version.jruby}</version>
<!--optional>true</optional-->
</dependency>


<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-templates</artifactId>
<version>${version.groovy}</version>
<!--optional>true</optional-->
</dependency>
<dependency>
<groupId>com.hubspot.jinjava</groupId>
<artifactId>jinjava</artifactId>
<version>2.5.6</version>
<!--optional>true</optional-->
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.11.RELEASE</version>
<!--optional>true</optional-->
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.30</version>
<!--optional>true</optional-->
</dependency>
<dependency>
<groupId>org.trimou</groupId>
<artifactId>trimou-core</artifactId>
<version>2.5.1.Final</version>
<!--optional>true</optional-->
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public abstract class AbstractScript
File sourceFile;
File destinationFile;
List<VariableProvider> variableProviders;
boolean bindScope = false;

@SneakyThrows
public boolean init(File _script, List<File> _inc)
Expand Down Expand Up @@ -71,64 +72,87 @@ public Map<String, Object> assembleContext()

if(this.getScriptFile()!=null)
{
_ret.put("_id", GuidUtil.toUUID(this.getScriptFile().getAbsolutePath()));
_ret.put(SCRIPT_ID_VAR, GuidUtil.toUUID(this.getScriptFile().getAbsolutePath()));
}
else
if(this.getSourceFile()!=null)
{
_ret.put("_id", GuidUtil.toUUID(this.getSourceFile().getAbsolutePath()));
_ret.put(SCRIPT_ID_VAR, GuidUtil.toUUID(this.getSourceFile().getAbsolutePath()));
}
else
{
_ret.put("_id", GuidUtil.randomUUID());
_ret.put(SCRIPT_ID_VAR, GuidUtil.randomUUID());
}

if(this.getSourceFile()!=null)
{
_ret.put("_fin", this.getSourceFile());
_ret.put("_finpath", this.getSourceFile().getAbsolutePath());
_ret.put("_findir", this.getSourceFile().getParent());
_ret.put(SCRIPT_INPUT_FILE_VAR, this.getSourceFile());
_ret.put(SCRIPT_INPUT_FILEPATH_VAR, this.getSourceFile().getAbsolutePath());
_ret.put(SCRIPT_INPUT_FILEDIR_VAR, this.getSourceFile().getParent());
}

if(this.getDestinationFile()!=null)
{
_ret.put("_fout", this.getDestinationFile());
_ret.put("_foutpath", this.getDestinationFile().getAbsolutePath());
_ret.put("_foutdir", this.getDestinationFile().getParent());
_ret.put(SCRIPT_OUTPUT_FILE_VAR, this.getDestinationFile());
_ret.put(SCRIPT_OUTPUT_FILEPATH_VAR, this.getDestinationFile().getAbsolutePath());
_ret.put(SCRIPT_OUTPUT_FILEDIR_VAR, this.getDestinationFile().getParent());
}

if(this.getInputStream()!=null) _ret.put("_sin", this.getInputStream());
if(this.getOutputStream()!=null) _ret.put("_sout", this.getOutputStream());
if(this.getInputStream()!=null) _ret.put(SCRIPT_INPUT_STREAM_VAR, this.getInputStream());
if(this.getOutputStream()!=null) _ret.put(SCRIPT_OUTPUT_STREAM_VAR, this.getOutputStream());
if(this.getErrorLogger()!=null) {
_ret.put("_log", this.getErrorLogger());
_ret.put(SCRIPT_LOGGER_VAR, this.getErrorLogger());
} else {
_ret.put("_log", log);
_ret.put(SCRIPT_LOGGER_VAR, log);
}

if(this.getArgs()!=null) {
_ret.put("_args", this.getArgs());
_ret.put(SCRIPT_ARGUMENTS_VAR, this.getArgs());
} else {
_ret.put("_args", Collections.emptyList());
_ret.put(SCRIPT_ARGUMENTS_VAR, Collections.emptyList());
}

if(this.getVars()!=null) {
_ret.put("_vars", this.getVars());
_ret.put(SCRIPT_VARIABLES_VAR, this.getVars());
} else {
_ret.put("_vars", Collections.emptyMap());
_ret.put(SCRIPT_VARIABLES_VAR, Collections.emptyMap());
}

_ret.put("_output_type", this.getOutputType().toLowerCase());
_ret.put("_ctx", _ret);
_ret.put("_", ScriptHelper.create());
_ret.put("_result", 0);
_ret.put(SCRIPT_OUTPUT_TYPE_VAR, this.getOutputType().toLowerCase());
_ret.put(SCRIPT_CONTEXT_VAR, _ret);
ScriptHelper _h = ScriptHelper.create();
_ret.put(SCRIPT_HELPER_VAR, _h);
_ret.put(SCRIPT_HELPER_VAR_ALT, _h);
_ret.put(SCRIPT_RESULT_VAR, 0);
return _ret;
}

public abstract Object executeObject();
public static final String SCRIPT_ID_VAR = "_id";

public static final String SCRIPT_INPUT_FILE_VAR = "_fin";
public static final String SCRIPT_INPUT_FILEPATH_VAR = "_finpath";
public static final String SCRIPT_INPUT_FILEDIR_VAR = "_findir";

public static final String SCRIPT_OUTPUT_FILE_VAR = "_fout";
public static final String SCRIPT_OUTPUT_FILEPATH_VAR = "_foutpath";
public static final String SCRIPT_OUTPUT_FILEDIR_VAR = "_foutdir";

public static final String SCRIPT_INPUT_STREAM_VAR = "_sin";
public static final String SCRIPT_OUTPUT_STREAM_VAR = "_sout";
public static final String SCRIPT_LOGGER_VAR = "_log";
public static final String SCRIPT_ARGUMENTS_VAR = "_args";
public static final String SCRIPT_VARIABLES_VAR = "_vars";
public static final String SCRIPT_OUTPUT_TYPE_VAR = "_output_type";
public static final String SCRIPT_HELPER_VAR = "_";
public static final String SCRIPT_HELPER_VAR_ALT = "_H";
public static final String SCRIPT_CONTEXT_VAR = "_ctx";
public static final String SCRIPT_RESULT_VAR = "_result";

public abstract Object executeObject(boolean _scopeOrBinding);

@SneakyThrows
public boolean execute()
{
return CommonUtil.checkBoolean(executeObject());
return CommonUtil.checkBoolean(executeObject(this.bindScope));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.github.terefang.jmelange.scripted.factory;

import com.github.terefang.jmelange.commons.CommonUtil;
import com.github.terefang.jmelange.scripted.AbstractScript;
import com.github.terefang.jmelange.scripted.JMelangeScriptFactory;
import com.github.terefang.jmelange.scripted.impl.JRubyScript;

import java.util.List;

public class JRubyScriptFactory implements JMelangeScriptFactory {
@Override
public String getName() {
return "jruby";
}

static List<String> SCRIPTNAMES = CommonUtil.toList("jruby", "ruby");

static List<String> SCRIPTEXTS = CommonUtil.toList(".jrb", ".rb");

@Override
public List<String> getScriptNames() {
return SCRIPTNAMES;
}

@Override
public List<String> getScriptExts() {
return SCRIPTEXTS;
}

@Override
public AbstractScript createScript() {
JRubyScript _scp = JRubyScript.create();
return _scp;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
import com.github.terefang.jmelange.commons.CommonUtil;
import com.github.terefang.jmelange.scripted.AbstractScript;
import com.github.terefang.jmelange.scripted.JMelangeScriptFactory;
import com.github.terefang.jmelange.scripted.impl.Jsr223Script;
import com.github.terefang.jmelange.scripted.impl.JythonScript;
import org.python.jsr223.PyScriptEngineFactory;

import java.util.List;

Expand All @@ -32,7 +30,6 @@ public List<String> getScriptExts() {
@Override
public AbstractScript createScript() {
JythonScript _scp = JythonScript.create();
_scp.setScriptEngine(new PyScriptEngineFactory().getScriptEngine());
return _scp;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.github.terefang.jmelange.scripted.impl;

import com.github.terefang.jmelange.scripted.AbstractScript;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.jruby.CompatVersion;
import org.jruby.RubyInstanceConfig;
import org.jruby.embed.EmbedEvalUnit;
import org.jruby.embed.LocalContextScope;
import org.jruby.embed.LocalVariableBehavior;
import org.jruby.embed.ScriptingContainer;
import org.jruby.embed.internal.BiVariableMap;

import java.io.PrintStream;
import java.io.Reader;
import java.util.Map;

@Slf4j
public class JRubyScript extends AbstractScript
{
public static JRubyScript create() { return new JRubyScript(); }
private ScriptingContainer sinstance;
private EmbedEvalUnit scriptCode;

@Override
@SneakyThrows
public boolean init(Reader _script)
{
try
{
this.sinstance = new ScriptingContainer(LocalContextScope.THREADSAFE, LocalVariableBehavior.PERSISTENT);
this.sinstance.setCompileMode(RubyInstanceConfig.CompileMode.FORCE);
this.sinstance.setCompatVersion(CompatVersion.RUBY2_1);
this.scriptCode = this.sinstance.parse(_script, this.getScriptFile().getName(), 0);
}
catch (Exception xe)
{
log.warn("error in file", xe);
}
finally
{
return (this.scriptCode != null);
}
}

@Override
public Object executeObject(boolean _scopeOrBindings)
{
try
{

synchronized (this.sinstance)
{
this.sinstance.clear();

if(this.getArgs()!=null) this.sinstance.setArgv(this.getArgs().toArray(new String[this.getArgs().size()]));

if(this.getInputStream()!=null) this.sinstance.setInput(this.getInputStream());
if(this.getOutputStream()!=null) this.sinstance.setOutput(new PrintStream(this.getOutputStream()));

for (Map.Entry<String, Object> e : this.assembleContext().entrySet()) {
this.sinstance.put("$"+e.getKey(), e.getValue());
}

BiVariableMap _vars = this.sinstance.getVarMap();

Object _ret = this.scriptCode.run();

return _ret;
}
}
catch (Exception xe)
{
log.warn("error in file", xe);
return null;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,15 @@ public boolean init(Reader _script)
{
this.scriptEngine.put(ScriptEngine.FILENAME, (this.getScriptFile()==null) ? "null": this.getScriptFile().getAbsolutePath());

this.script = IOUtil.toString(_script);

if(this.scriptEngine instanceof Compilable)
{
return (this.compiledScript = ((Compilable)this.scriptEngine).compile(_script)) != null;
}
else
{
return CommonUtil.isNotBlank(this.script = IOUtil.toString(_script));
return CommonUtil.isNotBlank(this.script);
}
}
finally
Expand All @@ -96,7 +98,7 @@ public boolean init(Reader _script)

@Override
@SneakyThrows
public Object executeObject()
public Object executeObject(boolean _scopeOrBinding)
{
Bindings scriptEngineBindings = this.scriptEngine.createBindings();
for(Map.Entry<String, Object> _entry : this.assembleContext().entrySet())
Expand All @@ -119,9 +121,15 @@ public Object executeObject()
_context.setBindings(scriptEngineBindings, ScriptContext.GLOBAL_SCOPE);

Object _res = null;
if(this.compiledScript != null)

if((this.compiledScript != null) && _scopeOrBinding)
{
_res = this.compiledScript.eval(_context);
}
else
if((this.compiledScript != null) && !_scopeOrBinding)
{
_res = this.compiledScript.eval((_context));
_res = this.compiledScript.eval(scriptEngineBindings);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public boolean init(Reader _script) {
}

@Override
public Object executeObject() {
public Object executeObject(boolean _scopeOrBindings)
{
Context l_engine = Context.enter();
try {
l_engine.setLanguageVersion(Context.VERSION_ES6);
Expand Down
Loading

0 comments on commit a565b76

Please sign in to comment.