Class: Processing::ApplicationExporter

Inherits:
BaseExporter show all
Defined in:
lib/ruby-processing/exporters/application_exporter.rb

Overview

A utility class to export Ruby-Processing sketches as Mac/Win/Nix Applications.

Constant Summary collapse

USAGE =
<<-EOS
    
The application exporter will generate a Mac application for you.
Usage: script/application <path_to_sketch>
Example: script/applet samples/jwishy.rb 
  
EOS

Constants inherited from BaseExporter

BaseExporter::DEFAULT_DESCRIPTION, BaseExporter::DEFAULT_DIMENSIONS, BaseExporter::DEFAULT_TITLE

Instance Method Summary collapse

Methods inherited from BaseExporter

#extract_class_name, #extract_description, #extract_dimension, #extract_information, #extract_libraries, #extract_real_requires, #extract_title, #get_main_file

Instance Method Details

#calculate_substitutionsObject



54
55
56
57
58
59
# File 'lib/ruby-processing/exporters/application_exporter.rb', line 54

def calculate_substitutions
  file_list = Dir.glob(@dest + "{/**/*.{rb,jar},/data/*.*}").map {|f| f.sub(@dest + "/", "")}
  @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(",")
end

#compute_destination_nameObject



35
36
37
# File 'lib/ruby-processing/exporters/application_exporter.rb', line 35

def compute_destination_name
  @dest = "#{@title}.app"
end

#copy_over_necessary_filesObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ruby-processing/exporters/application_exporter.rb', line 39

def copy_over_necessary_files
  @prefix = "lib"
  cp_r(Dir["#{RP5_ROOT}/lib/templates/application/{*,**}"], @dest)
  @necessary_files = [@main_file_path]
  @necessary_files += Dir["#{RP5_ROOT}/lib/{*,**}"]
  @necessary_files += @real_requires
  @necessary_files << "#{@main_folder}/data" if File.exists?("#{@main_folder}/data")
  @necessary_files.uniq!
  cp_r(@necessary_files, File.join(@dest, @prefix))
  cp_r(@libraries, File.join(@dest, @prefix, "library")) unless @libraries.empty?
  # Then move the icon
  potential_icon = Dir.glob(File.join(@dest, @prefix, "data/*.icns"))[0]
  move(potential_icon, File.join(@dest, "Contents/Resources/sketch.icns"), :force => true ) if potential_icon
end

#create_executablesObject



61
62
63
64
65
66
67
68
69
# File 'lib/ruby-processing/exporters/application_exporter.rb', line 61

def create_executables
  render_erb_in_path_with_binding(@dest, binding, :delete => true)
  rm Dir.glob(@dest + "/**/*.java")
  runnable = @dest + "/" + File.basename(@main_file, ".rb")
  move @dest + "/run", runnable
  move @dest + "/run.exe", "#{runnable}.exe"
  chmod 0755, runnable
  chmod 0755, "#{runnable}.exe"
end

#export!(sketch) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ruby-processing/exporters/application_exporter.rb', line 15

def export!(sketch)
  # Check to make sure that the main file exists
  @main_file_path, @main_file, @main_folder = *get_main_file(sketch)
  usage( @main_file_path && File.exists?(@main_file_path) )
  
  extract_information
  
  compute_destination_name
  
  wipe_and_recreate_destination
  
  copy_over_necessary_files
  
  calculate_substitutions
  
  create_executables
  
  symlink_library_into_place
end


71
72
73
74
75
76
77
78
# File 'lib/ruby-processing/exporters/application_exporter.rb', line 71

def symlink_library_into_place
  cd @dest + "/Contents/Resources"
  # Poor ol' windows can't symlink.
  # TODO...
  win = RUBY_PLATFORM.match(/mswin/)
  puts "\n[warning] Applications exported from Windows won't run on Macs...\n" if win 
  ln_s('../../lib', 'Java') unless win
end

#usage(predicate) ⇒ Object



80
81
82
83
84
# File 'lib/ruby-processing/exporters/application_exporter.rb', line 80

def usage(predicate)
  return if predicate
  puts USAGE
  exit
end