Class: Condenser::Asset
- Inherits:
-
Object
- Object
- Condenser::Asset
- Includes:
- EncodingUtils
- Defined in:
- lib/condenser/asset.rb
Constant Summary
Constants included from EncodingUtils
EncodingUtils::BOM, EncodingUtils::CHARSET_SIZE, EncodingUtils::CHARSET_START
Instance Attribute Summary collapse
-
#content_types ⇒ Object
readonly
Returns the value of attribute content_types.
-
#content_types_digest ⇒ Object
readonly
Returns the value of attribute content_types_digest.
-
#environment ⇒ Object
readonly
Returns the value of attribute environment.
-
#export ⇒ Object
Returns the value of attribute export.
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#imports ⇒ Object
Returns the value of attribute imports.
-
#linked_assets ⇒ Object
readonly
Returns the value of attribute linked_assets.
-
#processed ⇒ Object
Returns the value of attribute processed.
- #source ⇒ Object
-
#source_file ⇒ Object
readonly
Returns the value of attribute source_file.
-
#source_path ⇒ Object
readonly
Returns the value of attribute source_path.
- #sourcemap ⇒ Object
Instance Method Summary collapse
- #all_dependenies(deps, visited, meth, &block) ⇒ Object
- #all_export_dependencies ⇒ Object
- #all_process_dependencies ⇒ Object
- #basepath ⇒ Object
- #cache_key ⇒ Object
- #charset ⇒ Object
- #content_type ⇒ Object
- #digest ⇒ Object
-
#eql?(other) ⇒ Boolean
(also: #==)
Public: Compare assets.
- #export_cache_version ⇒ Object
- #export_dependencies ⇒ Object
- #ext ⇒ Object
- #has_default_export? ⇒ Boolean
- #has_exports? ⇒ Boolean
-
#hexdigest ⇒ Object
(also: #etag)
Public: Returns String hexdigest of source.
-
#initialize(env, attributes = {}) ⇒ Asset
constructor
A new instance of Asset.
- #inspect ⇒ Object
- #integrity ⇒ Object
- #length ⇒ Object (also: #size)
- #load_processors ⇒ Object
- #needs_reexporting! ⇒ Object
- #needs_reprocessing! ⇒ Object
- #path ⇒ Object
- #process ⇒ Object
- #process_cache_version ⇒ Object
- #process_dependencies ⇒ Object
- #restat! ⇒ Object
- #stat ⇒ Object
- #to_json ⇒ Object
- #to_s ⇒ Object
- #write(output_directory) ⇒ Object
Methods included from EncodingUtils
#detect, #detect_css, #detect_html, #detect_unicode, #detect_unicode_bom, #scan_css_charset
Constructor Details
#initialize(env, attributes = {}) ⇒ Asset
Returns a new instance of Asset.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/condenser/asset.rb', line 18 def initialize(env, attributes={}) @environment = env @filename = attributes[:filename] @content_types = Array(attributes[:content_types] || attributes[:content_type]) @content_types_digest = Digest::SHA1.base64digest(@content_types.join(':')) @source_file = attributes[:source_file] @source_path = attributes[:source_path] @linked_assets = Set.new @process_dependencies = Set.new @export_dependencies = Set.new @default_export = nil @exports = nil @processed = false @pcv = nil @export = nil @ecv = nil @processors_loaded = false @processors = Set.new end |
Instance Attribute Details
#content_types ⇒ Object (readonly)
Returns the value of attribute content_types.
12 13 14 |
# File 'lib/condenser/asset.rb', line 12 def content_types @content_types end |
#content_types_digest ⇒ Object (readonly)
Returns the value of attribute content_types_digest.
13 14 15 |
# File 'lib/condenser/asset.rb', line 13 def content_types_digest @content_types_digest end |
#environment ⇒ Object (readonly)
Returns the value of attribute environment.
12 13 14 |
# File 'lib/condenser/asset.rb', line 12 def environment @environment end |
#export ⇒ Object
Returns the value of attribute export.
16 17 18 |
# File 'lib/condenser/asset.rb', line 16 def export @export end |
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
12 13 14 |
# File 'lib/condenser/asset.rb', line 12 def filename @filename end |
#imports ⇒ Object
Returns the value of attribute imports.
16 17 18 |
# File 'lib/condenser/asset.rb', line 16 def imports @imports end |
#linked_assets ⇒ Object (readonly)
Returns the value of attribute linked_assets.
13 14 15 |
# File 'lib/condenser/asset.rb', line 13 def linked_assets @linked_assets end |
#processed ⇒ Object
Returns the value of attribute processed.
16 17 18 |
# File 'lib/condenser/asset.rb', line 16 def processed @processed end |
#source ⇒ Object
345 346 347 348 |
# File 'lib/condenser/asset.rb', line 345 def source process @source end |
#source_file ⇒ Object (readonly)
Returns the value of attribute source_file.
12 13 14 |
# File 'lib/condenser/asset.rb', line 12 def source_file @source_file end |
#source_path ⇒ Object (readonly)
Returns the value of attribute source_path.
12 13 14 |
# File 'lib/condenser/asset.rb', line 12 def source_path @source_path end |
#sourcemap ⇒ Object
350 351 352 353 |
# File 'lib/condenser/asset.rb', line 350 def sourcemap process @sourcemap end |
Instance Method Details
#all_dependenies(deps, visited, meth, &block) ⇒ Object
120 121 122 123 124 125 126 127 128 |
# File 'lib/condenser/asset.rb', line 120 def all_dependenies(deps, visited, meth, &block) deps.each do |dep| if !visited.include?(dep.source_file) visited << dep.source_file block.call(dep) all_dependenies(dep.send(meth), visited, meth, &block) end end end |
#all_export_dependencies ⇒ Object
138 139 140 141 142 143 144 |
# File 'lib/condenser/asset.rb', line 138 def all_export_dependencies f = [@source_file] all_dependenies(export_dependencies, [], :export_dependencies) do |dep| f << dep.source_file end f end |
#all_process_dependencies ⇒ Object
130 131 132 133 134 135 136 |
# File 'lib/condenser/asset.rb', line 130 def all_process_dependencies f = [@source_file] all_dependenies(process_dependencies, [], :process_dependencies) do |dep| f << dep.source_file end f end |
#basepath ⇒ Object
49 50 51 52 |
# File 'lib/condenser/asset.rb', line 49 def basepath dirname, basename, extensions, mime_types = @environment.decompose_path(filename) [dirname, basename].compact.join('/') end |
#cache_key ⇒ Object
146 147 148 149 150 151 152 153 154 155 |
# File 'lib/condenser/asset.rb', line 146 def cache_key Digest::SHA1.base64digest(JSON.generate([ Condenser::VERSION, @source_file, stat.ino, stat.mtime.to_f, stat.size, @content_types_digest ])) end |
#charset ⇒ Object
366 367 368 |
# File 'lib/condenser/asset.rb', line 366 def charset @source.encoding.name.downcase end |
#content_type ⇒ Object
45 46 47 |
# File 'lib/condenser/asset.rb', line 45 def content_type @content_types.last end |
#digest ⇒ Object
361 362 363 364 |
# File 'lib/condenser/asset.rb', line 361 def digest process @digest end |
#eql?(other) ⇒ Boolean Also known as: ==
Public: Compare assets.
Assets are equal if they share the same path and digest.
Returns true or false.
402 403 404 |
# File 'lib/condenser/asset.rb', line 402 def eql?(other) self.class == other.class && self.filename == other.filename && self.content_types == other.content_types end |
#export_cache_version ⇒ Object
168 169 170 171 172 173 174 175 176 177 |
# File 'lib/condenser/asset.rb', line 168 def export_cache_version return @ecv if @ecv f = [] all_dependenies(export_dependencies, [], :export_dependencies) do |dep| f << [dep.source_file, dep.stat.ino, dep.stat.mtime.to_f, dep.stat.size] end @ecv = Digest::SHA1.base64digest(JSON.generate(f)) end |
#export_dependencies ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/condenser/asset.rb', line 85 def export_dependencies deps = @environment.cache.fetch "export-deps/#{cache_key}" do process @export_dependencies + @process_dependencies end d = [] deps.each do |i| i = [i, nil] if i.is_a?(String) @environment.resolve(i[0], File.dirname(@source_file), accept: i[1]).each do |asset| d << asset end end d end |
#ext ⇒ Object
393 394 395 |
# File 'lib/condenser/asset.rb', line 393 def ext File.extname(filename) end |
#has_default_export? ⇒ Boolean
101 102 103 104 |
# File 'lib/condenser/asset.rb', line 101 def has_default_export? process @default_export end |
#has_exports? ⇒ Boolean
106 107 108 109 |
# File 'lib/condenser/asset.rb', line 106 def has_exports? process @exports end |
#hexdigest ⇒ Object Also known as: etag
Public: Returns String hexdigest of source.
371 372 373 374 |
# File 'lib/condenser/asset.rb', line 371 def hexdigest process @digest.unpack('H*'.freeze).first end |
#inspect ⇒ Object
62 63 64 65 66 67 |
# File 'lib/condenser/asset.rb', line 62 def inspect dirname, basename, extensions, mime_types = @environment.decompose_path(@filename) " #<\#{self.class.name}#\#{self.object_id} @filename=\#{@filename} @content_types=\#{@content_types.inspect} @source_file=\#{@source_file} @source_mime_types=\#{mime_types.inspect}>\n TEXT\nend\n" |
#integrity ⇒ Object
377 378 379 380 |
# File 'lib/condenser/asset.rb', line 377 def integrity process "#{@digest_name}-#{[@digest].pack('m0')}" end |
#length ⇒ Object Also known as: size
355 356 357 358 |
# File 'lib/condenser/asset.rb', line 355 def length process @source.bytesize end |
#load_processors ⇒ Object
111 112 113 114 115 116 117 118 |
# File 'lib/condenser/asset.rb', line 111 def load_processors return if @processors_loaded @processors_loaded = true process @processors.map! { |p| p.is_a?(String) ? p.constantize : p } @environment.load_processors(*@processors) end |
#needs_reexporting! ⇒ Object
185 186 187 188 189 |
# File 'lib/condenser/asset.rb', line 185 def needs_reexporting! restat! @export = nil @ecv = nil end |
#needs_reprocessing! ⇒ Object
179 180 181 182 183 |
# File 'lib/condenser/asset.rb', line 179 def needs_reprocessing! @processed = false @pcv = nil needs_reexporting! end |
#path ⇒ Object
41 42 43 |
# File 'lib/condenser/asset.rb', line 41 def path filename.sub(/\.(\w+)$/) { |ext| "-#{etag}#{ext}" } end |
#process ⇒ Object
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
# File 'lib/condenser/asset.rb', line 191 def process return if @processed result = @environment.build do @environment.cache.fetch_if(Proc.new {"process/#{cache_key}/#{process_cache_version}"}, "direct-deps/#{cache_key}") do @source = File.binread(@source_file) dirname, basename, extensions, mime_types = @environment.decompose_path(@source_file) data = { source: @source, source_file: @source_file, filename: @filename.dup, content_type: mime_types, map: nil, linked_assets: [], process_dependencies: [], export_dependencies: [], processors: Set.new } while @environment.templates.has_key?(data[:content_type].last) templator = @environment.templates[data[:content_type].pop] templator_klass = (templator.is_a?(Class) ? templator : templator.class) data[:processors] << templator_klass.name @environment.load_processors(templator_klass) templator.call(@environment, data) data[:filename] = data[:filename].gsub(/\.#{extensions.last}$/, '') end case @environment.mime_types[data[:content_type].last][:charset] when :unicode detect_unicode(data[:source]) when :css detect_css(data[:source]) when :html detect_html(data[:source]) else detect(data[:source]) if mime_types.last.start_with?('text/') end if @environment.preprocessors.has_key?(data[:content_type].last) @environment.preprocessors[data[:content_type].last].each do |processor| processor_klass = (processor.is_a?(Class) ? processor : processor.class) data[:processors] << processor_klass.name @environment.load_processors(processor_klass) processor.call(@environment, data) end end if data[:content_type].last != @content_types.last && @environment.transformers.has_key?(data[:content_type].last) from_mime_type = data[:content_type].pop @environment.transformers[from_mime_type].each do |to_mime_type, processor| processor_klass = (processor.is_a?(Class) ? processor : processor.class) data[:processors] << processor_klass.name @environment.load_processors(processor_klass) @environment.logger.info { "Transforming #{self.filename} from #{from_mime_type} to #{to_mime_type} with #{processor.name}" } processor.call(@environment, data) data[:content_type] << to_mime_type end end if mime_types != @content_types raise ContentTypeMismatch, "mime type(s) \"#{@content_types.join(', ')}\" does not match requested mime type(s) \"#{data[:mime_types].join(', ')}\"" end data[:digest] = @environment.digestor.digest(data[:source]) data[:digest_name] = @environment.digestor.name.sub(/^.*::/, '').downcase # Do this here and at the end so cache_key can be calculated if we # run this block @source = data[:source] @sourcemap = data[:map] @filename = data[:filename] @content_types = data[:content_type] @digest = data[:digest] @digest_name = data[:digest_name] @linked_assets = data[:linked_assets] @process_dependencies = data[:process_dependencies] @export_dependencies = data[:export_dependencies] @default_export = data[:default_export] @exports = data[:exports] @processors = data[:processors] @processors_loaded = true @processed = true data end end @source = result[:source] @sourcemap = result[:map] @filename = result[:filename] @content_types = result[:content_type] @digest = result[:digest] @digest_name = result[:digest_name] @linked_assets = result[:linked_assets] @process_dependencies = result[:process_dependencies] @export_dependencies = result[:export_dependencies] @default_export = result[:default_export] @exports = result[:exports] @processors = result[:processors] load_processors @processed = true end |
#process_cache_version ⇒ Object
157 158 159 160 161 162 163 164 165 166 |
# File 'lib/condenser/asset.rb', line 157 def process_cache_version return @pcv if @pcv f = [] all_dependenies(process_dependencies, [], :process_dependencies) do |dep| f << [dep.source_file, dep.stat.ino, dep.stat.mtime.to_f, dep.stat.size] end @pcv = Digest::SHA1.base64digest(JSON.generate(f)) end |
#process_dependencies ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/condenser/asset.rb', line 69 def process_dependencies deps = @environment.cache.fetch "direct-deps/#{cache_key}" do process @process_dependencies end d = [] deps.each do |i| i = [i, nil] if i.is_a?(String) @environment.resolve(i[0], File.dirname(@source_file), accept: i[1]).each do |asset| d << asset end end d end |
#restat! ⇒ Object
58 59 60 |
# File 'lib/condenser/asset.rb', line 58 def restat! @stat = nil end |
#stat ⇒ Object
54 55 56 |
# File 'lib/condenser/asset.rb', line 54 def stat @stat ||= File.stat(@source_file) end |
#to_json ⇒ Object
382 383 384 |
# File 'lib/condenser/asset.rb', line 382 def to_json { path: path, digest: hexdigest, size: size, integrity: integrity } end |
#to_s ⇒ Object
340 341 342 343 |
# File 'lib/condenser/asset.rb', line 340 def to_s process @source end |
#write(output_directory) ⇒ Object
386 387 388 389 390 391 |
# File 'lib/condenser/asset.rb', line 386 def write(output_directory) files = @environment.writers_for_mime_type(content_type).map do |writer| writer.call(output_directory, self) end files.flatten.compact end |