smart_asset 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,3 @@
1
1
  class SmartAsset
2
- VERSION = "0.1.3" unless defined?(::SmartAsset::VERSION)
2
+ VERSION = "0.2.0" unless defined?(::SmartAsset::VERSION)
3
3
  end
data/lib/smart_asset.rb CHANGED
@@ -14,7 +14,7 @@ require 'smart_asset/version'
14
14
  class SmartAsset
15
15
  class <<self
16
16
 
17
- attr_accessor :asset_host, :cache, :config, :dest, :env, :pub, :root, :sources
17
+ attr_accessor :asset_host, :cache, :config, :dest, :env, :envs, :pub, :root, :sources
18
18
 
19
19
  BIN = File.expand_path(File.dirname(__FILE__) + '/../bin')
20
20
  CLOSURE_COMPILER = BIN + '/closure_compiler.jar'
@@ -27,9 +27,10 @@ class SmartAsset
27
27
  end
28
28
 
29
29
  def compress(type)
30
+ dest = @dest[type]
30
31
  dir = "#{@pub}/#{@sources[type]}"
31
32
  ext = ext_from_type type
32
- FileUtils.mkdir_p @dest
33
+ FileUtils.mkdir_p dest
33
34
  (@config[type] || {}).each do |package, files|
34
35
  create_package = false
35
36
  compressed = {}
@@ -43,24 +44,27 @@ class SmartAsset
43
44
  modified = Time.parse(modified).utc.strftime("%Y%m%d%H%M%S")
44
45
  end
45
46
  file = file.to_s.gsub('/', '_')
46
- unless File.exists?(destination = "#{@dest}/#{modified}_#{file}.#{ext}")
47
+ unless File.exists?(destination = "#{dest}/#{modified}_#{package}_#{file}.#{ext}")
47
48
  create_package = true
48
- Dir["#{@dest}/*_#{file}.#{ext}"].each do |old|
49
+ Dir["#{dest}/*[0-9]_#{package}_#{file}.#{ext}"].each do |old|
49
50
  FileUtils.rm old
50
51
  end
51
52
  puts "\nCompressing #{source}..."
52
53
  if ext == 'js'
53
- `java -jar #{CLOSURE_COMPILER} --js #{source} --js_output_file #{destination} --warning_level QUIET`
54
+ warning = ENV['WARN'] ? nil : " --warning_level QUIET"
55
+ cmd = "java -jar #{CLOSURE_COMPILER} --js #{source} --js_output_file #{destination}#{warn}"
54
56
  elsif ext == 'css'
55
- `java -jar #{YUI_COMPRESSOR} #{source} -o #{destination}`
57
+ cmd = "java -jar #{YUI_COMPRESSOR} #{source} -o #{destination}"
56
58
  end
59
+ puts cmd if ENV['DEBUG']
60
+ `#{cmd}`
57
61
  end
58
62
  compressed[destination] = modified
59
63
  end
60
64
  end
61
65
  if modified = compressed.values.compact.sort.last
62
- old_packages = "#{@dest}/*_#{package}.#{ext}"
63
- package = "#{@dest}/#{modified}_#{package}.#{ext}"
66
+ old_packages = "#{dest}/*[0-9]_#{package}.#{ext}"
67
+ package = "#{dest}/#{modified}_#{package}.#{ext}"
64
68
  if create_package || !File.exists?(package)
65
69
  Dir[old_packages].each do |old|
66
70
  FileUtils.rm old
@@ -81,17 +85,33 @@ class SmartAsset
81
85
  @config = YAML::load(File.read("#{@root}/#{relative_config}"))
82
86
 
83
87
  @config['asset_host'] ||= ActionController::Base.asset_host rescue nil
88
+ @config['environments'] ||= %w(production)
84
89
  @config['public'] ||= 'public'
85
- @config['destination'] ||= 'packaged'
90
+ @config['destination'] ||= {}
91
+ @config['destination']['javascripts'] ||= 'javascripts/packaged'
92
+ @config['destination']['stylesheets'] ||= 'stylesheets/packaged'
86
93
  @config['sources'] ||= {}
87
94
  @config['sources']['javascripts'] ||= "javascripts"
88
95
  @config['sources']['stylesheets'] ||= "stylesheets"
89
96
 
97
+ # Convert from asset packager syntax
98
+ %w(javascripts stylesheets).each do |type|
99
+ if @config[type].respond_to?(:pop)
100
+ @config[type] = @config[type].inject({}) do |hash, package|
101
+ hash.merge! package
102
+ end
103
+ end
104
+ end
105
+
90
106
  @asset_host = @config['asset_host']
107
+ @envs = @config['environments']
91
108
  @sources = @config['sources']
92
109
 
93
110
  @pub = File.expand_path("#{@root}/#{@config['public']}")
94
- @dest = "#{@pub}/#{@config['destination']}"
111
+ @dest = %w(javascripts stylesheets).inject({}) do |hash, type|
112
+ hash[type] = "#{@pub}/#{@config['destination'][type]}"
113
+ hash
114
+ end
95
115
  end
96
116
 
97
117
  def paths(type, match)
@@ -109,12 +129,13 @@ class SmartAsset
109
129
  @asset_host[@env.to_s] :
110
130
  @asset_host
111
131
 
132
+ dest = @dest[type]
112
133
  ext = ext_from_type type
113
134
 
114
- if @env.intern == :production
135
+ if @envs.include?(@env.to_s)
115
136
  match = match.gsub('/', '_')
116
137
  @cache[type][match] =
117
- if result = Dir["#{@dest}/*_#{match}.#{ext}"].sort.last
138
+ if result = Dir["#{dest}/*_#{match}.#{ext}"].sort.last
118
139
  [ "#{host}#{result.gsub(@pub, '')}" ]
119
140
  else
120
141
  []
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
8
- - 3
9
- version: 0.1.3
7
+ - 2
8
+ - 0
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Winton Welsh
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-12-06 00:00:00 -08:00
17
+ date: 2010-12-07 00:00:00 -08:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency