zfben_libjs 0.0.11 → 0.0.12

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -73,6 +73,11 @@ support before and after events
73
73
  <%= lib %>
74
74
  # => <script src="/javascripts/lib.js?12345678"></script>
75
75
 
76
- # OR
77
76
  <%= lib :home %>
78
- # => <script src="/javascripts/lib.js?12345678"></script><script>lib('home')</script>
77
+ # => <script src="/javascripts/lib.js?12345678"></script><script>lib('home')</script>
78
+
79
+ <%= lib 'home.css' %>
80
+ # => <script src="/javascripts/lib.js?12345678"></script><link rel="stylesheet" href="/stylesheets/home.css?12345678" />
81
+
82
+ <%= lib 'home.js' %>
83
+ # => <script src="/javascripts/lib.js?12345678"></script><script src="/javascripts/home.js?12345678"></script>
data/Rakefile CHANGED
@@ -12,4 +12,11 @@ namespace :test do
12
12
 
13
13
  t.setup(sources_directory, test_cases, browsers)
14
14
  end
15
+
16
+ desc 'Runs all the Ruby tests'
17
+ task :ruby do
18
+ require File.realpath('lib/zfben_libjs.rb')
19
+ require 'test/unit'
20
+ Dir['test/ruby/*_test.rb'].each{ |f| require File.realpath(f) }
21
+ end
15
22
  end
@@ -0,0 +1,98 @@
1
+ class Zfben_libjs::Libjs
2
+ Lib_version = Time.now.strftime('?%s')
3
+ Default_options = {
4
+ :config => {
5
+
6
+ 'src' => 'src',
7
+ 'src/source' => 'src/.source',
8
+ 'src/javascripts' => 'src/javascripts',
9
+ 'src/images' => 'src/images',
10
+ 'src/stylesheets' => 'src/stylesheets',
11
+
12
+ 'url' => '',
13
+ 'url/javascripts' => 'url/javascripts',
14
+ 'url/images' => 'url/images',
15
+ 'url/stylesheets' => 'url/stylesheets',
16
+
17
+ 'download' => false,
18
+ 'minify' => true,
19
+ 'changeImageUrl' => true
20
+
21
+ },
22
+ :libs => {
23
+ 'lazyload' => 'https://raw.github.com/rgrove/lazyload/master/lazyload.js'
24
+ },
25
+ :bundle => {},
26
+ :routes => {},
27
+ :preload => {}
28
+ }
29
+
30
+ def initialize *opts
31
+
32
+ # recive path or hash options
33
+ case opts[0].class.to_s
34
+ when 'String'
35
+ opts = read_config_file(opts[0])
36
+ when 'Hash'
37
+ opts = opts[0]
38
+ else
39
+ opts = {}
40
+ end
41
+
42
+ @opts = merge_and_convert_options opts
43
+
44
+ @path_gem = File.realpath(File.dirname(__FILE__))
45
+
46
+ # config
47
+ ['src', 'src/source', 'src/javascripts', 'src/images', 'src/stylesheets'].each do |path|
48
+ FileUtils.mkdir(@opts[:config][path]) unless File.exists?(@opts[:config][path])
49
+ end
50
+
51
+ # Merge default libs
52
+ @libs = {
53
+ 'lazyload' => 'https://raw.github.com/rgrove/lazyload/master/lazyload.js'
54
+ }
55
+ @libs = @libs.merge(@opts[:libs]) if @opts.has_key?(:libs)
56
+ end
57
+
58
+ def opts
59
+ return @opts.clone
60
+ end
61
+
62
+ def self.defaults
63
+ return Default_options.clone
64
+ end
65
+
66
+ private
67
+
68
+ def read_config_file filepath
69
+ config_file = File.exists?(filepath) && !File.directory?(filepath) ? [filepath] : Dir[filepath + '*'].select{ |f| !File.directory?(f) }
70
+ if config_file.length == 0
71
+ err config_file + ' is not exist!'
72
+ end
73
+
74
+ begin
75
+ data = YAML.load(File.read(config_file[0]))
76
+ rescue Exception => e
77
+ data = {}
78
+ end
79
+
80
+ return data
81
+ end
82
+
83
+ def merge_and_convert_options opts
84
+ options = Default_options.clone
85
+
86
+ [:config, :libs, :bundle, :routes, :preload].each do |name|
87
+ if opts.has_key?(name)
88
+ options[name] = options[name].merge(opts[name])
89
+ else
90
+ if opts.has_key?(name.to_s)
91
+ options[name] = options[name].merge(opts[name.to_s])
92
+ end
93
+ end
94
+ end
95
+
96
+ return options
97
+ end
98
+ end
@@ -13,7 +13,7 @@ def get_filetype path
13
13
  end
