Class: Processing::AppletExporter

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

Overview

A utility class to export Ruby-Processing sketches as applets that can be viewed online.

Constant Summary collapse

USAGE =
"\nThe applet generator will generate a web-ready applet for you.\nUsage: rp5 applet <path_to_sketch>\nExample: rp5 applet samples/jwishy.rb\n\n"

Constants inherited from BaseExporter

BaseExporter::DEFAULT_DESCRIPTION, BaseExporter::DEFAULT_DIMENSIONS, BaseExporter::NECESSARY_FOLDERS

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



65
66
67
68
69
# File 'lib/ruby-processing/exporters/applet_exporter.rb', line 65

def calculate_substitutions
  file_list = Dir.glob(@dest + "{/**/*.{rb,jar},/data/*.*}").map {|f| f.sub(@dest+"/","")}
  @width = @width.to_i
  @file_list = file_list.join(",")
end

#compute_destination_nameObject



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

def compute_destination_name
  @dest = "#{@main_file.sub(".rb", "")}_applet"
end

#copy_over_necessary_filesObject



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

def copy_over_necessary_files
  @necessary_files = [@main_file_path]
  @necessary_files += Dir["#{RP5_ROOT}/lib/{*,**}"]
  @necessary_files += @real_requires
  NECESSARY_FOLDERS.each do |folder| 
    resource_path = File.join(@main_folder, folder)
    @necessary_files << resource_path if File.exists?(resource_path)
  end
  @necessary_files += Dir["#{RP5_ROOT}/lib/templates/applet/{*,**}"]
  @necessary_files += Dir.glob("library/{#{@libraries.join(",")}}") unless @libraries.empty?
  @necessary_files.uniq!
  cp_r(@necessary_files, @dest)
  cp_r(@libraries, File.join(@dest, "library")) unless @libraries.empty?
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/applet_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
  
  process_opengl_replacements
  
  calculate_substitutions
  
  render_erb_in_path_with_binding(@dest, binding, :delete => true)
end

#process_opengl_replacementsObject



54
55
56
57
58
59
60
61
62
63
# File 'lib/ruby-processing/exporters/applet_exporter.rb', line 54

def process_opengl_replacements
  @starting_class = @opengl ? "com.sun.opengl.util.JOGLAppletLauncher" : "org.jruby.JRubyApplet"
  return unless @opengl
  opengl_files = Dir["#{@dest}/library/opengl/*.jar"]
  opengl_files += Dir["#{@dest}/library/opengl/library/*.jar"]
  move(opengl_files, @dest)
  opengl_dir = "#{@dest}/library/opengl"
  remove_entry_secure(opengl_dir) if File.exists?(opengl_dir)
  @necessary_files.map! {|file| file.match(/^opengl/) ? File.basename(file) : file }
end

#usage(predicate) ⇒ Object



71
72
73
74
75
# File 'lib/ruby-processing/exporters/applet_exporter.rb', line 71

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