Class: Rocket::JS::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/rocket/js/builder.rb

Constant Summary collapse

CONTENTS =
[
  "rocket.license.js", 
]
SCRIPTS =
[
  "vendor/json/json2.js",
  "vendor/web-socket-js/swfobject.js",
  "vendor/web-socket-js/FABridge.js",
  "vendor/web-socket-js/web_socket.js",
  "rocket.core.js",
  "rocket.channels.js", 
  "rocket.defaults.js",
]
ASSETS =
[
  "vendor/web-socket-js/WebSocketMain.swf",
  "vendor/web-socket-js/WebSocketMainInsecure.zip",
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dest, minified = true) ⇒ Builder

Returns a new instance of Builder.



31
32
33
34
# File 'lib/rocket/js/builder.rb', line 31

def initialize(dest, minified=true)
  @dest     = dest
  @minified = minified
end

Instance Attribute Details

#destObject (readonly)

Returns the value of attribute dest.



28
29
30
# File 'lib/rocket/js/builder.rb', line 28

def dest
  @dest
end

#minifiedObject (readonly)

Returns the value of attribute minified.



29
30
31
# File 'lib/rocket/js/builder.rb', line 29

def minified
  @minified
end

Instance Method Details

#bundleObject



48
49
50
51
52
53
54
# File 'lib/rocket/js/builder.rb', line 48

def bundle
  SCRIPTS.map {|script|
    source = "/* #{File.basename(script)} */\n"
    source += File.read(File.expand_path("../../../../src/#{script}", __FILE__))
    source
  }.join("\n\n")
end

#copy_assetsObject



74
75
76
77
78
79
# File 'lib/rocket/js/builder.rb', line 74

def copy_assets
  ASSETS.each {|asset|
    src  = File.expand_path("../../../../src/#{asset}", __FILE__)
    FileUtils.cp(src, dest)
  }
end

#generateObject



36
37
38
39
# File 'lib/rocket/js/builder.rb', line 36

def generate
  generate_script
  copy_assets
end

#generate_scriptObject



41
42
43
44
45
46
# File 'lib/rocket/js/builder.rb', line 41

def generate_script
  FileUtils.mkdir_p(dest)
  source = bundle
  source = minify(source) if minified
  save_script(source)
end

#minify(src) ⇒ Object



56
57
58
59
# File 'lib/rocket/js/builder.rb', line 56

def minify(src)
  compressor = YUI::JavaScriptCompressor.new(:munge => true)
  compressor.compress(src)
end

#save_script(source) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rocket/js/builder.rb', line 61

def save_script(source)
  scriptname = "rocket-#{Rocket.version}#{'.min' if minified}.js"
  File.open(File.expand_path(scriptname, dest), 'w+') {|f|
    contents = CONTENTS.map {|content|
      File.read(File.expand_path("../../../../src/#{content}", __FILE__))
    }.join("\n\n").sub(/<%= VERSION %>/, Rocket.version)

    f.write(contents)
    f.write("\n\n")
    f.write(source)
  }
end