Class: Processing::BaseExporter

Inherits:
Object
  • Object
show all
Includes:
FileUtils
Defined in:
lib/ruby-processing/exporters/base_exporter.rb

Overview

This base exporter implements some of the common code-munging needed to generate apps and applets.

Direct Known Subclasses

AppletExporter, ApplicationExporter, Creator

Constant Summary collapse

DEFAULT_TITLE =
"Ruby-Processing Sketch"
DEFAULT_DIMENSIONS =
{'width' => '500', 'height' => '500'}
DEFAULT_DESCRIPTION =
''

Instance Method Summary collapse

Instance Method Details

#extract_class_name(source) ⇒ Object

Searches the source for a class name.



38
39
40
41
# File 'lib/ruby-processing/exporters/base_exporter.rb', line 38

def extract_class_name(source)
  match = source.match(/(\w+)\s*<\s*Processing::App/)
  match ? match[1] : nil
end

#extract_description(source) ⇒ Object

Searches the source for a description of the sketch.



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

def extract_description(source)
  match = source.match(/# Description:(.*?)\n[^#]/m)
  match ? match[1].gsub!(/\n(\s*)?#/, "") : DEFAULT_DESCRIPTION
end

#extract_dimension(source, dimension) ⇒ Object

Searches the source for the width and height of the sketch.



50
51
52
53
# File 'lib/ruby-processing/exporters/base_exporter.rb', line 50

def extract_dimension(source, dimension)
  match = source.match(/#{@info[:class_name]}\.new.*?:#{dimension}\s=>\s(\d+)/m)
  match ? match[1] : DEFAULT_DIMENSIONS[dimension]
end

#extract_informationObject

Centralized method to read the source of the sketch and extract all the juicy details.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ruby-processing/exporters/base_exporter.rb', line 22

def extract_information
  # Extract information from main file
  @info = {}
  @info[:source_code]     = source = read_source_code
  @info[:class_name]      = extract_class_name(source)
  @info[:title]           = extract_title(source)
  @info[:width]           = extract_dimension(source, 'width')
  @info[:height]          = extract_dimension(source, 'height')
  @info[:description]     = extract_description(source)
  @info[:libraries]       = extract_libraries(source)
  @info[:real_requires]   = extract_real_requires(source)
  hash_to_ivars @info
  @info
end

#extract_libraries(source) ⇒ Object

Searches the source any libraries that have been loaded.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/ruby-processing/exporters/base_exporter.rb', line 62

def extract_libraries(source)
  libs = []
  code = source.dup
  loop do
    matchdata = code.match(/load_\w+_library.+?["':](\S+?)["'\s]/)
    break unless matchdata
    match = matchdata[1]
    @opengl = true if match.match(/opengl/i)
    local_path = "#{local_dir}/library/#{match}"
    rp5_path = "#{RP5_ROOT}/library/#{match}"
    File.exists?(local_path) ? libs << local_path : 
      (libs << rp5_path if File.exists?(rp5_path))
    code = matchdata.post_match
  end
  libs
end

#extract_real_requires(source) ⇒ Object

Looks for all of the codes require or load commands, checks to see if the file exists (that it’s not a gem, or a standard lib) and hands you back all the real ones.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/ruby-processing/exporters/base_exporter.rb', line 82

def extract_real_requires(source)
  code = source.dup
  requirements = []
  partial_paths = []
  loop do
    matchdata = code.match(/^.*\b(require|load)\b.*$/)
    break unless matchdata
    line = matchdata[0].gsub('__FILE__', "'#{@main_file_path}'")
    line = line.gsub(/\b(require|load)\b/, 'partial_paths << ')
    eval(line)
    requirements += Dir["{#{local_dir}/,}{#{partial_paths.join(',')}}.{rb,jar}"]
    code = matchdata.post_match
  end
  return requirements
end

#extract_title(source) ⇒ Object

Searches the source for a title.



44
45
46
47
# File 'lib/ruby-processing/exporters/base_exporter.rb', line 44

def extract_title(source)
  match = source.match(/#{@info[:class_name]}\.new.*?:title\s=>\s["'](.+)["']/m)
  match ? match[1] : DEFAULT_TITLE
end

#get_main_file(file) ⇒ Object

Returns the filepath, basename, and directory name of the sketch.



16
17
18
# File 'lib/ruby-processing/exporters/base_exporter.rb', line 16

def get_main_file(file)
  return file, File.basename(file), File.dirname(file)
end