Class: Processing::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-processing/runner.rb

Overview

Utility class to handle the different commands that the ‘rp5’ command offers. Able to run, watch, live, create, app, applet, and unpack

Constant Summary collapse

HELP_MESSAGE =
<<-EOS
  Version: #{Processing::VERSION}

  Ruby-Processing is a little shim between Processing and JRuby that helps
  you create sketches of code art.

  Usage:
    rp5 [run | watch | live | create [width height] | app | applet | unpack] path/to/sketch

    run:        run sketch once
    watch:      watch for changes on the file and relaunch it on the fly
    live:       launch sketch and give an interactive IRB shell
    create:     create new sketch. Use --bare to generate simpler sketches without a class
    app:        create an application version of the sketch
    applet:     create an applet version of the sketch
    unpack:     unpack samples or library

  Common options:
    --jruby:    passed, use the installed version of jruby, instead of
          our vendored jarred one (useful for gems).
  
  Configuration file:
    A YAML configuration file is located at #{Processing::CONFIG_FILE_PATH}
    
    Possible options are:

java_args:        pass additionnals arguments to Java VM upon launching. 
                  Useful for increasing available memory (for example:
                  -Xms256m -Xmx256m) or force 32 bits mode (-d32).
sketchbook_path:  specify Processing sketchbook path to load additionnal 
                  libraries

  Examples:
    rp5 unpack samples
    rp5 run samples/contributed/jwishy.rb
    rp5 create some_new_sketch --bare 640 480
    rp5 watch some_new_sketch.rb
    rp5 applet some_new_sketch.rb

  Everything Else:
    http://wiki.github.com/jashkenas/ruby-processing

EOS

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.executeObject

Start running a ruby-processing sketch from the passed-in arguments



56
57
58
59
60
# File 'lib/ruby-processing/runner.rb', line 56

def self.execute
  runner = new
  runner.parse_options(ARGV)
  runner.execute!
end

Instance Method Details

#app(sketch) ⇒ Object

Generate a cross-platform application of a given Ruby-Processing sketch.



115
116
117
# File 'lib/ruby-processing/runner.rb', line 115

def app(sketch)
  Processing::ApplicationExporter.new.export!(sketch)
end

#applet(sketch) ⇒ Object

Generate an applet and HTML page for a given sketch.



120
121
122
# File 'lib/ruby-processing/runner.rb', line 120

def applet(sketch)
  Processing::AppletExporter.new.export!(sketch)
end

#create(sketch, args, bare) ⇒ Object

Create a fresh Ruby-Processing sketch, with the necessary boilerplate filled out.



91
92
93
# File 'lib/ruby-processing/runner.rb', line 91

def create(sketch, args, bare)
  Processing::Creator.new.create!(sketch, args, bare)
end

#execute!Object

Dispatch central.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/ruby-processing/runner.rb', line 63

def execute!
  case @options.action
  when 'run'    then run(@options.path, @options.args)
  when 'watch'  then watch(@options.path, @options.args)
  when 'live'   then live(@options.path, @options.args)
  when 'create' then create(@options.path, @options.args, @options.bare)
  when 'app'    then app(@options.path)
  when 'applet' then applet(@options.path)
  when 'unpack' then unpack(@options.path)
  when /-v/     then show_version
  when /-h/     then show_help
  else
    show_help
  end
end

#live(sketch, args) ⇒ Object

Run a sketch, opening its guts to IRB, letting you play with it.



109
110
111
112
# File 'lib/ruby-processing/runner.rb', line 109

def live(sketch, args)
  ensure_exists(sketch)
  spin_up('live.rb', sketch, args)
end

#parse_options(args) ⇒ Object

Parse the command-line options. Keep it simple.



80
81
82
83
84
85
86
87
# File 'lib/ruby-processing/runner.rb', line 80

def parse_options(args)
  @options = OpenStruct.new
  @options.bare   = !!args.delete('--bare')
  @options.jruby  = !!args.delete('--jruby')
  @options.action = args[0]     || nil
  @options.path   = args[1]     || File.basename(Dir.pwd + '.rb')
  @options.args   = args[2..-1] || []
end

#run(sketch, args) ⇒ Object

Just simply run a ruby-processing sketch.



96
97
98
99
# File 'lib/ruby-processing/runner.rb', line 96

def run(sketch, args)
  ensure_exists(sketch)
  spin_up('run.rb', sketch, args)
end

#show_helpObject

Show the standard help/usage message.



139
140
141
# File 'lib/ruby-processing/runner.rb', line 139

def show_help
  puts HELP_MESSAGE
end

#show_versionObject

Display the current version of Ruby-Processing.



134
135
136
# File 'lib/ruby-processing/runner.rb', line 134

def show_version
  puts "Ruby-Processing version #{Processing::VERSION}"
end

#unpack(dir) ⇒ Object

Install the included samples to a given path, where you can run and alter them to your heart’s content.



126
127
128
129
130
131
# File 'lib/ruby-processing/runner.rb', line 126

def unpack(dir)
  require 'fileutils'
  usage = "Usage: rp5 unpack [samples | library]"
  puts usage and return unless dir.match(/\A(samples|library)\Z/)
  FileUtils.cp_r("#{RP5_ROOT}/#{dir}", "#{Dir.pwd}/#{dir}")
end

#watch(sketch, args) ⇒ Object

Run a sketch, keeping an eye on it’s file, and reloading whenever it changes.



103
104
105
106
# File 'lib/ruby-processing/runner.rb', line 103

def watch(sketch, args)
  ensure_exists(sketch)
  spin_up('watch.rb', sketch, args)
end