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:
ruby-processing / script / application
100755 83 lines (66 sloc) 2.969 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
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env ruby
 
# This is a utility to export Ruby-Processing sketches as Mac Apps.
# -- omygawshkenas
 
require File.join(File.dirname(__FILE__), "base_exporter.rb")
 
module Processing
  class ApplicationExporter < BaseExporter
    def export!
 
      # Check to make sure that the main file exists
      @main_file_path, @main_file = *get_main_file()
      unless PLATFORM.match(/darwin/)
        puts <<-ERROR
Sorry, but the application exporter can only make
Mac applications so far.
ERROR
        exit
      end
      usage( @main_file_path && File.exists?(@main_file_path) )
      
      # Extract all the cool details.
      @info = extract_information
      hash_to_ivars @info
      
      # Make the appropriate directory
      app_dir = "applications/#{@title}.app"
      remove_entry_secure app_dir if File.exists?(app_dir)
      mkdir_p app_dir
      
      # Copy over all the required files
      prefix = "lib"
      cp_r(Dir.glob("script/application_files/{*,**}"), app_dir)
      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.uniq!
      cp_r(necessary_files, File.join(app_dir, prefix))
      library_files = Dir.glob("library/{#{@libs_to_load.join(",")}}") if @libs_to_load.length > 0
      cp_r(library_files, File.join(app_dir, prefix, "library")) if library_files
      
      # Move the icon
      potential_icon = Dir.glob(File.join(app_dir, prefix, "data/*.icns"))[0]
      move(potential_icon, File.join(app_dir, "Contents/Resources/sketch.icns"), :force => true ) if potential_icon
      
      # Figure out the substitutions.
      file_list = Dir.glob(app_dir + "{/**/*.{rb,jar},/data/*.*}").map {|f| f.sub(app_dir + "/", "")}
      @class_path = file_list.map {|f| "$JAVAROOT/" + f.sub(prefix+"/", "") }.join(":")
      @linux_class_path = file_list.map{|f| f.sub(prefix+"/", "")}.join(":")
      @windows_class_path = file_list.map{|f| f.sub(prefix+"/", "")}.join(",")
      
      # Do it.
      render_erb_in_path_with_binding(app_dir, binding, :delete => true)
      rm Dir.glob(app_dir + "/**/*.{class,java}")
      runnable = app_dir + "/" + File.basename(@main_file, ".rb")
      move app_dir + "/run", runnable
      move app_dir + "/run.exe", runnable + ".exe"
      `chmod +x "#{runnable}"`
      cd app_dir + "/Contents/Resources"
      `ln -s ../../lib Java`
      
    end
    
    def usage(predicate)
      unless predicate
        puts <<-USAGE
The application exporter will generate a Mac application for you.
Usage: script/application <path_to_sketch>
Example: script/applet samples/jwishy.rb
USAGE
        exit
      end
    end
  end
end
 
Processing::ApplicationExporter.new.export!