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
Search Repo:
Initial work on an application exporter. Refactored the exporters.
jashkenas (author)
Fri Apr 18 06:03:02 -0700 2008
commit  d8ad434a85bfae52eee0c7d31050b372c0ebefbf
tree    007118e125e5b318960a1b048034b80e5925c967
parent  4d1e8eb85b7e53d825f3b6ad2539c8c1349bf7ad
...
4
5
6
7
8
 
9
10
11
 
12
13
14
15
16
17
 
 
18
19
20
...
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
62
63
64
65
66
67
 
 
68
69
70
...
87
88
89
90
 
91
92
93
...
4
5
6
 
 
7
8
9
 
10
11
12
13
 
 
 
14
15
16
17
18
...
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
...
67
68
69
 
70
71
72
73
0
@@ -4,17 +4,15 @@
0
 # sketches to applets that can be viewed online.
0
 # -- omygawshkenas
0
 
0
-require 'fileutils'
0
-require 'erb'
0
+require File.join(File.dirname(__FILE__), "base_exporter.rb")
0
 
0
 module Processing
0
- class AppletExporter
0
+ class AppletExporter < BaseExporter
0
     def export!
0
 
0
       # Check to make sure that the main file exists
0
- main_file_path = ARGV.first
0
- main_file = File.basename(main_file_path)
0
- unless main_file_path && File.exists?(main_file_path)
0
+ @main_file_path, main_file = *get_main_file()
0
+ unless @main_file_path && File.exists?(@main_file_path)
0
         puts <<-USAGE
0
         
0
       The applet generator will generate a web-ready applet for you.
0
@@ -25,46 +23,28 @@ module Processing
0
         exit
0
       end
0
       
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
- matchdata = true
0
- 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
- end
0
- code = matchdata.post_match
0
- end
0
- end
0
+ # Extract all the cool details.
0
+ source_code, class_name, title, width, height, description, libs_to_load = *extract_information()
0
       
0
       # Make the appropriate directory
0
       applet_dir = "applets/#{main_file.sub(".rb", "")}"
0
- FileUtils.remove_entry_secure applet_dir if File.exists?(applet_dir)
0
- FileUtils.mkdir_p applet_dir
0
+ remove_entry_secure applet_dir if File.exists?(applet_dir)
0
+ mkdir_p applet_dir
0
       
0
       # Copy over all the required files
0
- necessary_files = [main_file_path, "ruby-processing.rb", "core.jar"]
0
+ necessary_files = [@main_file_path, "ruby-processing.rb", "core.jar"]
0
       necessary_files << "data" if File.exists?("data")
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
- FileUtils.cp_r(necessary_files, applet_dir)
0
+ cp_r(necessary_files, applet_dir)
0
       
0
       # Figure out OpenGL replacements, if necessary:
0
       @starting_class = @opengl ? "com.sun.opengl.util.JOGLAppletLauncher" : "org.jruby.JRubyApplet"
0
       if @opengl
0
         opengl_files = Dir.glob(applet_dir + "/opengl/*.jar")
0
         opengl_files += Dir.glob(applet_dir + "/opengl/library/*.jar")
0
- FileUtils.move(opengl_files, applet_dir)
0
- FileUtils.remove_entry_secure(applet_dir + "/opengl")
0
+ move(opengl_files, applet_dir)
0
+ remove_entry_secure(applet_dir + "/opengl")
0
         necessary_files.map! {|file| file.match(/^opengl/) ? File.basename(file) : file }
0
       end
0
       
0
@@ -87,7 +67,7 @@ module Processing
0
       File.open(applet_dir + "/index.html", "w") do |file|
0
         file.print rendered_index
0
       end
0
- FileUtils.rm(applet_dir + "/index.html.erb")
0
+ rm(applet_dir + "/index.html.erb")
0
       
0
     end
0
   end
...
3
4
5
6
7
8
 
 
9
10
11
12
13
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
...
59
60
61
62
 
63
64
65
...
3
4
5
 
 
 
6
7
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
10
11
12
...
33
34
35
 
36
37
38
39
0
@@ -3,36 +3,10 @@
0
 # This is a utility for generating empty Ruby-Processing templates.
0
 # -- omygawshkenas
0
 
0
-require 'fileutils'
0
-require 'erb'
0
-
0
+require File.join(File.dirname(__FILE__), "base_exporter.rb")
0
+
0
 module Processing
0
- class Generator
0
-
0
- # Ripped from activesupport
0
- def titleize(word)
0
- humanize(underscore(word)).gsub(/\b([a-z])/) { $1.capitalize }
0
- end
0
-
0
- def humanize(lower_case_and_underscored_word)
0
- lower_case_and_underscored_word.to_s.gsub(/_id$/, "").gsub(/_/, " ").capitalize
0
- end
0
-
0
- def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
0
- if first_letter_in_uppercase
0
- lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
0
- else
0
- lower_case_and_underscored_word.first + camelize(lower_case_and_underscored_word)[1..-1]
0
- end
0
- end
0
-
0
- def underscore(camel_cased_word)
0
- camel_cased_word.to_s.gsub(/::/, '/').
0
- gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
0
- gsub(/([a-z\d])([A-Z])/,'\1_\2').
0
- tr("-", "_").
0
- downcase
0
- end
0
+ class Generator < BaseExporter
0
     
0
     def generate!
0
       # Check to make sure that the main file exists
0
@@ -59,7 +33,7 @@ module Processing
0
   
0
       # Make the file
0
       dir = File.dirname main_file_path
0
- FileUtils.mkdir_p dir
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
       File.open(File.join(dir, "#{@file_name}.rb"), "w") do |file|

Comments

    No one has commented yet.