smart_asset 0.4.7 → 0.5.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.4.7" unless defined?(::SmartAsset::VERSION)
2
+ VERSION = "0.5.0" unless defined?(::SmartAsset::VERSION)
3
3
  end
data/lib/smart_asset.rb CHANGED
@@ -2,6 +2,7 @@ require File.dirname(__FILE__) + '/smart_asset/gems'
2
2
 
3
3
  SmartAsset::Gems.require(:lib)
4
4
 
5
+ require 'digest'
5
6
  require 'fileutils'
6
7
  require 'time'
7
8
  require 'yaml'
@@ -29,14 +30,9 @@ class SmartAsset
29
30
  def compress(type)
30
31
  dest = @dest[type]
31
32
  dir = "#{@pub}/#{@sources[type]}"
32
- ext = ext_from_type type
33
- version = {}
34
- cache = {}
35
-
36
- # Read version yaml
37
- if File.exists?(version_path = "#{dest}/#{type}.yml")
38
- version = YAML::load(File.read(version_path))
39
- end
33
+ ext = ext_from_type(type)
34
+ packages = []
35
+ time_cache = {}
40
36
 
41
37
  FileUtils.mkdir_p dest
42
38
 
@@ -44,24 +40,36 @@ class SmartAsset
44
40
  next if ENV['PACKAGE'] && ENV['PACKAGE'] != package
45
41
  if files
46
42
  # Retrieve list of Git modified timestamps
47
- modified = []
43
+ timestamps = []
48
44
  files.each do |file|
49
- fname = "#{dir}/#{file}.#{ext}"
50
- if File.exists?(fname)
51
- mod = (cache[fname] ||= `cd #{@root} && git log --pretty=format:%cd -n 1 --date=iso #{@config['public']}/#{@sources[type]}/#{file}.#{ext}`)
52
- if mod.strip.empty? || mod.include?('command not found')
53
- modified << (ENV['MODIFIED'] ? Time.parse(ENV['MODIFIED']) : Time.now).utc.strftime("%Y%m%d%H%M%S")
45
+ if File.exists?("#{dir}/#{file}.#{ext}")
46
+ if time_cache[file]
47
+ time = time_cache[file]
54
48
  else
55
- modified << Time.parse(mod).utc.strftime("%Y%m%d%H%M%S")
49
+ time = `cd #{@root} && git log --pretty=format:%cd -n 1 --date=iso #{@config['public']}/#{@sources[type]}/#{file}.#{ext}`
50
+ if time.strip.empty? || time.include?('command not found')
51
+ time = ENV['MODIFIED'] ? Time.parse(ENV['MODIFIED']) : Time.now
52
+ else
53
+ time = Time.parse(time)
54
+ end
55
+ time = time.utc.strftime("%Y%m%d%H%M%S")
56
+ time += file.to_s
57
+ time_cache[file] = time
56
58
  end
59
+ timestamps << time
57
60
  end
58
61
  end
59
- modified = modified.sort.last
60
- next unless modified
62
+ next if timestamps.empty?
63
+
64
+ # Modified hash
65
+ hash = Digest::SHA1.hexdigest(timestamps.join)[0..7]
61
66
 
62
- # If package changed or package file doesn't exist
63
- output = "#{dest}/#{modified}_#{package}.#{ext}"
64
- if version[package] != files || !File.exists?(output)
67
+ # Package path
68
+ package = "#{dest}/#{hash}_#{package}.#{ext}"
69
+ packages << package
70
+
71
+ # If package file does not exist
72
+ unless File.exists?(package)
65
73
  data = []
66
74
 
67
75
  # Join files in package
@@ -71,24 +79,20 @@ class SmartAsset
71
79
  end
72
80
  end
73
81
 
74
- # Delete old package
75
- Dir["#{dest}/*[0-9]_#{package}.#{ext}"].each do |old|
76
- FileUtils.rm old
77
- end
78
-
79
82
  # Don't create new compressed file if no data
80
- next if data.empty?
83
+ data = data.join("\n")
84
+ next if data.strip.empty?
81
85
 
82
86
  # Compress joined files
83
87
  tmp = "#{dest}/tmp.#{ext}"
84
- File.open(tmp, 'w') { |f| f.write(data.join("\n")) }
85
- puts "\nCreating #{output}..."
88
+ File.open(tmp, 'w') { |f| f.write(data) }
89
+ puts "\nCreating #{package}..."
86
90
  if ext == 'js'
87
91
  warning = ENV['WARN'] ? nil : " --warning_level QUIET"
88
- cmd = "java -jar #{CLOSURE_COMPILER} --js #{tmp} --js_output_file #{output}#{warning}"
92
+ cmd = "java -jar #{CLOSURE_COMPILER} --js #{tmp} --js_output_file #{package}#{warning}"
89
93
  elsif ext == 'css'
90
94
  warning = ENV['WARN'] ? " -v" : nil
91
- cmd = "java -jar #{YUI_COMPRESSOR} #{tmp} -o #{output}#{warning}"
95
+ cmd = "java -jar #{YUI_COMPRESSOR} #{tmp} -o #{package}#{warning}"
92
96
  end
93
97
  puts cmd if ENV['DEBUG']
94
98
  `#{cmd}`
@@ -97,25 +101,23 @@ class SmartAsset
97
101
  # Fix YUI compression issue
98
102
  if ext == 'css'
99
103
  if RUBY_PLATFORM.downcase.include?('darwin')
100
- `sed -i '' 's/ and(/ and (/g' #{output}`
104
+ `sed -i '' 's/ and(/ and (/g' #{package}`
101
105
  else
102
- `sed -i 's/ and(/ and (/g' #{output}`
106
+ `sed -i 's/ and(/ and (/g' #{package}`
103
107
  end
104
108
  end
105
109
  end
106
110
  end
107
111
  end
108
112
 
109
- # Delete removed packages
110
- (version.keys - (@config[type] || {}).keys).each do |package|
111
- Dir["#{dest}/*[0-9]_#{package}.#{ext}"].each do |old|
112
- FileUtils.rm old
113
- end
113
+ # Remove old/unused packages
114
+ (Dir["#{dest}/????????_*.#{ext}"] - packages).each do |path|
115
+ FileUtils.rm path
114
116
  end
115
117
 
116
- # Write version yaml file
117
- if @config[type]
118
- File.open(version_path, 'w') { |f| f.write(YAML::dump(@config[type])) }
118
+ # Delete legacy yml files
119
+ Dir["#{dest}/*.yml"].each do |path|
120
+ FileUtils.rm path
119
121
  end
120
122
  end
121
123
 
@@ -188,7 +190,7 @@ class SmartAsset
188
190
 
189
191
  if @envs.include?(@env.to_s)
190
192
  @cache[type][match] =
191
- if result = Dir["#{dest}/*[0-9]_#{match}.#{ext}"].sort.last
193
+ if result = Dir["#{dest}/????????_#{match}.#{ext}"].sort.last
192
194
  [ result.gsub(@pub, '') ]
193
195
  else
194
196
  []
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 4
8
- - 7
9
- version: 0.4.7
7
+ - 5
8
+ - 0
9
+ version: 0.5.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-16 00:00:00 -08:00
17
+ date: 2010-12-20 00:00:00 -08:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency