public
Description: Code as Art, Art as Code. Processing and Ruby are meant for each other.
Homepage: http://github.com/jashkenas/ruby-processing/wikis
Clone URL: git://github.com/jashkenas/ruby-processing.git
Ruby-Processing 0.8 coming on through. Refactored export scripts.

Changelog:

Revision 0.8
  * Ruby-Processing can now export Mac applications! Running
    script/application my_sketch.rb will create MySketch.app,
    complete with all of its data and libraries. If you have
    a .icns file inside of your data folder, it will become
    the app's icon.

  * Added a mathematical Fern sample. It's a port of Luis
    Correia's java original, with algorithms from Wikipedia.

  * Sketches now have a library_loaded? method, so that you can
    check if a library has been started successfully, and
    conditionally enable things. (Good for OpenGL.)

  * The Boids library is now about 40% faster. It also comes with
    an example in library/boids/samples.

  * Specs have been started both for exporting and for Ruby-
    Processing itself.
jashkenas (author)
Sat Apr 19 08:52:37 -0700 2008
commit  cab92ab62fdd247c9323e7b4c3cc3faafe245a89
tree    4b5b7d4f970529d62b4abb97e7fe848399b772fd
parent  07e3f3843a27211a348a7331cefa3ab2572ad7a2
0
...
25
26
27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
29
30
...
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
0
@@ -25,6 +25,26 @@ them on the fly. Live coding, anyone?
0
 
0
 Changelog:
0
 
0
+Revision 0.8
0
+ * Ruby-Processing can now export Mac applications! Running
0
+ script/application my_sketch.rb will create MySketch.app,
0
+ complete with all of its data and libraries. If you have
0
+ a .icns file inside of your data folder, it will become
0
+ the app's icon.
0
+
0
+ * Added a mathematical Fern sample. It's a port of Luis
0
+ Correia's java original, with algorithms from Wikipedia.
0
+
0
+ * Sketches now have a library_loaded? method, so that you can
0
+ check if a library has been started successfully, and
0
+ conditionally enable things. (Good for OpenGL.)
0
+
0
+ * The Boids library is now about 40% faster. It also comes with
0
+ an example in library/boids/samples.
0
+
0
+ * Specs have been started both for exporting and for Ruby-
0
+ Processing itself.
0
+
0
 Revision 0.7
0
   * Thanks to MenTaLguY, once again, for work on the JRubyApplet, OpenGL
0
     is now a first-class citizen. If you're using OpenGL in your sketch,
...
2
3
4
5
 
6
7
8
...
2
3
4
 
5
6
7
8
0
@@ -2,7 +2,7 @@
0
 # Send suggestions, ideas, and hate-mail to jashkenas [at] gmail.com
0
 # Also, send samples and libraries.
0
 #
0
-# Revision 0.7
0
+# Revision 0.8
0
 # - omygawshkenas
0
 
0
 require 'java'
...
 
1
2
3
...
31
32
33
34
35
 
36
...
1
2
3
4
...
32
33
34
 
35
36
37
0
@@ -1,3 +1,4 @@
0
+# Description:
0
 # This full-screen demo can demonstrate the kinds of speedups
0
 # that are possible with OpenGL-accelerated rendering. If you
0
 # have the OpenGL library installed, you'll get *much*
0
@@ -31,4 +32,4 @@ class FullScreen < Processing::App
0
   end
0
 end
0
 
0
-FullScreen.new(:full_screen => true, :title => "Full Screen")
0
\ No newline at end of file
0
+FullScreen.new(:full_screen => true, :title => "Full Screen", :width => 600, :height => 600)
0
\ No newline at end of file
...
11
12
13
14
 
15
16
17
...
24
25
26
27
 
 
28
29
30
 
31
32
33
...
36
37
38
39
 
40
41
42
...
52
53
54
55
56
57
58
 
 
59
60
61
62
63
64
65
66
...
11
12
13
 
14
15
16
17
...
24
25
26
 
27
28
29
30
 
31
32
33
34
...
37
38
39
 
40
41
42
43
...
53
54
55
 
 
 
 
56
57
58
 
 
59
 
60
61
62
0
@@ -11,7 +11,7 @@ module Processing
0
     def export!
0
 
0
       # Check to make sure that the main file exists
0
- @main_file_path, main_file = *get_main_file()
0
+ @main_file_path, @main_file = *get_main_file()
0
       unless @main_file_path && File.exists?(@main_file_path)
0
         puts <<-USAGE
0
         
0
@@ -24,10 +24,11 @@ module Processing
0
       end
0
       
0
       # Extract all the cool details.
0
- source_code, class_name, title, width, height, description, libs_to_load = *extract_information()
0
+ @info = extract_information
0
+ hash_to_ivars @info
0
       
0
       # Make the appropriate directory
0
- applet_dir = "applets/#{main_file.sub(".rb", "")}"
0
+ applet_dir = "applets/#{@main_file.sub(".rb", "")}"
0
       remove_entry_secure applet_dir if File.exists?(applet_dir)
