Sha256: 1cd0dbf7345f868a647cdc8c361438b49d464719bbebb2b40c840254c0d27097

Contents?: true

Size: 1.76 KB

Versions: 2

Compression:

Stored size: 1.76 KB

Contents

$LOAD_PATH << File.expand_path(File.dirname(__FILE__) + "/../../")
SKETCH_ROOT = File.dirname(ARGV[0]) unless defined? SKETCH_ROOT

require 'ruby-processing'
require 'ruby-processing/app'


module Processing
  
  SKETCH_PATH = ARGV[0]
  
  # For use with "bare" sketches that don't want to define a class or methods
  SKETCH_TEMPLATE = <<-EOS
    class Sketch < Processing::App
      <% if has_methods %>
      <%= source %>
      <% else %>
      def setup
        size(DEFAULT_WIDTH, DEFAULT_HEIGHT, JAVA2D)
        <%= source %>
        no_loop
      end
      <% end %>
    end
  EOS
  
  # This method is the common entry point to run a sketch, bare or complete.
  def self.load_and_run_sketch
    source = self.read_sketch_source
    has_sketch = !!source.match(/^[^#]*< Processing::App/)
    has_methods = !!source.match(/^[^#]*(def\s+setup|def\s+draw)/)
    
    if has_sketch
      load Processing::SKETCH_PATH
      Processing::App.sketch_class.new if !$app
      return
    else
      require 'erb'
      code = ERB.new(SKETCH_TEMPLATE).result(binding)
      Object.class_eval code, Processing::SKETCH_PATH, 0
      Processing::App.sketch_class.new if !$app
    end
  end
  
  
  # Read in the sketch source code. Needs to work both online and offline.
  def self.read_sketch_source
    if Processing.online?
      # Fuck the following lines. Fucking Java can go sit on broken glass.
      source = ''
      url = java.net.URL.new(JRUBY_APPLET.get_code_base, Processing::SKETCH_PATH)
      input = java.io.BufferedReader.new(java.io.InputStreamReader.new(url.open_stream))
      while line = input.read_line do
        source << (line + "\n") if line
      end
      input.close
    else
      # Ahhh, much better.
      source = File.read(Processing::SKETCH_PATH)
    end
    source
  end
  
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ruby-processing-1.0.7 lib/ruby-processing/runners/base.rb
ruby-processing-1.0.8 lib/ruby-processing/runners/base.rb