[jrubyscripting] Apply RUBYLIB configuration to $LOAD_PATH (#12123)
* [jrubyscripting] Apply RUBYLIB configuration to $LOAD_PATH Signed-off-by: Jimmy Tanagra <jcode@tanagra.id.au>pull/12158/head
parent
5abea08da3
commit
4e122005ca
bundles/org.openhab.automation.jrubyscripting/src/main/java/org/openhab/automation/jrubyscripting/internal
|
@ -47,6 +47,7 @@ public class JRubyScriptEngineConfiguration {
|
|||
private static final Path DEFAULT_RUBYLIB = Paths.get(OpenHAB.getConfigFolder(), "automation", "lib", "ruby");
|
||||
|
||||
private static final String GEM_HOME = "gem_home";
|
||||
private static final String RUBYLIB = "rubylib";
|
||||
|
||||
// Map of configuration parameters
|
||||
private static final Map<String, OptionalConfigurationElement> CONFIGURATION_PARAMETERS = Map.ofEntries(
|
||||
|
@ -62,7 +63,7 @@ public class JRubyScriptEngineConfiguration {
|
|||
new OptionalConfigurationElement.Builder(OptionalConfigurationElement.Type.RUBY_ENVIRONMENT)
|
||||
.mappedTo("GEM_HOME").defaultValue(DEFAULT_GEM_HOME.toString()).build()),
|
||||
|
||||
Map.entry("rubylib",
|
||||
Map.entry(RUBYLIB,
|
||||
new OptionalConfigurationElement.Builder(OptionalConfigurationElement.Type.RUBY_ENVIRONMENT)
|
||||
.mappedTo("RUBYLIB").defaultValue(DEFAULT_RUBYLIB.toString()).build()),
|
||||
|
||||
|
@ -209,6 +210,33 @@ public class JRubyScriptEngineConfiguration {
|
|||
logger.debug("Ruby environment property ({}) has no value", environmentProperty);
|
||||
}
|
||||
}
|
||||
|
||||
configureRubyLib(engine);
|
||||
}
|
||||
|
||||
/**
|
||||
* Split up and insert ENV['RUBYLIB'] into Ruby's $LOAD_PATH
|
||||
* This needs to be called after ENV['RUBYLIB'] has been set by configureRubyEnvironment
|
||||
*
|
||||
* @param engine Engine in which to configure environment
|
||||
*/
|
||||
private void configureRubyLib(ScriptEngine engine) {
|
||||
OptionalConfigurationElement rubyLibConfigElement = CONFIGURATION_PARAMETERS.get(RUBYLIB);
|
||||
if (rubyLibConfigElement == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
Optional<String> rubyLib = rubyLibConfigElement.getValue();
|
||||
if (rubyLib.isPresent() && !rubyLib.get().trim().isEmpty()) {
|
||||
final String code = "$LOAD_PATH.unshift *ENV['RUBYLIB']&.split(File::PATH_SEPARATOR)" + //
|
||||
"&.reject(&:empty?)" + //
|
||||
"&.reject { |path| $LOAD_PATH.include?(path) }"; //
|
||||
try {
|
||||
engine.eval(code);
|
||||
} catch (ScriptException exception) {
|
||||
logger.warn("Error setting $LOAD_PATH from RUBYLIB='{}': {}", rubyLib.get(), exception.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue