#!/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!