Skip to content

Commit aeb4f90

Browse files
committed
[jrubyscripting] Support using Gemfile with Bundler
Signed-off-by: Jimmy Tanagra <[email protected]>
1 parent 95d3179 commit aeb4f90

File tree

6 files changed

+290
-180
lines changed

6 files changed

+290
-180
lines changed

bundles/org.openhab.automation.jrubyscripting/src/main/java/org/openhab/automation/jrubyscripting/internal/JRubyConsoleCommandExtension.java

+33-19
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,17 @@ public Object execute(Session session, List<Object> argList) throws Exception {
181181
}
182182

183183
private void info(Console console) {
184+
String gemfile = jRubyScriptEngineFactory.getConfiguration().getGemfilePath();
184185
final String PRINT_VERSION_NUMBERS = """
185186
library_version = defined?(OpenHAB::DSL::VERSION) && OpenHAB::DSL::VERSION
186187
187188
puts "JRuby #{JRUBY_VERSION}"
188189
puts "JRuby Scripting Library #{library_version || 'is not installed'}"
189-
puts "GEM_HOME: #{ENV['GEM_HOME']}"
190-
puts "RUBYLIB: #{ENV['RUBYLIB']}"
191-
""";
190+
puts "ENV['GEM_HOME']: #{ENV['GEM_HOME']}"
191+
puts "ENV['RUBYLIB']: #{ENV['RUBYLIB']}"
192+
""" + (gemfile == null ? "" : """
193+
puts "ENV['BUNDLE_GEMFILE']: #{ENV['BUNDLE_GEMFILE']}"
194+
""");
192195

193196
executeWithFullJRuby(console, engine -> engine.eval(PRINT_VERSION_NUMBERS));
194197
console.println("Script path: " + scriptFileWatcher.getWatchPath());
@@ -220,7 +223,14 @@ private void info(Console console) {
220223
}
221224
console.println(groupLabel);
222225
parameters.forEach(parameter -> {
223-
console.println(" " + parameter.getName() + ": " + config.get(parameter.getName()));
226+
console.print(" " + parameter.getName() + ": ");
227+
String value = config.get(parameter.getName());
228+
if (value.contains("\n")) {
229+
console.println(" (multiline)");
230+
console.println(" " + value.replace("\n", "\n "));
231+
} else {
232+
console.println(value);
233+
}
224234
});
225235
console.println("");
226236
});
@@ -303,7 +313,20 @@ private void startConsole(Console console, Session session, String[] args) {
303313
}
304314

305315
synchronized private void bundler(Console console, String[] args) {
316+
final String gemfilePath = jRubyScriptEngineFactory.getConfiguration().getGemfilePath();
317+
if (gemfilePath == null) {
318+
console.println(
319+
"No Gemfile configured. Please set the 'bundle_gemfile_path' or 'bundle_gemfile_content' property in the add-on configuration.");
320+
return;
321+
}
322+
323+
// we have to split this because we dont want to format the string with ruby '%w' in it
306324
final String BUNDLER = """
325+
require 'jruby'
326+
JRuby.runtime.instance_config.update_native_env_enabled = false
327+
ENV['BUNDLE_GEMFILE'] = '%s'
328+
""".formatted(gemfilePath) + """
329+
307330
require "bundler"
308331
require "bundler/friendly_errors"
309332
@@ -319,20 +342,11 @@ synchronized private void bundler(Console console, String[] args) {
319342
end
320343
""";
321344

322-
String originalDir = System.setProperty("user.dir", scriptFileWatcher.getWatchPath().toString());
323-
try {
324-
executeWithPlainJRuby(console, engine -> {
325-
engine.put(ScriptEngine.ARGV, args);
326-
engine.eval(BUNDLER);
327-
return null;
328-
});
329-
} finally {
330-
if (originalDir == null) {
331-
System.clearProperty("user.dir");
332-
} else {
333-
System.setProperty("user.dir", originalDir);
334-
}
335-
}
345+
executeWithPlainJRuby(console, engine -> {
346+
engine.put(ScriptEngine.ARGV, args);
347+
engine.eval(BUNDLER);
348+
return null;
349+
});
336350
}
337351

338352
synchronized private void gem(Console console, String[] args) {
@@ -488,7 +502,7 @@ private List<String> getUsages() {
488502
buildCommandUsage(INFO, "displays information about JRuby Scripting add-on"), //
489503
buildCommandUsage(CONSOLE + " [--list|-l|--help|-h] | [script] [options]",
490504
"starts an interactive JRuby console"), //
491-
buildCommandUsage(BUNDLE + " [arguments]", "runs Ruby bundler in the main Script path"), //
505+
buildCommandUsage(BUNDLE + " [arguments]", "runs Ruby bundler with the configured Gemfile"), //
492506
buildCommandUsage(GEM + " [arguments]", "manages JRuby Scripting add-on's RubyGems"), //
493507
buildCommandUsage(UPDATE, "updates the configured gems"), //
494508
buildCommandUsage(PRUNE + " [-f|--force]", "cleans up older versions in the .gem directory") //

0 commit comments

Comments
 (0)