|
| 1 | +# frozen_string_literal: true |
| 2 | +require_relative '../jruby_art/jruby_complete' |
| 3 | +require_relative '../jruby_art/java_opts' |
| 4 | +module Processing |
| 5 | + # The command class check for configuration and options, before creating and |
| 6 | + # executing the jruby (or java) command to run the sketch |
| 7 | + class Command |
| 8 | + attr_reader :runner, :args, :filename |
| 9 | + def initialize(runner:, args:, filename:) |
| 10 | + @runner = runner |
| 11 | + @args = args |
| 12 | + @filename = filename |
| 13 | + end |
| 14 | + |
| 15 | + # Trade in this Ruby instance for a JRuby instance, loading in a starter |
| 16 | + # script and passing it some arguments. Unless you set JRUBY: false in |
| 17 | + # ~/.jruby_art/config.yml, an installed version of jruby is used instead |
| 18 | + # of our vendored one. Note the use of jruby-complete might make using |
| 19 | + # other gems in your sketches hard (but not impossible).... |
| 20 | + def cmd(root) |
| 21 | + cmda = jruby_command(Processing::RP_CONFIG.fetch('JRUBY', true), root) |
| 22 | + begin |
| 23 | + puts *cmda |
| 24 | + exec(*cmda) |
| 25 | + # exec replaces the Ruby process with the JRuby one. |
| 26 | + rescue Java::JavaLang::ClassNotFoundException |
| 27 | + end |
| 28 | + end |
| 29 | + |
| 30 | + private |
| 31 | + |
| 32 | + # avoiding multiline ternary etc |
| 33 | + def jruby_command(installed, root) |
| 34 | + return ['jruby', JRubyOpts.new(root).opts, runner, filename, args].flatten if installed |
| 35 | + opts = JavaOpts.new(root).opts |
| 36 | + complete = JRubyComplete.complete |
| 37 | + ['java', opts, '-cp', complete, 'org.jruby.Main', runner, filename, args].flatten |
| 38 | + end |
| 39 | + end |
| 40 | +end |
0 commit comments