fusion 0.0.9 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/bin/fusion +47 -0
  2. data/lib/fusion.rb.orig +159 -0
  3. metadata +6 -21
@@ -0,0 +1,47 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+ require 'yaml'
5
+ require_relative '../lib/fusion'
6
+
7
+ bundlers = {
8
+ "quick" => Fusion::Quick,
9
+ "optimized" => Fusion::Optimized,
10
+ "advanced_optimized" => Fusion::AdvancedOptimized,
11
+ "pretty" => Fusion::Pretty
12
+ }
13
+
14
+ options = {
15
+ :mode => "quick"
16
+ }
17
+
18
+ def usage
19
+ puts "Usage: fusion -m <mode> <bundle_file>"
20
+ puts "Modes: #{modes.join('|')} -- default: #{options[:mode]}"
21
+ exit(1)
22
+ end
23
+
24
+ options_parser = OptionParser.new do |x|
25
+ x.on("-m", "--mode=MODE", "Specify compilation mode") do |value|
26
+ options[:mode] = value
27
+ end
28
+ end
29
+
30
+ options_parser.parse!
31
+
32
+ puts "Options: #{options}"
33
+
34
+ usage unless ARGV[0]
35
+
36
+ bundle = File.absolute_path(ARGV[0])
37
+ path = File.split(bundle)[0..-2].join("/")
38
+
39
+ bundle_options = YAML.load( File.read(bundle) )
40
+
41
+ puts "bundle options: #{bundle_options}"
42
+ puts "project path: #{path}"
43
+
44
+ bundle_options.each do |this_bundle|
45
+ bundler = bundlers[options[:mode]].new(this_bundle, path)
46
+ bundler.run
47
+ end
@@ -0,0 +1,159 @@
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
+ require 'cgi'
9
+
10
+ module Fusion
11
+
12
+ def configure(options)
13
+ @options ||= {}
14
+ @options.update(options)
15
+
16
+ raise Exception("Configuration error -- must specify #{:bundle_file_path} when configuring Fusion javascript bundler") if @options[:bundle_file_path].nil?
17
+
18
+ @options[:project_path] = File.join(@options[:bundle_file_path].split("/")[0..-2])
19
+ end
20
+
21
+ # So that the bundler can be configured and run in two different places ... like Sass
22
+ module_function :configure
23
+
24
+ class Basic
25
+
26
+ def initialize
27
+ @bundle_options = Fusion.instance_variable_get('@options')
28
+ @log = @bundle_options[:logger] || Logger.new(STDOUT)
29
+
30
+ @bundle_configs = YAML::load(File.open(@bundle_options[:bundle_file_path]))
31
+ end
32
+
33
+ def run
34
+ start = Time.now
35
+
36
+ @bundle_configs.each do |config|
37
+ bundle(config)
38
+ end
39
+
40
+ @log.debug "Javascript Reloaded #{@bundle_configs.size} bundle(s) (#{Time.now - start}s)"
41
+ end
42
+
43
+ def gather_files(config)
44
+ input_files = []
45
+
46
+ if(config[:input_files])
47
+ config[:input_files].each do |input_file|
48
+ if (input_file =~ URI::regexp).nil?
49
+ # Not a URL
50
+ input_files << File.join(@bundle_options[:project_path], input_file)
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
+ File.join(@bundle_options[:project_path], config[:output_file])
80
+ end
81
+
82
+ def get_remote_file(url)
83
+ <<<<<<< HEAD
84
+ filename = CGI.escape(url.split("/").last)
85
+ =======
86
+ filename = CGI.escape(url)
87
+ >>>>>>> 5fbc75b23e4a721aa6c7d051b72965a57b79ecde
88
+ local_directory = File.join(@bundle_options[:project_path], ".remote")
89
+ local_file_path = File.join(local_directory, filename)
90
+
91
+ return local_file_path if File.exists?(local_file_path)
92
+
93
+ @log.debug "Fetching remote file (#{url})"
94
+
95
+ m = Mechanize.new
96
+ response = m.get(url)
97
+
98
+ raise Exception.new("Error downloading file (#{url}) -- returned code #{repsonse.code}") unless response.code == "200"
99
+
100
+ @log.debug "Got file (#{url})"
101
+
102
+ unless Dir.exists?(local_directory)
103
+ Dir.mkdir(local_directory)
104
+ end
105
+
106
+ File.open(local_file_path,"w") {|f| f << response.body}
107
+
108
+ local_file_path
109
+ end
110
+
111
+
112
+ end
113
+
114
+ class Quick < Basic
115
+
116
+ def bundle(config)
117
+ js = []
118
+
119
+ input_files = gather_files(config)
120
+
121
+ input_files.each do |input_file|
122
+ js << File.open(input_file, "r").read
123
+ end
124
+
125
+ js = js.join("\n")
126
+
127
+ File.open(get_output_file(config), "w") { |f| f << js }
128
+ end
129
+
130
+ end
131
+
132
+ class Optimized < Basic
133
+
134
+ def bundle(config)
135
+ options = []
136
+
137
+ options << ["js_output_file", get_output_file(config)]
138
+ options << ["compilation_level", "SIMPLE_OPTIMIZATIONS"]
139
+ options << ["language_in","ECMASCRIPT5"] # This will be compatible w all newer browsers, and helps us avoid old IE quirks
140
+
141
+ gather_files(config).each do |input_file|
142
+ options << ["js", input_file]
143
+ end
144
+
145
+ options.collect! do |option|
146
+ "--#{option[0]} #{option[1]}"
147
+ end
148
+
149
+ jar_file = File.join(__FILE__.split("/")[0..-3].join("/"), "/compiler/compiler.jar")
150
+ cmd = "java -jar #{jar_file} #{options.join(" ")}"
151
+ io = IO.popen(cmd, "w")
152
+ io.close
153
+
154
+ raise Exception.new("Error creating bundle: #{get_output_file(config)}") unless $?.exitstatus == 0
155
+ end
156
+
157
+ end
158
+
159
+ end
metadata CHANGED
@@ -1,13 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fusion
3
3
  version: !ruby/object:Gem::Version