14
14
 
15
15
  def download url, path
16
- if url =~ /:\/\// && (@config['download'] == true || !File.exists?(path))
16
+ if url =~ /:\/\// && (@opts[:config]['download'] == true || !File.exists?(path))
17
17
  unless system 'wget ' + url + ' -O ' + path + ' -N'
18
18
  p url + ' download fail!'
19
19
  system('rm ' + path)
@@ -0,0 +1,43 @@
1
+ if defined?(Rails)
2
+ Lib_version = Time.now.strftime('?%s')
3
+ module Rails
4
+ module ActionView::Helpers::AssetTagHelper
5
+ def lib *opts
6
+ html = lib_js('lib.js')
7
+ unless opts.blank?
8
+ preload = []
9
+ lib_preload = []
10
+ opts.each do |name|
11
+ name = name.to_s
12
+ if name.end_with?('.css') || name.end_with?('.js')
13
+ preload.push name
14
+ else
15
+ lib_preload.push name
16
+ end
17
+ end
18
+ if preload.length > 0
19
+ preload.each do |url|
20
+ if url.end_with?('.css')
21
+ html << lib_css(url)
22
+ else
23
+ html << lib_js(url)
24
+ end
25
+ end
26
+ end
27
+ if lib_preload.length > 0
28
+ html << "<script>lib('#{lib_preload.join(' ')}')</script>"
29
+ end
30
+ end
31
+ return html
32
+ end
33
+
34
+ def lib_js url
35
+ return "<script src='/javascripts/#{url}#{Lib_version}'></script>"
36
+ end
37
+
38
+ def lib_css url
39
+ return "<link rel='stylesheet' href='/stylesheets/#{url}#{Lib_version}' />"
40
+ end
41
+ end
42
+ end
43
+ end
data/lib/zfben_libjs.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'rubygems'
2
+ require 'fileutils'
2
3
  require 'rainbow'
3
4
  require 'json'
4
5
  require 'compass'
@@ -6,7 +7,14 @@ require 'coffee-script'
6
7
  require 'uglifier'
7
8
  require 'yaml'
8
9
  require 'sass/css'
9
- require File.join(File.dirname(__FILE__), 'zfben_libjs', 'lib.rb')
10
+ require 'active_support/core_ext'
11
+
12
+ module Zfben_libjs
13
+ class Libjs
14
+ end
15
+ end
16
+
17
+ ['lib.rb', 'initialize.rb', 'railtie.rb'].each { |f| require File.join(File.dirname(__FILE__), 'zfben_libjs', f) }
10
18
 
11
19
  def err msg
12
20
  STDERR.print "#{msg}\n".color(:red)
@@ -19,64 +27,12 @@ end
19
27
 
20
28
  module Zfben_libjs
21
29
  class Libjs
22
- def initialize config_file
23
- @config_file = File.exists?(config_file) && !File.directory?(config_file) ? [config_file] : Dir[config_file + '*'].select{ |f| !File.directory?(f) }
24
- if @config_file.length == 0
25
- err config_file + ' is not exist!'
26
- else
27
- @config_file = @config_file[0]
28
- end
29
-
30
- begin
31
- @data = YAML.load(File.read(@config_file))
32
- rescue => e
33
- err "#{@config_file} load filed!\n#{e}"
34
- end
35
-
36
- @path_gem = File.realpath(File.join(File.dirname(__FILE__), 'zfben_libjs'))
37
- @path_lib = File.realpath('.')
38
-
39
- tip "#{@config_file} load success!"
40
- end
41
30
 
42
31
  def build!
43
- tip '== Starting Build @' + @config_file
44
-
45
- # Merge default config
46
- @config = {
47
- 'src' => 'src',
48
- 'download' => false,
49
- 'minify' => true,
50
- 'url' => ''
51
- }.merge(@data['config'])
32
+ tip '== Starting Build'
52
33
 
