fusion 0.0.7 → 0.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/basic.rb +118 -0
- data/lib/fusion.rb +3 -215
- data/lib/optimized.rb +53 -0
- data/lib/quick.rb +20 -0
- metadata +9 -6
data/lib/basic.rb
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
require 'open4'
|
3
|
+
require 'uri'
|
4
|
+
require 'cgi'
|
5
|
+
require 'rubygems' #I'm getting an error loading mechanize ... do I need to load this?
|
6
|
+
require 'mechanize'
|
7
|
+
require 'logger'
|
8
|
+
|
9
|
+
module Fusion
|
10
|
+
|
11
|
+
class Basic
|
12
|
+
|
13
|
+
def initialize
|
14
|
+
@bundle_options = Fusion.instance_variable_get('@options')
|
15
|
+
@log = @bundle_options[:logger] || Logger.new(STDOUT)
|
16
|
+
|
17
|
+
if @bundle_options[:bundle_configs]
|
18
|
+
@bundle_configs = @bundle_options[:bundle_configs]
|
19
|
+
else
|
20
|
+
@bundle_configs = YAML::load(File.open(@bundle_options[:bundle_file_path]))
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
def run
|
26
|
+
start = Time.now
|
27
|
+
|
28
|
+
bundles = @bundle_configs.collect do |config|
|
29
|
+
bundle(config)
|
30
|
+
end
|
31
|
+
|
32
|
+
@log.debug "Javascript Reloaded #{@bundle_configs.size} bundle(s) (#{Time.now - start}s)"
|
33
|
+
|
34
|
+
bundles
|
35
|
+
end
|
36
|
+
|
37
|
+
def gather_files(config)
|
38
|
+
input_files = []
|
39
|
+
|
40
|
+
if(config[:input_files])
|
41
|
+
config[:input_files].each do |input_file|
|
42
|
+
if (input_file =~ URI::regexp).nil?
|
43
|
+
# Not a URL
|
44
|
+
file_path = input_file
|
45
|
+
|
46
|
+
unless input_file == File.absolute_path(input_file)
|
47
|
+
file_path = File.join(@bundle_options[:project_path], input_file)
|
48
|
+
end
|
49
|
+
|
50
|
+
input_files << file_path
|
51
|
+
else
|
52
|
+
# This is a remote file, if we don't have it, get it
|
53
|
+
input_files << get_remote_file(input_file)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
if (config[:input_directory])
|
59
|
+
directory = File.join(@bundle_options[:project_path],config[:input_directory])
|
60
|
+
|
61
|
+
unless File.exists?(directory)
|
62
|
+
@log.debug "Path #{directory} does not exist"
|
63
|
+
FileUtils.mkpath(directory)
|
64
|
+
@log.debug "Created path: #{directory}"
|
65
|
+
end
|
66
|
+
|
67
|
+
file_names = Dir.open(directory).entries.sort.find_all {|filename| filename.end_with?(".js") }
|
68
|
+
|
69
|
+
input_files += file_names.collect do |file_name|
|
70
|
+
File.join(directory, file_name)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
input_files.uniq
|
75
|
+
end
|
76
|
+
|
77
|
+
def get_output_file(config)
|
78
|
+
raise Exception.new("Undefined js bundler output file") if config[:output_file].nil?
|
79
|
+
output_file = File.join(@bundle_options[:project_path], config[:output_file])
|
80
|
+
path_directories = output_file.split("/")
|
81
|
+
|
82
|
+
if path_directories.size > 1
|
83
|
+
path = File.join(File.expand_path("."), path_directories[0..-2].join("/"))
|
84
|
+
FileUtils::mkpath(File.join(path,"/"))
|
85
|
+
end
|
86
|
+
|
87
|
+
output_file
|
88
|
+
end
|
89
|
+
|
90
|
+
def get_remote_file(url)
|
91
|
+
filename = CGI.escape(url)
|
92
|
+
local_directory = File.join(@bundle_options[:project_path], ".remote")
|
93
|
+
local_file_path = File.join(local_directory, filename)
|
94
|
+
|
95
|
+
return local_file_path if File.exists?(local_file_path)
|
96
|
+
|
97
|
+
@log.debug "Fetching remote file (#{url})"
|
98
|
+
|
99
|
+
m = Mechanize.new
|
100
|
+
response = m.get(url)
|
101
|
+
|
102
|
+
raise Exception.new("Error downloading file (#{url}) -- returned code #{repsonse.code}") unless response.code == "200"
|
103
|
+
|
104
|
+
@log.debug "Got file (#{url})"
|
105
|
+
|
106
|
+
unless Dir.exists?(local_directory)
|
107
|
+
Dir.mkdir(local_directory)
|
108
|
+
end
|
109
|
+
|
110
|
+
File.open(local_file_path,"w") {|f| f << response.body}
|
111
|
+
|
112
|
+
local_file_path
|
113
|
+
end
|
114
|
+
|
115
|
+
|
116
|
+
end
|
117
|
+
|
118
|
+
end
|
data/lib/fusion.rb
CHANGED
@@ -1,10 +1,6 @@
|
|
1
|
-
require '
|
2
|
-
require '
|
3
|
-
require '
|
4
|
-
require 'cgi'
|
5
|
-
require 'rubygems' #I'm getting an error loading mechanize ... do I need to load this?
|
6
|
-
require 'mechanize'
|
7
|
-
require 'logger'
|
1
|
+
require 'basic'
|
2
|
+
require 'quick'
|
3
|
+
require 'optimized'
|
8
4
|
|
9
5
|
module Fusion
|
10
6
|
|
@@ -23,212 +19,4 @@ module Fusion
|
|
23
19
|
# So that the bundler can be configured and run in two different places ... like Sass
|
24
20
|
module_function :configure
|
25
21
|
|
26
|
-
class Basic
|
27
|
-
|
28
|
-
def initialize
|
29
|
-
@bundle_options = Fusion.instance_variable_get('@options')
|
30
|
-
@log = @bundle_options[:logger] || Logger.new(STDOUT)
|
31
|
-
|
32
|
-
if @bundle_options[:bundle_configs]
|
33
|
-
@bundle_configs = @bundle_options[:bundle_configs]
|
34
|
-
else
|
35
|
-
@bundle_configs = YAML::load(File.open(@bundle_options[:bundle_file_path]))
|
36
|
-
end
|
37
|
-
|
38
|
-
end
|
39
|
-
|
40
|
-
def run
|
41
|
-
start = Time.now
|
42
|
-
|
43
|
-
bundles = @bundle_configs.collect do |config|
|
44
|
-
bundle(config)
|
45
|
-
end
|
46
|
-
|
47
|
-
@log.debug "Javascript Reloaded #{@bundle_configs.size} bundle(s) (#{Time.now - start}s)"
|
48
|
-
|
49
|
-
bundles
|
50
|
-
end
|
51
|
-
|
52
|
-
def gather_files(config)
|
53
|
-
input_files = []
|
54
|
-
|
55
|
-
if(config[:input_files])
|
56
|
-
config[:input_files].each do |input_file|
|
57
|
-
if (input_file =~ URI::regexp).nil?
|
58
|
-
# Not a URL
|
59
|
-
file_path = input_file
|
60
|
-
|
61
|
-
unless input_file == File.absolute_path(input_file)
|
62
|
-
file_path = File.join(@bundle_options[:project_path], input_file)
|
63
|
-
end
|
64
|
-
|
65
|
-
input_files << file_path
|
66
|
-
else
|
67
|
-
# This is a remote file, if we don't have it, get it
|
68
|
-
input_files << get_remote_file(input_file)
|
69
|
-
end
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
|
-
if (config[:input_directory])
|
74
|
-
directory = File.join(@bundle_options[:project_path],config[:input_directory])
|
75
|
-
|
76
|
-
unless File.exists?(directory)
|
77
|
-
@log.debug "Path #{directory} does not exist"
|
78
|
-
FileUtils.mkpath(directory)
|
79
|
-
@log.debug "Created path: #{directory}"
|
80
|
-
end
|
81
|
-
|
82
|
-
file_names = Dir.open(directory).entries.sort.find_all {|filename| filename.end_with?(".js") }
|
83
|
-
|
84
|
-
input_files += file_names.collect do |file_name|
|
85
|
-
File.join(directory, file_name)
|
86
|
-
end
|
87
|
-
end
|
88
|
-
|
89
|
-
input_files.uniq
|
90
|
-
end
|
91
|
-
|
92
|
-
def get_output_file(config)
|
93
|
-
raise Exception.new("Undefined js bundler output file") if config[:output_file].nil?
|
94
|
-
output_file = File.join(@bundle_options[:project_path], config[:output_file])
|
95
|
-
path_directories = output_file.split("/")
|
96
|
-
|
97
|
-
if path_directories.size > 1
|
98
|
-
path = File.join(File.expand_path("."), path_directories[0..-2].join("/"))
|
99
|
-
FileUtils::mkpath(File.join(path,"/"))
|
100
|
-
end
|
101
|
-
|
102
|
-
output_file
|
103
|
-
end
|
104
|
-
|
105
|
-
def get_remote_file(url)
|
106
|
-
filename = CGI.escape(url)
|
107
|
-
local_directory = File.join(@bundle_options[:project_path], ".remote")
|
108
|
-
local_file_path = File.join(local_directory, filename)
|
109
|
-
|
110
|
-
return local_file_path if File.exists?(local_file_path)
|
111
|
-
|
112
|
-
@log.debug "Fetching remote file (#{url})"
|
113
|
-
|
114
|
-
m = Mechanize.new
|
115
|
-
response = m.get(url)
|
116
|
-
|
117
|
-
raise Exception.new("Error downloading file (#{url}) -- returned code #{repsonse.code}") unless response.code == "200"
|
118
|
-
|
119
|
-
@log.debug "Got file (#{url})"
|
120
|
-
|
121
|
-
unless Dir.exists?(local_directory)
|
122
|
-
Dir.mkdir(local_directory)
|
123
|
-
end
|
124
|
-
|
125
|
-
File.open(local_file_path,"w") {|f| f << response.body}
|
126
|
-
|
127
|
-
local_file_path
|
128
|
-
end
|
129
|
-
|
130
|
-
|
131
|
-
end
|
132
|
-
|
133
|
-
class Quick < Basic
|
134
|
-
|
135
|
-
def bundle(config)
|
136
|
-
js = []
|
137
|
-
input_files = gather_files(config)
|
138
|
-
|
139
|
-
input_files.each do |input_file|
|
140
|
-
js << File.open(input_file, "r").read
|
141
|
-
end
|
142
|
-
|
143
|
-
js = js.join("\n")
|
144
|
-
|
145
|
-
File.open(get_output_file(config), "w") { |f| f << js }
|
146
|
-
|
147
|
-
js
|
148
|
-
end
|
149
|
-
|
150
|
-
end
|
151
|
-
|
152
|
-
class Optimized < Basic
|
153
|
-
|
154
|
-
def bundle(config)
|
155
|
-
options = []
|
156
|
-
|
157
|
-
options << ["js_output_file", get_output_file(config)]
|
158
|
-
options << ["compilation_level", "SIMPLE_OPTIMIZATIONS"]
|
159
|
-
options << ["language_in","ECMASCRIPT5"] # This will be compatible w all newer browsers, and helps us avoid old IE quirks
|
160
|
-
|
161
|
-
gather_files(config).each do |input_file|
|
162
|
-
options << ["js", input_file]
|
163
|
-
end
|
164
|
-
|
165
|
-
options.collect! do |option|
|
166
|
-
"--#{option[0]} #{option[1]}"
|
167
|
-
end
|
168
|
-
|
169
|
-
jar_file = File.join(__FILE__.split("/")[0..-3].join("/"), "/compiler/compiler.jar")
|
170
|
-
cmd = "java -jar #{jar_file} #{options.join(" ")}"
|
171
|
-
io = IO.popen(cmd, "w")
|
172
|
-
io.close
|
173
|
-
|
174
|
-
raise Exception.new("Error creating bundle: #{get_output_file(config)}") unless $?.exitstatus == 0
|
175
|
-
end
|
176
|
-
|
177
|
-
end
|
178
|
-
|
179
|
-
class DebugOptimized < Optimized
|
180
|
-
def gather_files(config)
|
181
|
-
@log.debug "Warning ... using Debug compiler."
|
182
|
-
input_files = []
|
183
|
-
|
184
|
-
if(config[:input_files])
|
185
|
-
config[:input_files].each do |input_file|
|
186
|
-
@log.debug "Remote file? #{!(input_file =~ URI::regexp).nil?}"
|
187
|
-
|
188
|
-
if (input_file =~ URI::regexp).nil?
|
189
|
-
# Not a URL
|
190
|
-
input_files << File.join(@bundle_options[:project_path], input_file)
|
191
|
-
else
|
192
|
-
# This is a remote file, if we don't have it, get it
|
193
|
-
input_files << get_remote_file(input_file)
|
194
|
-
end
|
195
|
-
end
|
196
|
-
end
|
197
|
-
|
198
|
-
if (config[:input_directory])
|
199
|
-
directory = File.join(@bundle_options[:project_path],config[:input_directory])
|
200
|
-
|
201
|
-
file_names = Dir.open(directory).entries.sort.find_all {|filename| filename.end_with?(".js") }
|
202
|
-
|
203
|
-
input_files += file_names.collect do |file_name|
|
204
|
-
File.join(directory, file_name)
|
205
|
-
end
|
206
|
-
end
|
207
|
-
|
208
|
-
input_files.uniq!
|
209
|
-
|
210
|
-
# Now wrap each file in a try/catch block and update the input_files list
|
211
|
-
|
212
|
-
FileUtils::mkpath(File.join(@bundle_options[:project_path],".debug"))
|
213
|
-
|
214
|
-
input_files.collect do |input_file|
|
215
|
-
contents = File.open(input_file).read
|
216
|
-
new_input_file =""
|
217
|
-
file_name = input_file.split("/").last
|
218
|
-
|
219
|
-
if input_file.include?(".remote")
|
220
|
-
new_input_file = input_file.gsub(".remote",".debug")
|
221
|
-
else
|
222
|
-
new_input_file = File.join(@bundle_options[:project_path], ".debug", file_name)
|
223
|
-
end
|
224
|
-
|
225
|
-
new_contents = "///////////////////\n//mw_bundle: #{input_file}\n///////////////////\n\n try{\n#{contents}\n}catch(e){\nconsole.log('Error (' + e + 'generated in : #{input_file}');\n}"
|
226
|
-
File.open(new_input_file,"w") {|f| f << new_contents}
|
227
|
-
|
228
|
-
new_input_file
|
229
|
-
end
|
230
|
-
|
231
|
-
end
|
232
|
-
end
|
233
|
-
|
234
22
|
end
|
data/lib/optimized.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
module Fusion
|
2
|
+
class Optimized < Fusion::Basic
|
3
|
+
|
4
|
+
def bundle(config)
|
5
|
+
options = get_options(config)
|
6
|
+
|
7
|
+
jar_file = File.join(__FILE__.split("/")[0..-3].join("/"), "/compiler/compiler.jar")
|
8
|
+
cmd = "java -jar #{jar_file} #{options.join(" ")}"
|
9
|
+
io = IO.popen(cmd, "w")
|
10
|
+
io.close
|
11
|
+
|
12
|
+
raise Exception.new("Error creating bundle: #{get_output_file(config)}") unless $?.exitstatus == 0
|
13
|
+
|
14
|
+
File.read(get_output_file(config))
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def get_options(config)
|
20
|
+
options = []
|
21
|
+
|
22
|
+
options << ["js_output_file", get_output_file(config)]
|
23
|
+
options << ["compilation_level", "SIMPLE_OPTIMIZATIONS"]
|
24
|
+
options << ["language_in","ECMASCRIPT5"] # This will be compatible w all newer browsers, and helps us avoid old IE quirks
|
25
|
+
|
26
|
+
gather_files(config).each do |input_file|
|
27
|
+
options << ["js", input_file]
|
28
|
+
end
|
29
|
+
|
30
|
+
options.collect! do |option|
|
31
|
+
"--#{option[0]} #{option[1]}"
|
32
|
+
end
|
33
|
+
|
34
|
+
options
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
class AdvancedOptimized < Fusion::Optimized
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def get_options(config)
|
44
|
+
options = super(config)
|
45
|
+
|
46
|
+
compilation_level_index = options.find_index {|opt| opt =~ /compilation_level/}
|
47
|
+
options[compilation_level_index] = ["--compilation_level ADVANCED_OPTIMIZATIONS"]
|
48
|
+
|
49
|
+
options
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
data/lib/quick.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
module Fusion
|
2
|
+
class Quick < Fusion::Basic
|
3
|
+
|
4
|
+
def bundle(config)
|
5
|
+
js = []
|
6
|
+
input_files = gather_files(config)
|
7
|
+
|
8
|
+
input_files.each do |input_file|
|
9
|
+
js << File.open(input_file, "r").read
|
10
|
+
end
|
11
|
+
|
12
|
+
js = js.join("\n")
|
13
|
+
|
14
|
+
File.open(get_output_file(config), "w") { |f| f << js }
|
15
|
+
|
16
|
+
js
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fusion
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.8
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-10-
|
12
|
+
date: 2011-10-24 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: open4
|
16
|
-
requirement: &
|
16
|
+
requirement: &2152900880 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *2152900880
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: mechanize
|
27
|
-
requirement: &
|
27
|
+
requirement: &2152900440 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,7 +32,7 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *2152900440
|
36
36
|
description: Fusion bundles and re-bundles your javascript in two modes - quick (dumb
|
37
37
|
concatenation) and optimized (google closure compiler's SIMPLE_OPTIMIZATIONS level
|
38
38
|
email:
|
@@ -42,7 +42,10 @@ extensions: []
|
|
42
42
|
extra_rdoc_files: []
|
43
43
|
files:
|
44
44
|
- README.md
|
45
|
+
- lib/basic.rb
|
45
46
|
- lib/fusion.rb
|
47
|
+
- lib/optimized.rb
|
48
|
+
- lib/quick.rb
|
46
49
|
- compiler/compiler.jar
|
47
50
|
- compiler/COPYING
|
48
51
|
- compiler/README
|