Skip to content
Draft
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public class JRubyScriptEngineConfiguration {
public static final Path HOME_PATH = Path.of("automation", "ruby");
public static final Path HOME_PATH_ABS = Path.of(OpenHAB.getConfigFolder()).resolve(HOME_PATH);
private static final Path DEFAULT_GEMFILE_PATH = HOME_PATH_ABS.resolve("Gemfile");
private static final Path BUNDLE_USER_HOME = Path.of(OpenHAB.getUserDataFolder(), "cache", "automation", "ruby");

private static final Logger LOGGER = LoggerFactory.getLogger(JRubyScriptEngineConfiguration.class);

Expand Down Expand Up @@ -84,6 +85,10 @@ public static class JRubyScriptingConfiguration {
private String specificGemHome = "";
private File bundleGemfile = DEFAULT_GEMFILE_PATH.toFile();

JRubyScriptEngineConfiguration() {
ensureDirectoryExists(BUNDLE_USER_HOME.toString(), "Ruby Bundler user path");
}

/**
* Update configuration
*
Expand Down Expand Up @@ -200,12 +205,15 @@ private boolean ensureGemHomeExists(String gemHome) {
return false;
}

File gemHomeDirectory = new File(gemHome);
if (!gemHomeDirectory.exists()) {
LOGGER.debug("gem_home directory '{}' does not exist, creating", gemHome);
if (!gemHomeDirectory.mkdirs()) {
LOGGER.warn("Error creating gem_home directory: {}", gemHome);
return false;
return ensureDirectoryExists(gemHome, "gem_home directory");
}

private boolean ensureDirectoryExists(String dir, String description) {
File directory = new File(dir);
if (!directory.exists()) {
LOGGER.debug("{} '{}' does not exist, creating", description, dir);
if (!directory.mkdirs()) {
LOGGER.warn("Error creating {}: {}", description, dir);
}
}
return true;
Expand Down Expand Up @@ -241,7 +249,7 @@ public File getGemfile() {

/**
* Run bundle install or update.
*
*
* This is to be called at start up or configuration change,
* so that gems are available when user scripts are run.
*
Expand All @@ -257,6 +265,8 @@ public void bundlerInit(ScriptEngine engine, boolean update) {
require "bundler"
require "bundler/cli"

Gem.instance_variable_set(:@user_home, ENV['BUNDLE_USER_HOME'])

Bundler::CLI.start(["%s"])
""".formatted(operation);

Expand Down Expand Up @@ -285,6 +295,8 @@ public void bundlerSetup(ScriptEngine engine) {
JRuby.runtime.instance_config.update_native_env_enabled = false
require "bundler"

Gem.instance_variable_set(:@user_home, ENV['BUNDLE_USER_HOME'])

Bundler.settings.temporary(auto_install: true) do
require "bundler/setup"
Bundler.require
Expand All @@ -302,7 +314,7 @@ public void bundlerSetup(ScriptEngine engine) {

/**
* Install a gems in ScriptEngine
*
*
* @param engine Engine to install gems
*/
synchronized void configureGems(ScriptEngine engine, boolean update) {
Expand Down Expand Up @@ -351,6 +363,8 @@ synchronized void configureGems(ScriptEngine engine, boolean update) {
require 'bundler/inline'
require 'openssl'

Gem.instance_variable_set(:@user_home, ENV['BUNDLE_USER_HOME'])

gemfile(%b) do
source 'https://rubygems.org/'
%s
Expand Down Expand Up @@ -415,7 +429,12 @@ public static void setEnvironmentVariable(ScriptEngine engine, String key, @Null
public void configureRubyEnvironment(ScriptEngine scriptEngine) {
setEnvironmentVariable(scriptEngine, "GEM_HOME", getSpecificGemHome());
setEnvironmentVariable(scriptEngine, "RUBYLIB", configuration.rubylib);
setEnvironmentVariable(scriptEngine, "BUNDLE_USER_HOME", BUNDLE_USER_HOME.toString());
if (bundleGemfile.exists()) {
Path bundleUserConfigDir = bundleGemfile.toPath().resolveSibling(".bundle");
ensureDirectoryExists(bundleUserConfigDir.toString(), "Ruby Bundler user config path");
setEnvironmentVariable(scriptEngine, "BUNDLE_USER_CONFIG",
bundleUserConfigDir.resolve("config").toString());
setEnvironmentVariable(scriptEngine, "BUNDLE_GEMFILE", bundleGemfile.toString());
}

Expand Down
Loading