53
- @config['src'] = File.join(File.dirname(@config_file), @config['src'])
54
- system('mkdir ' + @config['src']) unless File.exists?(@config['src'])
55
-
56
- ['source'].each do |path|
57
- @config['src/' + path] = File.join(@config['src'], '.' + path) unless @config.has_key?('src/' + path)
58
- system('mkdir ' + @config['src/' + path]) unless File.exists?(@config['src/' + path])
59
- end
60
-
61
- ['javascripts', 'stylesheets', 'images'].each do |path|
62
- @config['url/' + path] = @config['url'] + '/' + path unless @config.has_key?('url/' + path)
63
- @config['src/' + path] = File.join(@config['src'], path) unless @config.has_key?('src/' + path)
64
- system('mkdir ' + @config['src/' + path]) unless File.exists?(@config['src/' + path])
65
- end
66
-
67
- # Merge default libs
68
- @libs = {
69
- 'lazyload' => 'https://raw.github.com/rgrove/lazyload/master/lazyload.js'
70
- }.merge(@data['libs'])
71
-
72
- @bundle = @data['bundle']
73
-
74
- @routes = @data['routes']
75
-
76
- @preload = @data['preload']
77
-
78
- if @config.has_key?('before')
79
- load @config['before']
34
+ if @opts[:config].has_key?('before')
35
+ load @opts[:config]['before']
80
36
  end
81
37
 
82
38
 
@@ -98,7 +54,7 @@ module Zfben_libjs
98
54
  if @libs.has_key?(url) && name != url
99
55
  lib.push(url)
100
56
  else
101
- p path = File.join(@config['src/source'], name, File.basename(url))
57
+ p path = File.join(@opts[:config]['src/source'], name, File.basename(url))
102
58
  dir = File.dirname(path)
103
59
  system('mkdir ' + dir) unless File.exists?(dir)
104
60
  download url, path
@@ -173,12 +129,12 @@ module Zfben_libjs
173
129
  file
174
130
  }.compact
175
131
  if css != ''
176
- file = File.join(@config['src/source'], name + '.css')
132
+ file = File.join(@opts[:config]['src/source'], name + '.css')
177
133
  File.open(file, 'w'){ |f| f.write(css) }
178
134
  lib.push(file)
179
135
  end
180
136
  if js != ''
181
- file = File.join(@config['src/source'], name + '.js')
137
+ file = File.join(@opts[:config]['src/source'], name + '.js')
182
138
  File.open(file, 'w'){ |f| f.write(js) }
183
139
  lib.push(file)
184
140
  end
@@ -194,22 +150,22 @@ module Zfben_libjs
194
150
  type = 'images'
195
151
  end
196
152
 
197
- path = File.join(@config['src/' + type], File.basename(file))
153
+ path = File.join(@opts[:config]['src/' + type], File.basename(file))
198
154
 
199
155
  tip '=> ' + path
200
156
 
201
157
  system('cp ' + file + ' ' + path)
202
158
 
203
159
  reg = /url\("?'?([^'")]+)'?"?\)/
