Class: OpenLaszlo::Applet

Inherits:
Object
  • Object
show all
Defined in:
lib/openlaszlo/applet.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ Applet

Returns a new instance of Applet.



5
6
7
8
# File 'lib/openlaszlo/applet.rb', line 5

def initialize(source)
  source = source + '.lzx' unless source =~ /\.lzx$/
  @source = source
end

Instance Attribute Details

#sourceObject (readonly)

Returns the value of attribute source.



3
4
5
# File 'lib/openlaszlo/applet.rb', line 3

def source
  @source
end

Class Method Details

.compile(basename, target, options = {}) ⇒ Object

Class methods



72
73
74
75
76
# File 'lib/openlaszlo/applet.rb', line 72

def self.compile(basename, target, options={})
  source = "lzx/#{basename}"
  target = "public/#{target}"
  self.new(source).compile(target, options)
end

Instance Method Details

#compile(target = nil, options = {}) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/openlaszlo/applet.rb', line 14

def compile(target=nil, options={})
  target ||= source + '.swf'
  return if uptodate?(target) unless options[:force]

  puts "Compiling #{source} -> #{target}" if options[:verbose]
  OpenLaszlo::compile(source, options)
end

#preprocess_string(content) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/openlaszlo/applet.rb', line 59

def preprocess_string(content)
  re = /^\s*([a-zA-Z][a-zA-Z0=9]*)\.each\(function\(([a-zA-Z][a-zA-Z0-9]*)\)\{(.*?)}\);/m
  content.gsub!(re) do |s|
    target, var, body = s.match(re).captures
    "var $0=#{target}, $1=$0.length;for(var $2=0; $1--;){var #{var}=$0[$2++];#{body}}"
  end
  return content
end

#preprocess_to(dir, options = {}) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/openlaszlo/applet.rb', line 48

def preprocess_to(dir, options={})
  files = Dir[File.join(source_dir, '**/*.js')] - Dir[File.join(source_dir, '.hg')]
  files.each do |src|
    dst = File.join(dir, src.sub(source_dir, ''))
    next if File.exists?(dst) and (!options[:force] and File.mtime(dst) > File.mtime(src))
    puts "Copy #{src} #{dst}"
    content = preprocess_string(open(src).read)
    open(dst, 'w') do |fo| fo << content end
  end.length
end

#runtime_assetsObject



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/openlaszlo/applet.rb', line 35

def runtime_assets
  dir = File.dirname(source)
  includes = `grep -h http: #{dir}/*.lzx`.scan(/http:(.*?)['"]/).
    map(&:first)
  includes += `grep -h http: #{dir}/*/*.lzx`.scan(/http:(.*?)['"]/).
    map(&:first).
    map { |s| s.sub(/^\.\.\//, '') }
  return includes.
    uniq.
    map { |f| File.join(dir, f) }.
    select { |src| File.exists?(src) && File.ftype(src) != 'directory' }
end

#source_dirObject



10
11
12
# File 'lib/openlaszlo/applet.rb', line 10

def source_dir
  File.dirname(source)
end

#uptodate?(target = nil) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/openlaszlo/applet.rb', line 22

def uptodate?(target=nil)
  target ||= source + '.swf'
  return false unless File.exists?(target)
  sources = Dir["#{source_dir}/**/*"].reject { |fname| fname =~ /\.lzx\.swf/ } -
    Dir["#{source_dir}/build/**/*"]
  source_mtime = sources.
    # the 'rescue' ignores symbolic links without targets
    map { |f| File.mtime(f) rescue nil }.
    compact.
    max
  source_mtime < File.mtime(target)
end