Class: Processing::Runner

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(out_stream = STDOUT, error_stream = STDERR) ⇒ Runner

Returns a new instance of Runner.



19
20
21
22
23
24
25
# File 'lib/processing.rb', line 19

def initialize(out_stream = STDOUT, error_stream = STDERR)
  @out_stream = out_stream
  @error_stream = error_stream
  @options = OpenStruct.new
  @options.action = :run
  @options.path = File.basename(Dir.pwd) + ".rb"
end

Class Method Details

.executeObject



12
13
14
15
16
# File 'lib/processing.rb', line 12

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

Instance Method Details

#create(project) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/processing.rb', line 89

def create(project)
  project.underscore!
  destination = File.join(Dir.pwd, project)
  begin
    FileUtils.mkdir(destination)
  rescue Exception => e
    exit_with_error("Can not overwrite an existing project.")
  end

  FileUtils.cp_r("#{TEMPLATE_DIR}/application.rb", "#{destination}/#{project}.rb")
  File.open(File.join(destination, "#{project}.rb"), "r+") do |f|
    lines = f.readlines
    lines.each {|l| l.gsub!(/Application/, project.camelize)}
    f.rewind
    f.print lines
    f.truncate(f.pos)
  end
end

#execute!Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/processing.rb', line 33

def execute!
  case @options.action
  when :run
    run(@options.path)
  when :watch
    watch(@options.path)
  when :create
    create(@options.path)
  end
end

#parse_options(args) ⇒ Object



27
28
29
30
31
# File 'lib/processing.rb', line 27

def parse_options(args)
  parse_options!(args)
rescue Exception => e
  exit_with_error(e)
end

#parse_options!(args) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/processing.rb', line 44

def parse_options!(args)
  args.extend(OptionParser::Arguable)
  args.options do |opts|
    opts.banner = "Usage: processing [options] [PATH]"

    opts.separator ""
    opts.separator "Creating a project:"

    opts.on("-c", "--create PROJECT", "Create a new Ruby-Processing PROJECT") do |project|
      @options.action = :create
      @options.path = project
    end

    # TODO: also let user extract ruby-processing files, library, ...
    # TODO: option to create data folder
    # TODO: option to create bare sketch without putting it into a directory

    opts.separator ""
    opts.separator "Running a project:"

    opts.on("-r", "--run FILE", "Run a Ruby-Processing FILE") do |file|
      @options.action = :run
      @options.path = file
    end

    opts.on("-w", "--watch FILE", "Run and update a Ruby-Processing sketch") do |file|
      @options.action = :watch
      @options.path = file
    end

    # TODO: let user watch files, live code etc.
    # TODO: also let user watch files, live-script, ...

    opts.separator ""
    opts.separator "Common options:"

    opts.on_tail("-v", "--version", "Show version") do
      exit_with_success("ruby-processing-gem version #{Processing::VERSION}")
    end
    opts.on_tail("-h", "--help", "Show this message") do
      exit_with_success(opts)
    end
  end.parse!
end

#run(file) ⇒ Object



108
109
110
111
# File 'lib/processing.rb', line 108

def run(file)
  prepare_sandbox(file)
  puts `./script/open #{file}`
end

#watch(file) ⇒ Object



113
114
115
116
# File 'lib/processing.rb', line 113

def watch(file)
  prepare_sandbox(file)
  puts `./script/watch #{file}`
end