204
- if type == 'stylesheets' && @config['changeImageUrl'] && reg =~ File.read(path)
160
+ if type == 'stylesheets' && @opts[:config]['changeImageUrl'] && reg =~ File.read(path)
205
161
  css = File.read(path).partition_all(reg).map{ |f|
206
162
  if reg =~ f
207
163
  filename = File.basename(f.match(reg)[1])
208
- if File.exists?(File.join(@config['src/images'], filename))
209
- if @config['url'] == ''
164
+ if File.exists?(File.join(@opts[:config]['src/images'], filename))
165
+ if @opts[:config]['url'] == ''
210
166
  url = '../images/' << File.basename(f.match(reg)[1])
211
167
  else
212
- url = @config['url/images'] + File.basename(f.match(reg)[1])
168
+ url = @opts[:config]['url/images'] + File.basename(f.match(reg)[1])
213
169
  end
214
170
  f = 'url("' << url << '")'
215
171
  end
@@ -222,7 +178,7 @@ module Zfben_libjs
222
178
  path = nil
223
179
  end
224
180
 
225
- if @config['minify']
181
+ if @opts[:config]['minify']
226
182
  if type == 'stylesheets'
227
183
  min = minify(File.read(path), :css)
228
184
  File.open(path, 'w'){ |f| f.write(min) }
@@ -257,9 +213,9 @@ module Zfben_libjs
257
213
  path = path.map{ |url|
258
214
  case File.extname(url)
259
215
  when '.css'
260
- url = @config['url/stylesheets'] + '/' + File.basename(url)
216
+ url = @opts[:config]['url/stylesheets'] + '/' + File.basename(url)
261
217
  when '.js'
262
- url = @config['url/javascripts'] + '/' + File.basename(url)
218
+ url = @opts[:config]['url/javascripts'] + '/' + File.basename(url)
263
219
  else
264
220
  url = nil
265
221
  end
@@ -269,13 +225,11 @@ module Zfben_libjs
269
225
  end
270
226
 
271
227
  libjs << "\n/* libs */\nlib.libs(#{@urls.to_json});lib.loaded('add', 'lazyload');"
228
+ libjs << Time.now.strftime('lib.defaults.version = "?%s";')
272
229
 
273
- if @config['autoVersion']
274
- libjs << Time.now.strftime('lib.defaults.version = "?%s";')
275
- end
276
-
277
- if @bundle != nil && @bundle.length > 0
278
- @bundle.each do |name, libs|
230
+ if @opts.has_key?(:bundle)
231
+ bundle = {}
232
+ @opts[:bundle].each do |name, libs|
279
233
  css = ''
280
234
  js = ''
281
235
  files = []
@@ -296,69 +250,50 @@ module Zfben_libjs
296
250
  path = []
297
251
 
298
252
  if css != ''
299
- file = File.join(@config['src/stylesheets'], name + '.css')
253
+ file = File.join(@opts[:config]['src/stylesheets'], name + '.css')
300
254
  File.open(file, 'w'){ |f| f.write(css) }
301
- path.push(@config['url/stylesheets'] + '/' + File.basename(file))
255
+ path.push(@opts[:config]['url/stylesheets'] + '/' + File.basename(file))
302
256
  end
303
257
 
304
258
  if js != ''
305
- files_url = files.map{ |f| @config['url/javascripts'] + '/' + File.basename(f) }.join("','")
259
+ files_url = files.map{ |f| @opts[:config]['url/javascripts'] + '/' + File.basename(f) }.join("','")
306
260
  js << "\nif(typeof lib === 'function'){lib.loaded('add', '#{files_url}');}"
307
- file = File.join(@config['src/javascripts'], name + '.js')
261
+ file = File.join(@opts[:config]['src/javascripts'], name + '.js')
308
262
  File.open(file, 'w'){ |f| f.write(js) }
309
- path.push(@config['url/javascripts'] + '/' + File.basename(file))
263
+ path.push(@opts[:config]['url/javascripts'] + '/' + File.basename(file))
310
264
  end
311
265
 
312
266
  if path.length > 0
313
267
  path = path[0] if path.length == 0
314
- @bundle[name] = path
315
- else
316
- @bundle.delete(name)
268
+ bundle[name] = path
317
269
  end
318
270
  end
319
271
 
320
- libjs << "\n/* bundle */\nlib.libs(#{@bundle.to_json});"
272
+ libjs << "\n/* bundle */\nlib.libs(#{bundle.to_json});"
321
273
  end
322
274
 
323
- if defined?(@routes) && !@routes.nil?
275
+ if @opts.has_key?(:routes)
324
276
  routes = {}
325
- @routes.each do |path, lib_name|
277
+ @opts[:routes].each do |path, lib_name|
326
278
  lib_name = lib_name.join ' ' if lib_name.class == Array
327
279
  routes[path] = lib_name
328
280
  end
329
281
  libjs << "\n/* routes */\nlib.routes('add', #{routes.to_json});"
330
282
  end
331
283
 
332
- if @preload.class == Array && @preload.length > 0
333
- libjs << "\n/* preload */\nlib('#{@preload.join(' ')}');"
284
+ if @opts.has_key?(:preload)
285
+ preload = @opts[:preload].class == Array ? @opts[:preload] : [ @opts[:preload] ]
286
+ libjs << "\n/* preload */\nlib('#{preload.join(' ')}');"
334
287
  end
335
288
 
336
- libjs = minify(libjs, :js) if @config['minify']
337
- File.open(File.join(@config['src/javascripts'], 'lib.js'), 'w'){ |f| f.write(libjs) }
289
+ libjs = minify(libjs, :js) if @opts[:config]['minify']
290
+ File.open(File.join(@opts[:config]['src/javascripts'], 'lib.js'), 'w'){ |f| f.write(libjs) }
338
291
 
339
- if @config.has_key?('after')
340
- load @config['after']
292
+ if @opts[:config].has_key?('after')
293
+ load @opts[:config]['after']
341
294
  end
342
295
 
343
296
  tip '== End Build =='
344
297
  end
345
298
  end
346
-
347
- Lib_version ||= Time.now.strftime('?%s')
348
-
349
- if defined?(Rails)
350
- module Rails
351
- module ActionView::Helpers::AssetTagHelper
352
- def lib *opts
353
- p opts
354
- html = '<script src="/javascripts/lib.js' + Lib_version + '"></script>'
355
- unless opts.blank?
356
- html += "<script>lib('#{opts.join(' ')}')</script>"
357
- end
358
- return html
359
- end
360
- end
361
- end
362
- end
363
299
  end
364
-
@@ -0,0 +1,6 @@
1
+ class TestLibjs < Test::Unit::TestCase
2
+ def test
3
+ assert_equal Zfben_libjs::Libjs.new.opts, Zfben_libjs::Libjs.defaults
4
+ assert_equal Zfben_libjs::Libjs.new('test').opts, Zfben_libjs::Libjs.new('t').opts
5
+ end
6
+ end
data/test.yml CHANGED
@@ -4,7 +4,6 @@ config:
4
4
  download: false # if file isn't exists, it will be downloaded always
5
5
  minify: false # use uglifier and sass engine to minify files
6
6
  changeImageUrl: true # change image urls in css
7
- autoVersion: true # add version to files url
8
7
 
9
8
  # Files to be download and writen in lib.js
10
9
  libs:
data/zfben_libjs.gemspec CHANGED
@@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = 'zfben_libjs'
6
- s.version = '0.0.11'
6
+ s.version = '0.0.12'
7
7
  s.authors = ["Ben"]
8
8
  s.email = ["ben@zfben.com"]
9
9
  s.homepage = "https://github.com/benz303/zfben_libjs"
@@ -30,5 +30,6 @@ Gem::Specification.new do |s|
30
30
  s.add_dependency 'compass'
31
31
  s.add_dependency 'coffee-script'
32
32
  s.add_dependency 'uglifier'
33
+ s.add_dependency 'active_support'
33
34
 
34
35
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zfben_libjs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.11
4
+ version: 0.0.12
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-08-21 00:00:00.000000000Z
12
+ date: 2011-08-29 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rainbow
16
- requirement: &17831320 !ruby/object:Gem::Requirement
16
+ requirement: &20317000 !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: *17831320
24
+ version_requirements: *20317000
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: json
27
- requirement: &17830860 !ruby/object:Gem::Requirement
27
+ requirement: &20316540 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *17830860
35
+ version_requirements: *20316540
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: compass
38
- requirement: &17830440 !ruby/object:Gem::Requirement
38
+ requirement: &20316120 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *17830440
46
+ version_requirements: *20316120
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: coffee-script
49
- requirement: &17830020 !ruby/object:Gem::Requirement
49
+ requirement: &20315700 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *17830020
57
+ version_requirements: *20315700
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: uglifier
60
- requirement: &17829600 !ruby/object:Gem::Requirement
60
+ requirement: &20315280 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,7 +65,18 @@ dependencies:
65
65
  version: '0'
66
66
  type: :runtime
67
67
  prerelease: false
68
- version_requirements: *17829600
68
+ version_requirements: *20315280
69
+ - !ruby/object:Gem::Dependency
70
+ name: active_support
71
+ requirement: &20314860 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :runtime
78
+ prerelease: false
79
+ version_requirements: *20314860
69
80
  description: ''
70
81
  email:
71
82
  - ben@zfben.com
@@ -79,9 +90,11 @@ files:
79
90
  - Rakefile
80
91
  - bin/libjs
81
92
  - lib/zfben_libjs.rb
93
+ - lib/zfben_libjs/initialize.rb
82
94
  - lib/zfben_libjs/lib.coffee
83
95
  - lib/zfben_libjs/lib.rb
84
96
  - lib/zfben_libjs/libjs.yml
97
+ - lib/zfben_libjs/railtie.rb
85
98
  - test.yml
86
99
  - test/javascript/assets/callback.js
87
100
  - test/javascript/fixtures/lib_fixtures.html
@@ -90,6 +103,7 @@ files:
90
103
  - test/load_times.js
91
104
  - test/route_regexp.js
92
105
  - test/route_string.js
106
+ - test/ruby/initialize_test.rb
93
107
  - test/support_filetype/coffee.coffee
94
108
  - test/support_filetype/css.css
95
109
  - test/support_filetype/js.js