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, and unpack

Constant Summary collapse

HELP_MESSAGE =
<<-EOS
  Version: #{RubyProcessing::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 | 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
    unpack:     unpack samples or library

  Common options:
    --nojruby:  do not use the installed version of jruby, instead use our vendored
          jarred one (required for shader sketches, and some others).
  
  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 640 480
    rp5 create some_new_sketch --p3d 640 480
    rp5 watch 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



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

def self.execute
  runner = self.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.



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

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

#create(sketch, args, p3d) ⇒ Object

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



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

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

#execute!Object

Dispatch central.



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

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.p3d)
  when 'app'    then app(@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.



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

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
88
# File 'lib/ruby-processing/runner.rb', line 80

def parse_options(args)
  @options = OpenStruct.new
  @options.p3d   = !!args.delete('--p3d')
  @options.jruby  = !!args.delete('--jruby')
  @options.nojruby  = !!args.delete('--nojruby')
  @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.



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

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

#show_helpObject

Show the standard help/usage message.



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

def show_help
  puts HELP_MESSAGE
end

#show_versionObject

Display the current version of Ruby-Processing.



130
131
132
# File 'lib/ruby-processing/runner.rb', line 130

def show_version
  puts "Ruby-Processing version #{RubyProcessing::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.



122
123
124
125
126
127
# File 'lib/ruby-processing/runner.rb', line 122

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.



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

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