0
       mkdir_p applet_dir
0
       
0
@@ -36,7 +37,7 @@ module Processing
0
       necessary_files << "data" if File.exists?("data")
0
       necessary_files += Dir.glob("script/base_files/{*,**}") # Base files
0
       necessary_files += Dir.glob("script/applet_files/{*,**}") # Exporter files
0
- necessary_files += Dir.glob("library/{#{libs_to_load.join(",")}}") if libs_to_load.length > 0
0
+ necessary_files += Dir.glob("library/{#{@libs_to_load.join(",")}}") if @libs_to_load.length > 0
0
       cp_r(necessary_files, applet_dir)
0
       
0
       # Figure out OpenGL replacements, if necessary:
0
@@ -52,15 +53,10 @@ module Processing
0
       
0
       # Figure out the substitutions to make.
0
       file_list = Dir.glob(applet_dir + "{/**/*.{rb,jar},/data/*.*}").map {|f| f.sub(applet_dir+"/","")}
0
- h1 = title ? "<h1>#{title[1]}</h1>" : ""
0
- description = description ? "<div id='description'>" + h1 + "<p>" + description[1].gsub!("\n #", "") + "</p></div>" : ""
0
- @title = title ? title[1] : "Ruby-Processing Sketch"
0
- @width = width ? width[1] : "400"
0
+ h1 = "<h1>#{@title}</h1>"
0
+ @description = "<div id='description'>" + h1 + "<p>" + @description.gsub!("\n #", "") + "</p></div>"
0
       @width_plus_14 = (@width.to_i + 14).to_s
0
- @height = height ? height[1] : "400"
0
- @main_file = main_file
0
       @file_list = file_list.join(",")
0
- @description = description
0
       
0
       # Fill in the blanks in the HTML.
0
       render_erb_in_path_with_binding(applet_dir, binding, :delete => true)
...
20
21
22
23
24
25
26
27
28
29
30
31
32
 
33
34
35
36
 
 
37
38
39
...
47
48
49
50
 
51
52
53
...
63
64
65
 
 
 
 
 
 
 
 
 
 
 
 
 
66
67
68
...
20
21
22
 
 
 
 
 
 
 
 
 
 
23
24
25
 
 
26
27
28
29
30
...
38
39
40
 
41
42
43
44
...
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
0
@@ -20,20 +20,11 @@ module Processing
0
         ERROR
0
         exit
0
       end
0
- unless @main_file_path && File.exists?(@main_file_path)
0
- puts <<-USAGE
0
-
0
- The application exporter will generate a Mac application for you.
0
- Usage: script/application <path_to_sketch>
0
- Example: script/applet samples/jwishy.rb
0
-
0
- USAGE
0
- exit
0
- end
0
+ usage( @main_file_path && File.exists?(@main_file_path) )
0
       
0
       # Extract all the cool details.
0
- source_code, @class_name, title, width, height, description, libs_to_load = *extract_information()
0
- @title = title ? title[1] : "Ruby-Processing Sketch"
0
+ @info = extract_information
0
+ hash_to_ivars @info
0
       
0
       # Make the appropriate directory
0
       app_dir = "applications/#{@title}.app"
0
@@ -47,7 +38,7 @@ module Processing
0
       necessary_files << "data" if File.exists?("data")
0
       necessary_files += Dir.glob("script/base_files/{*,**}") # Base files
0
       cp_r(necessary_files, File.join(app_dir, prefix))
0
- library_files = Dir.glob("library/{#{libs_to_load.join(",")}}") if libs_to_load.length > 0
0
+ library_files = Dir.glob("library/{#{@libs_to_load.join(",")}}") if @libs_to_load.length > 0
0
       cp_r(library_files, File.join(app_dir, prefix, "library")) if library_files
0
       
0
       # Move the icon
0
@@ -63,6 +54,19 @@ module Processing
0
       rm Dir.glob(app_dir + "/**/*.{class,java}")
0
       
0
     end
0
+
0
+ def usage(predicate)
0
+ unless predicate
0
+ puts <<-USAGE
0
+
0
+ The application exporter will generate a Mac application for you.
0
+ Usage: script/application <path_to_sketch>
0
+ Example: script/applet samples/jwishy.rb
0
+
0
+ USAGE
0
+ exit
0
+ end
0
+ end
0
   end
0
 end
0
 
...
14
15
16
17
18
19
20
21
22
 
 
 
 
 
 
 
23
24
 
25
26
27
28
29
30
31
 
32
33
34
35
36
 
 
 
 
 
 
 
37
38
39
40
41
42
 
43
44
45
46
47
 
 
 
 
48
49
50
...
14
15
16
 
 
 
 
 
 
17
18
19
20
21
22
23
24
 
25
26
27
28
29
30
31
 
32
33
34
35
36
 
37
38
39
40
41
42
43
44
45
46
47
48
 
49
50
51
52
53
54
55
56
57
58
59
60
61
0
@@ -14,37 +14,48 @@ module Processing
0
     
0
     def extract_information
0
       # Extract information from main file
