Skip to content

Commit 75ae2c4

Browse files
committed
re-factor a bit
1 parent a1377a2 commit 75ae2c4

2 files changed

Lines changed: 23 additions & 17 deletions

File tree

lib/jruby_art/command.rb

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,37 @@
22
require_relative '../jruby_art/jruby_complete'
33
require_relative '../jruby_art/java_opts'
44
module Processing
5-
# This is class wrapper for building the command
5+
# The command class check for configuration and options, before creating and
6+
# executing the jruby (or java) command to run the sketch
67
class Command
7-
attr_reader :executable, :runner, :args, :filename
8-
def initialize(executable:, runner:, args:, filename:)
9-
@executable = executable
8+
attr_reader :runner, :args, :filename
9+
def initialize(runner:, args:, filename:)
1010
@runner = runner
1111
@args = args
1212
@filename = filename
1313
end
1414

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)....
1520
def cmd(root)
16-
if executable =~ /jruby/
17-
cmda = [executable, JRubyOpts.new(root).opts, runner, filename, args].flatten
18-
else
19-
cmda = [executable, JavaOpts.new(root).opts, '-cp', JRubyComplete.complete, 'org.jruby.Main', runner, filename, args].flatten
20-
end
21+
cmda = jruby_command(Processing::RP_CONFIG.fetch('JRUBY', true), root)
2122
begin
22-
# exec(*command)
2323
exec(*cmda)
2424
# exec replaces the Ruby process with the JRuby one.
2525
rescue Java::JavaLang::ClassNotFoundException
2626
end
2727
end
28+
29+
private
30+
31+
# avoiding multiline ternary etc
32+
def jruby_command(installed, root)
33+
opts = JRubyOpts.new(root).opts
34+
return ['jruby', opts, runner, filename, args].flatten if installed
35+
['java', opts, '-cp', JRubyComplete.complete, 'org.jruby.Main', runner, filename, args].flatten
36+
end
2837
end
2938
end

lib/jruby_art/runner.rb

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,15 +156,12 @@ def show_version
156156

157157
private
158158

159-
# Trade in this Ruby instance for a JRuby instance, loading in a starter
160-
# script and passing it some arguments. Unless you set JRUBY: false in
161-
# ~/.jruby_art/config.yml, an installed version of jruby is used instead
162-
# of our vendored one. Note the use of jruby-complete might make using
163-
# other gems in your sketches hard (but not impossible)....
159+
# We now build and execute the command arguments in the Command class.
160+
# Here we only need to supply the starter script, filename and args if any,
161+
# the Command class checks config (is executable java or jruby?)
162+
# and for any options in java_args.txt or config
164163
def spin_up(starter_script, filename, argc)
165-
executable = Processing::RP_CONFIG.fetch('JRUBY', true) ? 'jruby' : 'java'
166164
build = Command.new(
167-
executable: executable,
168165
runner: "#{K9_ROOT}/lib/jruby_art/runners/#{starter_script}",
169166
args: argc,
170167
filename: filename

0 commit comments

Comments
 (0)