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 / script / applet
100755 72 lines (55 sloc) 2.653 kb
1
2
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
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
71
72
#!/usr/bin/env ruby
 
# This is a utility to export Ruby-Processing
# sketches to applets that can be viewed online.
# -- omygawshkenas
 
require File.join(File.dirname(__FILE__), "base_exporter.rb")
 
module Processing
  class AppletExporter < BaseExporter
    def export!
 
      # Check to make sure that the main file exists
      @main_file_path, @main_file = *get_main_file()
      unless @main_file_path && File.exists?(@main_file_path)
        puts <<-USAGE
The applet generator will generate a web-ready applet for you.
Usage: script/applet <path_to_sketch>
Example: script/applet samples/jwishy.rb
USAGE
        exit
      end
      
      # Extract all the cool details.
      @info = extract_information
      hash_to_ivars @info
      
      # Make the appropriate directory
      applet_dir = "applets/#{@main_file.sub(".rb", "")}"
      remove_entry_secure applet_dir if File.exists?(applet_dir)
      mkdir_p applet_dir
      
      # Copy over all the required files
      necessary_files = [@main_file_path, "ruby-processing.rb", "core.jar"]
      necessary_files += extract_real_requires(@main_file_path)
      necessary_files << "data" if File.exists?("data")
      necessary_files += Dir.glob("script/base_files/{*,**}") # Base files
      necessary_files += Dir.glob("script/applet_files/{*,**}") # Exporter files
      necessary_files += Dir.glob("library/{#{@libs_to_load.join(",")}}") if @libs_to_load.length > 0
      necessary_files.uniq!
      cp_r(necessary_files, applet_dir)
      
      # Figure out OpenGL replacements, if necessary:
      @starting_class = @opengl ? "com.sun.opengl.util.JOGLAppletLauncher" : "org.jruby.JRubyApplet"
      if @opengl
        opengl_files = Dir.glob(applet_dir + "/opengl/*.jar")
        opengl_files += Dir.glob(applet_dir + "/opengl/library/*.jar")
        move(opengl_files, applet_dir)
        remove_entry_secure(applet_dir + "/opengl")
        necessary_files.map! {|file| file.match(/^opengl/) ? File.basename(file) : file }
      end
      
      
      # Figure out the substitutions to make.
      file_list = Dir.glob(applet_dir + "{/**/*.{rb,jar},/data/*.*}").map {|f| f.sub(applet_dir+"/","")}
      h1 = "<h1>#{@title}</h1>"
      @description = "<div id='description'>" + h1 + "<p>" + @description.gsub!("\n #", "") + "</p></div>" if !@description.empty?
      @width_plus_14 = (@width.to_i + 14).to_s
      @file_list = file_list.join(",")
      
      # Fill in the blanks in the HTML.
      render_erb_in_path_with_binding(applet_dir, binding, :delete => true)
      
    end
  end
end
 
Processing::AppletExporter.new.export!