0
- source_code = File.open(@main_file_path, "r") {|file| file.readlines.join(" ")}
0
- class_name = source_code.match(/(\w+)\s*<\s*Processing::App/)[1]
0
- title = source_code.match(/#{class_name}\.new.*?:title\s=>\s["'](.+)["']/m)
0
- width = source_code.match(/#{class_name}\.new.*?:width\s=>\s(\d+)/m)
0
- height = source_code.match(/#{class_name}\.new.*?:height\s=>\s(\d+)/m)
0
- description = source_code.match(/# Description:(.*?)\n [^#]/m)
0
+ info = {}
0
+ source_code = info[:source_code] = File.open(@main_file_path, "r") {|file| file.readlines.join(" ")}
0
+ info[:class_name] = source_code.match(/(\w+)\s*<\s*Processing::App/)[1]
0
+ info[:title] = source_code.match(/#{info[:class_name]}\.new.*?:title\s=>\s["'](.+)["']/m)
0
+ info[:width] = source_code.match(/#{info[:class_name]}\.new.*?:width\s=>\s(\d+)/m)
0
+ info[:height] = source_code.match(/#{info[:class_name]}\.new.*?:height\s=>\s(\d+)/m)
0
+ info[:description] = source_code.match(/# Description:(.*?)\n [^#]/m)
0
       matchdata = true
0
- libs_to_load = []
0
+ info[:libs_to_load] = []
0
       code = source_code.dup
0
       while matchdata
0
         matchdata = code.match(/load_\w+_library.+?["':](\S+?)["'\s]/)
0
         if matchdata
0
           if File.exists?("library/#{matchdata[1]}")
0
             @opengl = true if matchdata[1].match(/opengl/i)
0
- libs_to_load << matchdata[1]
0
+ info[:libs_to_load] << matchdata[1]
0
           end
0
           code = matchdata.post_match
0
         end
0
       end
0
- return [source_code, class_name, title, width, height, description, libs_to_load]
0
+ defaults = {:description => "", :title => "Ruby-Processing Sketch", :width => "400", :height => "400"}
0
+ defaults.each {|k,v| info[k] ? info[k] = info[k][1] : info[k] = v }
0
+ return info
0
+ end
0
+
0
+ def hash_to_ivars(hash)
0
+ hash.each{|k,v| instance_variable_set("@" + k.to_s, v) }
0
     end
0
     
0
     def render_erb_in_path_with_binding(path, some_binding, opts={})
0
       erbs = Dir.glob(path + "/**/*.erb")
0
       erbs.each do |erb|
0
- rendered = ERB.new(File.new(erb).read, nil, "<>", "rendered").result(some_binding)
0
+ rendered = render_erb_from_string_with_binding(File.new(erb).read, some_binding)
0
         File.open(erb.sub(".erb", ""), "w") {|f| f.print rendered }
0
         rm erb if opts[:delete]
0
       end
0
     end
0
     
0
+ def render_erb_from_string_with_binding(erb, some_binding)
0
+ rendered = ERB.new(erb, nil, "<>", "rendered").result(some_binding)
0
+ end
0
+
0
     # Ripped from activesupport
0
     def titleize(word)
0
       humanize(underscore(word)).gsub(/\b([a-z])/) { $1.capitalize }
...
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 
25
26
27
...
35
36
37
38
 
39
40
41
42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
44
45
...
11
12
13
 
 
 
 
 
 
 
 
 
 
 
14
15
16
17
...
25
26
27
 
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
0
@@ -11,17 +11,7 @@ module Processing
0
     def generate!
0
       # Check to make sure that the main file exists
0
       main_file_path = ARGV.first
0
- unless main_file_path
0
- puts <<-USAGE
0
-
0
- Usage: script/generate <sketch_to_generate> <width> <height>
0
- Width and Height are optional.
0
-
0
- Example: script/generate fancy_drawing/app 800 600
0
-
0
- USAGE
0
- exit
0
- end
0
+ usage main_file_path
0
       main_file = File.basename(main_file_path, ".rb")
0
       
0
       # Get the substitutions
0
@@ -35,11 +25,25 @@ module Processing
0
       dir = File.dirname main_file_path
0
       mkdir_p dir
0
       template = File.new(File.join(File.dirname(__FILE__), "generate_files/app.rb.erb"))
0
- rendered = ERB.new(template.read, nil, "<>", "rendered").result(binding)
0
+ rendered = render_erb_from_string_with_binding(template.read, binding)
0
       File.open(File.join(dir, "#{@file_name}.rb"), "w") do |file|
0
         file.print rendered
0
       end
0
     end
0
+
0
+ def usage(predicate)
0
+ unless predicate
0
+ puts <<-USAGE
0
+
0
+ Usage: script/generate <sketch_to_generate> <width> <height>
0
+ Width and Height are optional.
0
+
0
+ Example: script/generate fancy_drawing/app 800 600
0
+
0
+ USAGE
0
+ exit
0
+ end
0
+ end
0
   end
0
 end
0
 

Comments

    No one has commented yet.