[jrubyscripting] Apply RUBYLIB configuration to $LOAD_PATH ()

* [jrubyscripting] Apply RUBYLIB configuration to $LOAD_PATH

Signed-off-by: Jimmy Tanagra <jcode@tanagra.id.au>
pull/12158/head
jimtng 2022-01-29 20:55:09 +10:00 committed by GitHub
parent 5abea08da3
commit 4e122005ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 29 additions and 1 deletions
bundles/org.openhab.automation.jrubyscripting/src/main/java/org/openhab/automation/jrubyscripting/internal

View File

@ -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());
}
}
}
/**