Class: SmartAsset
- Inherits:
-
Object
- Object
- SmartAsset
- Defined in:
- lib/smart_asset.rb,
lib/smart_asset/helper.rb,
lib/smart_asset/adapters/rails3.rb,
lib/smart_asset/adapters/stasis.rb,
lib/smart_asset/adapters/sinatra.rb
Defined Under Namespace
Modules: Adapters, Helper Classes: SmartAssetRailtie, Stasis
Constant Summary collapse
- BIN =
File.(File.dirname(__FILE__) + '/../bin')
- YUI_COMPRESSOR =
BIN + '/yuicompressor-2.4.6.jar'
Class Attribute Summary collapse
-
.append_random ⇒ Object
Returns the value of attribute append_random.
-
.asset_counter ⇒ Object
Returns the value of attribute asset_counter.
-
.asset_host ⇒ Object
Returns the value of attribute asset_host.
-
.cache ⇒ Object
Returns the value of attribute cache.
-
.config ⇒ Object
Returns the value of attribute config.
-
.dest ⇒ Object
Returns the value of attribute dest.
-
.env ⇒ Object
Returns the value of attribute env.
-
.envs ⇒ Object
Returns the value of attribute envs.
-
.pub ⇒ Object
Returns the value of attribute pub.
-
.root ⇒ Object
Returns the value of attribute root.
-
.sources ⇒ Object
Returns the value of attribute sources.
Class Method Summary collapse
- .binary(root, relative_config = nil) ⇒ Object
- .compress(type) ⇒ Object
- .load_config(root, relative_config = nil) ⇒ Object
- .paths(type, match) ⇒ Object
- .prepend_asset_host(path) ⇒ Object
Class Attribute Details
.append_random ⇒ Object
Returns the value of attribute append_random.
13 14 15 |
# File 'lib/smart_asset.rb', line 13 def append_random @append_random end |
.asset_counter ⇒ Object
Returns the value of attribute asset_counter.
13 14 15 |
# File 'lib/smart_asset.rb', line 13 def asset_counter @asset_counter end |
.asset_host ⇒ Object
Returns the value of attribute asset_host.
13 14 15 |
# File 'lib/smart_asset.rb', line 13 def asset_host @asset_host end |
.cache ⇒ Object
Returns the value of attribute cache.
13 14 15 |
# File 'lib/smart_asset.rb', line 13 def cache @cache end |
.config ⇒ Object
Returns the value of attribute config.
13 14 15 |
# File 'lib/smart_asset.rb', line 13 def config @config end |
.dest ⇒ Object
Returns the value of attribute dest.
13 14 15 |
# File 'lib/smart_asset.rb', line 13 def dest @dest end |
.env ⇒ Object
Returns the value of attribute env.
13 14 15 |
# File 'lib/smart_asset.rb', line 13 def env @env end |
.envs ⇒ Object
Returns the value of attribute envs.
13 14 15 |
# File 'lib/smart_asset.rb', line 13 def envs @envs end |
.pub ⇒ Object
Returns the value of attribute pub.
13 14 15 |
# File 'lib/smart_asset.rb', line 13 def pub @pub end |
.root ⇒ Object
Returns the value of attribute root.
13 14 15 |
# File 'lib/smart_asset.rb', line 13 def root @root end |
.sources ⇒ Object
Returns the value of attribute sources.
13 14 15 |
# File 'lib/smart_asset.rb', line 13 def sources @sources end |
Class Method Details
.binary(root, relative_config = nil) ⇒ Object
18 19 20 21 22 |
# File 'lib/smart_asset.rb', line 18 def binary(root, relative_config=nil) load_config root, relative_config compress 'javascripts' compress 'stylesheets' end |
.compress(type) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/smart_asset.rb', line 24 def compress(type) dest = @dest[type] dir = "#{@pub}/#{@sources[type]}" ext = ext_from_type(type) packages = [] time_cache = {} FileUtils.mkdir_p dest (@config[type] || {}).each do |package, files| next if ENV['PACKAGE'] && ENV['PACKAGE'] != package if files # Retrieve list of Git modified timestamps = [] files.each do |file| file_path = "#{dir}/#{file}.#{ext}" if File.exists?(file_path) if time_cache[file] time = time_cache[file] else time = `cd #{@root} && git log --pretty=format:%cd -n 1 --date=iso #{@config['public']}/#{@sources[type]}/#{file}.#{ext}` if time.strip.empty? || time.include?('command not found') time = ENV['MODIFIED'] ? Time.parse(ENV['MODIFIED']) : Time.now else time = Time.parse(time) end time = time.utc.strftime("%Y%m%d%H%M%S") time += file.to_s time_cache[file] = time end << time else $stderr.puts "WARNING: '#{file_path}' was not found." end end next if .empty? # Modified hash hash = Digest::SHA1.hexdigest(.join)[0..7] # Package path package = "#{dest}/#{hash}_#{package}.#{ext}" # If package file exists if File.exists?(package) packages << package else data = [] # Join files in package files.each do |file| if File.exists?(source = "#{dir}/#{file}.#{ext}") data << File.read(source) end end # Don't create new compressed file if no data data = data.join("\n") next if data.strip.empty? # Compress joined files tmp = "#{dest}/tmp.#{ext}" File.open(tmp, 'w') { |f| f.write(data) } puts "\nCreating #{package}..." warning = ENV['WARN'] ? " -v" : nil if ext == 'js' cmd = "uglifyjs --output #{package}#{warning} -nc #{tmp}" elsif ext == 'css' cmd = "java -jar #{YUI_COMPRESSOR} #{tmp} -o #{package}#{warning}" end puts cmd if ENV['DEBUG'] `#{cmd}` FileUtils.rm(tmp) unless ENV['DEBUG'] # Fix YUI compression issue if ext == 'css' if RUBY_PLATFORM.downcase.include?('darwin') `sed -i '' 's/ and(/ and (/g' #{package}` else `sed -i 's/ and(/ and (/g' #{package}` end end # Package created packages << package end end end # Remove old/unused packages (Dir["#{dest}/#{"[^_]"*8}_*.#{ext}"] - packages).each do |path| FileUtils.rm path end # Delete legacy files Dir["#{dest}/*.yml", "#{dest}/#{"[0-9]"*14}_*.{css,js}"].each do |path| FileUtils.rm path end end |
.load_config(root, relative_config = nil) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/smart_asset.rb', line 124 def load_config(root, relative_config=nil) relative_config ||= 'config/assets.yml' @root = File.(root) return unless File.file?("#{@root}/#{relative_config}") @cache = nil @config = YAML::load(File.read("#{@root}/#{relative_config}")) # Default values if @config['append_random'].nil? @config['append_random'] = {} end if @config['append_random'].is_a?(::Hash) && @config['append_random']['development'].nil? @config['append_random']['development'] = true end @config['asset_host_count'] ||= 4 @config['asset_host'] ||= ActionController::Base.asset_host rescue nil @config['environments'] ||= %w(production) @config['public'] ||= 'public' @config['destination'] ||= {} @config['destination']['javascripts'] ||= 'javascripts/packaged' @config['destination']['stylesheets'] ||= 'stylesheets/packaged' @config['sources'] ||= {} @config['sources']['javascripts'] ||= "javascripts" @config['sources']['stylesheets'] ||= "stylesheets" # Convert from asset packager syntax %w(javascripts stylesheets).each do |type| if @config[type].respond_to?(:pop) @config[type] = @config[type].inject({}) do |hash, package| hash.merge! package end end end # Class variables @append_random = if @config['append_random'].is_a?(::Hash) @config['append_random'][@env] else @config['append_random'] end @asset_host = @config['asset_host'] @envs = @config['environments'] @sources = @config['sources'] @pub = File.("#{@root}/#{@config['public']}") @dest = %w(javascripts stylesheets).inject({}) do |hash, type| hash[type] = "#{@pub}/#{@config['destination'][type]}" hash end end |
.paths(type, match) ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/smart_asset.rb', line 182 def paths(type, match) match = match.to_s @cache ||= {} @cache[type] ||= {} if @cache[type][match] return @cache[type][match] end dest = @dest[type] ext = ext_from_type type if @envs.include?(@env.to_s) @cache[type][match] = if result = Dir["#{dest}/#{"[^_]"*8}_#{match}.#{ext}"].first [ result.gsub(@pub, '') ] else [] end elsif @config && @config[type] result = @config[type].collect do |package, files| if package.to_s == match files.collect do |file| file = "/#{@sources[type]}/#{file}.#{ext}" file if File.exists?("#{@pub}/#{file}") end end end result.flatten.compact.uniq end end |
.prepend_asset_host(path) ⇒ Object
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/smart_asset.rb', line 215 def prepend_asset_host(path) if @asset_host.respond_to?(:keys) host = @asset_host[@env.to_s] else host = @asset_host end if host if !@asset_counter || @asset_counter == @config['asset_host_count'] @asset_counter = 0 end count = @asset_counter.to_s @asset_counter += 1 host.gsub('%d', count) + path else path end end |