4
- hash: 13
5
4
  prerelease:
6
- segments:
7
- - 0
8
- - 0
9
- - 9
10
- version: 0.0.9
5
+ version: 0.0.10
11
6
  platform: ruby
12
7
  authors:
13
8
  - Sean Jezewski
@@ -15,7 +10,7 @@ autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
12
 
18
- date: 2011-12-02 00:00:00 Z
13
+ date: 2012-01-21 00:00:00 Z
19
14
  dependencies:
20
15
  - !ruby/object:Gem::Dependency
21
16
  name: open4
@@ -25,9 +20,6 @@ dependencies:
25
20
  requirements:
26
21
  - - ">="
27
22
  - !ruby/object:Gem::Version
28
- hash: 3
29
- segments:
30
- - 0
31
23
  version: "0"
32
24
  type: :runtime
33
25
  version_requirements: *id001
@@ -39,17 +31,14 @@ dependencies:
39
31
  requirements:
40
32
  - - ">="
41
33
  - !ruby/object:Gem::Version
42
- hash: 3
43
- segments:
44
- - 0
45
34
  version: "0"
46
35
  type: :runtime
47
36
  version_requirements: *id002
48
37
  description: Fusion bundles and re-bundles your javascript in two modes - quick (dumb concatenation) and optimized (google closure compiler's SIMPLE_OPTIMIZATIONS level
49
38
  email:
50
39
  - sean@moovweb.com
51
- executables: []
52
-
40
+ executables:
41
+ - fusion
53
42
  extensions: []
54
43
 
55
44
  extra_rdoc_files: []
@@ -58,12 +47,14 @@ files:
58
47
  - README.md
59
48
  - lib/basic.rb
60
49
  - lib/fusion.rb
50
+ - lib/fusion.rb.orig
61
51
  - lib/optimized.rb
62
52
  - lib/pretty.rb
63
53
  - lib/quick.rb
64
54
  - compiler/compiler.jar
65
55
  - compiler/COPYING
66
56
  - compiler/README
57
+ - bin/fusion
67
58
  homepage: http://github.com/moovweb/fusion
68
59
  licenses: []
69
60
 
@@ -77,18 +68,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
77
68
  requirements:
78
69
  - - ">="
79
70
  - !ruby/object:Gem::Version
80
- hash: 3
81
- segments:
82
- - 0
83
71
  version: "0"
84
72
  required_rubygems_version: !ruby/object:Gem::Requirement
85
73
  none: false
86
74
  requirements:
87
75
  - - ">="
88
76
  - !ruby/object:Gem::Version
89
- hash: 3
90
- segments:
91
- - 0
92
77
  version: "0"
93
78
  requirements: []
94
79