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.
-
#exports ⇒ Object
readonly
Returns the value of attribute exports.
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#imports ⇒ Object
Returns the value of attribute imports.
-
#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
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #all_dependenies(deps, visited, meth, &block) ⇒ Object
- #all_export_dependencies(visited = Set.new) ⇒ Object
- #all_process_dependencies(visited = Set.new) ⇒ Object
- #basepath ⇒ Object
- #cache_key ⇒ Object
- #charset ⇒ Object
- #content_type ⇒ Object
- #digest ⇒ Object
-
#eql?(other) ⇒ Boolean
(also: #==)
Public: Compare assets.
- #etag ⇒ Object
- #export ⇒ Object
- #export_cache_version ⇒ Object
- #export_dependencies ⇒ Object
- #ext ⇒ Object
- #has_default_export? ⇒ Boolean
- #has_exports? ⇒ Boolean
-
#hexdigest ⇒ Object
Public: Returns String hexdigest of source.
-
#initialize(env, attributes = {}) ⇒ Asset
constructor
A new instance of Asset.
- #inspect ⇒ Object
- #integrity ⇒ Object
- #length ⇒ Object (also: #size)
- #linked_assets ⇒ Object
- #load_processors ⇒ Object
- #needs_reexporting! ⇒ Object
- #needs_reprocessing! ⇒ Object
-
#normalize_filename_base(source_filename) ⇒ Object
Remove Enviroment base if it exists.
- #normialize_dependency_names(deps) ⇒ Object
- #path ⇒ Object
- #process ⇒ Object
- #process_cache_version ⇒ Object
- #process_dependencies ⇒ Object
- #restat! ⇒ 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.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/condenser/asset.rb', line 20 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.
14 15 16 |
# File 'lib/condenser/asset.rb', line 14 def content_types @content_types end |
#content_types_digest ⇒ Object (readonly)
Returns the value of attribute content_types_digest.
15 16 17 |
# File 'lib/condenser/asset.rb', line 15 def content_types_digest @content_types_digest end |
#environment ⇒ Object (readonly)
Returns the value of attribute environment.
14 15 16 |
# File 'lib/condenser/asset.rb', line 14 def environment @environment end |
#exports ⇒ Object (readonly)
Returns the value of attribute exports.
15 16 17 |
# File 'lib/condenser/asset.rb', line 15 def exports @exports end |
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
14 15 16 |
# File 'lib/condenser/asset.rb', line 14 def filename @filename end |
#imports ⇒ Object
Returns the value of attribute imports.
18 19 20 |
# File 'lib/condenser/asset.rb', line 18 def imports @imports end |
#processed ⇒ Object
Returns the value of attribute processed.
18 19 20 |
# File 'lib/condenser/asset.rb', line 18 def processed @processed end |
#source ⇒ Object
438 439 440 441 |
# File 'lib/condenser/asset.rb', line 438 def source process @source end |
#source_file ⇒ Object (readonly)
Returns the value of attribute source_file.
14 15 16 |
# File 'lib/condenser/asset.rb', line 14 def source_file @source_file end |
#source_path ⇒ Object (readonly)
Returns the value of attribute source_path.
14 15 16 |
# File 'lib/condenser/asset.rb', line 14 def source_path @source_path end |
#sourcemap ⇒ Object
443 444 445 446 |
# File 'lib/condenser/asset.rb', line 443 def sourcemap process @sourcemap end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
15 16 17 |
# File 'lib/condenser/asset.rb', line 15 def type @type end |
Instance Method Details
#all_dependenies(deps, visited, meth, &block) ⇒ Object
135 136 137 138 139 140 141 142 143 |
# File 'lib/condenser/asset.rb', line 135 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(visited = Set.new) ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/condenser/asset.rb', line 158 def all_export_dependencies(visited = Set.new) f = Set.new if !visited.include?(@source_file) f << @source_file visited << self.source_file end all_dependenies(export_dependencies, visited, :export_dependencies) do |dep| f << dep.source_file end f end |
#all_process_dependencies(visited = Set.new) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/condenser/asset.rb', line 145 def all_process_dependencies(visited = Set.new) f = Set.new if !visited.include?(@source_file) f << @source_file visited << self.source_file end all_dependenies(process_dependencies, visited, :process_dependencies) do |dep| f << dep.source_file end f end |
#basepath ⇒ Object
51 52 53 54 |
# File 'lib/condenser/asset.rb', line 51 def basepath dirname, basename, extensions, mime_types = @environment.decompose_path(filename) [dirname, basename].compact.join('/') end |
#cache_key ⇒ Object
171 172 173 174 175 176 177 178 179 |
# File 'lib/condenser/asset.rb', line 171 def cache_key @cache_key ||= Digest::SHA1.base64digest(JSON.generate([ Condenser::VERSION, @environment.pipline_digest, normalize_filename_base(@source_file), Digest::SHA256.file(@source_file).hexdigest, @content_types_digest ])) end |
#charset ⇒ Object
459 460 461 |
# File 'lib/condenser/asset.rb', line 459 def charset @source.encoding.name.downcase end |
#content_type ⇒ Object
47 48 49 |
# File 'lib/condenser/asset.rb', line 47 def content_type @content_types.last end |
#digest ⇒ Object
454 455 456 457 |
# File 'lib/condenser/asset.rb', line 454 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.
506 507 508 |
# File 'lib/condenser/asset.rb', line 506 def eql?(other) self.class == other.class && self.filename == other.filename && self.content_types == other.content_types end |
#etag ⇒ Object
469 470 471 472 |
# File 'lib/condenser/asset.rb', line 469 def etag process @etag end |
#export ⇒ Object
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 |
# File 'lib/condenser/asset.rb', line 381 def export return @export if @export @export = @environment.build do data = @environment.cache.fetch_if(Proc.new {"export/#{cache_key}/#{export_cache_version}"}, "export-deps/#{cache_key}") do process dirname, basename, extensions, mime_types = @environment.decompose_path(@filename) data = { etag: @etag, type: @type, source: @source.dup, source_file: @source_file, filename: @filename.dup, content_types: @content_types, sourcemap: nil, linked_assets: [], process_dependencies: [], export_dependencies: [] } if @environment.exporters.has_key?(content_type) @environment.exporters[content_type].each do |exporter| @environment.logger.info { "Exporting #{self.filename} with #{exporter.name}" } exporter.call(@environment, data) end end if minifier = @environment.minifier_for(content_type) @environment.logger.info { "Minifing #{self.filename} with #{minifier.name}" } minifier.call(@environment, data) end data[:digest] = @environment.digestor.digest(data[:source]) data[:digest_name] = @environment.digestor.name.sub(/^.*::/, '').downcase data end if @environment.build_cache.listening # TODO we could skip file and all their depencies here if they are # already in build_cache.@export_dependencies all_export_dependencies.each do |sf| @environment.build_cache.instance_variable_get(:@export_dependencies)[sf]&.add(self) end end Export.new(@environment, data) end end |
#export_cache_version ⇒ Object
205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/condenser/asset.rb', line 205 def export_cache_version return @ecv if @ecv f = [] all_dependenies(export_dependencies, Set.new, :export_dependencies) do |dep| f << [ normalize_filename_base(dep.source_file), Digest::SHA256.file(dep.source_file).hexdigest ] end @ecv = Digest::SHA1.hexdigest(JSON.generate(f)) end |
#export_dependencies ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/condenser/asset.rb', line 84 def export_dependencies deps = @environment.cache.fetch "export-deps/#{cache_key}" do process # Sort so etag and cache key are same irrelevant of ordering of # dependencies (@export_dependencies + @process_dependencies).map { |fn| [normalize_filename_base(fn[0]), fn[1]] }.sort_by { |d| d[0] } end deps.inject([]) do |memo, i| i[0] = File.join(@environment.base, i[0].delete_prefix('!')) if i[0].start_with?('!') && @environment.base @environment.resolve(i[0], File.dirname(@source_file), accept: i[1], npm: true).each do |asset| memo << asset end memo end end |
#ext ⇒ Object
497 498 499 |
# File 'lib/condenser/asset.rb', line 497 def ext File.extname(filename) end |
#has_default_export? ⇒ Boolean
116 117 118 119 |
# File 'lib/condenser/asset.rb', line 116 def has_default_export? process @default_export end |
#has_exports? ⇒ Boolean
121 122 123 124 |
# File 'lib/condenser/asset.rb', line 121 def has_exports? process @exports end |
#hexdigest ⇒ Object
Public: Returns String hexdigest of source.
464 465 466 467 |
# File 'lib/condenser/asset.rb', line 464 def hexdigest process @digest.unpack('H*'.freeze).first end |
#inspect ⇒ Object
60 61 62 63 64 65 |
# File 'lib/condenser/asset.rb', line 60 def inspect dirname, basename, extensions, mime_types = @environment.decompose_path(@filename) <<-TEXT #<#{self.class.name}##{self.object_id} @filename=#{@filename} @content_types=#{@content_types.inspect} @source_file=#{@source_file} @source_mime_types=#{mime_types.inspect}> TEXT end |
#integrity ⇒ Object
474 475 476 477 |
# File 'lib/condenser/asset.rb', line 474 def integrity process "#{@digest_name}-#{[@digest].pack('m0')}" end |
#length ⇒ Object Also known as: size
448 449 450 451 |
# File 'lib/condenser/asset.rb', line 448 def length process @source.bytesize end |
#linked_assets ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/condenser/asset.rb', line 101 def linked_assets deps = @environment.cache.fetch "linked-assets/#{cache_key}" do process @linked_assets.map { |fn| [normalize_filename_base(fn[0]), fn[1]] } end deps.inject([]) do |memo, i| i[0] = File.join(@environment.base, i[0].delete_prefix('!')) if i[0].start_with?('!') && @environment.base @environment.resolve(i[0], File.dirname(@source_file), accept: i[1]).each do |asset| memo << asset end memo end end |
#load_processors ⇒ Object
126 127 128 129 130 131 132 133 |
# File 'lib/condenser/asset.rb', line 126 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
226 227 228 229 230 |
# File 'lib/condenser/asset.rb', line 226 def needs_reexporting! restat! @export = nil @ecv = nil end |
#needs_reprocessing! ⇒ Object
219 220 221 222 223 224 |
# File 'lib/condenser/asset.rb', line 219 def needs_reprocessing! @processed = false @pcv = nil @cache_key = nil needs_reexporting! end |
#normalize_filename_base(source_filename) ⇒ Object
Remove Enviroment base if it exists. This allows two of the same repos in a different location to use the same cache (like capistrano deploys)
183 184 185 186 187 188 189 |
# File 'lib/condenser/asset.rb', line 183 def normalize_filename_base(source_filename) if @environment.base && source_filename.start_with?(@environment.base) '!'+source_filename.delete_prefix(@environment.base).delete_prefix(File::SEPARATOR) else source_filename end end |
#normialize_dependency_names(deps) ⇒ Object
370 371 372 373 374 375 376 377 378 379 |
# File 'lib/condenser/asset.rb', line 370 def normialize_dependency_names(deps) deps.map do |fn| if fn.is_a?(String) dirname, basename, extensions, mime_types = @environment.decompose_path(fn, source_file) [dirname ? File.join(dirname, basename) : basename, mime_types.empty? ? @content_types : mime_types] else fn end end end |
#path ⇒ Object
43 44 45 |
# File 'lib/condenser/asset.rb', line 43 def path filename.sub(/\.(\w+)$/) { |ext| "-#{etag}#{ext}" } end |
#process ⇒ Object
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 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 |
# File 'lib/condenser/asset.rb', line 232 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: Set.new, process_dependencies: Set.new, export_dependencies: Set.new, 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) @environment.logger.info { "Pre Processing #{self.filename} with #{processor.name}" } 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 @environment.postprocessors.has_key?(data[:content_type].last) @environment.postprocessors[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) @environment.logger.info { "Post Processing #{self.filename} with #{processor.name}" } processor.call(@environment, data) 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 data[:process_dependencies] = normialize_dependency_names(data[:process_dependencies]) data[:export_dependencies] = normialize_dependency_names(data[:export_dependencies]) data[:linked_assets] = normialize_dependency_names(data[:linked_assets]) # 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 = Set.new(data[:linked_assets]) @process_dependencies = Set.new(data[:process_dependencies]) @export_dependencies = Set.new(data[:export_dependencies]) @default_export = data[:default_export] @exports = data[:exports] @processors = data[:processors] @processors_loaded = true @processed = true @type = data[:type] digestor = @environment.digestor.new digestor << data[:source] all_dependenies(export_dependencies, Set.new, :export_dependencies) do |dep| digestor << dep.source end data[:etag] = digestor.digest.unpack('H*'.freeze).first @etag = data[:etag] 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 = Set.new(result[:linked_assets]) @process_dependencies = Set.new(result[:process_dependencies]) @export_dependencies = Set.new(result[:export_dependencies]) @default_export = result[:default_export] @exports = result[:exports] @processors = result[:processors] @etag = result[:etag] @type = result[:type] load_processors @processed = true end |
#process_cache_version ⇒ Object
191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/condenser/asset.rb', line 191 def process_cache_version return @pcv if @pcv f = [] all_dependenies(process_dependencies, Set.new, :process_dependencies) do |dep| f << [ normalize_filename_base(dep.source_file), Digest::SHA256.file(dep.source_file).hexdigest ] end @pcv = Digest::SHA1.base64digest(JSON.generate(f)) end |
#process_dependencies ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/condenser/asset.rb', line 67 def process_dependencies deps = @environment.cache.fetch "direct-deps/#{cache_key}" do process # Sort so etag and cache key are same irrelevant of ordering of # dependencies @process_dependencies.map { |fn| [normalize_filename_base(fn[0]), fn[1]] }.sort_by { |d| d[0] } end deps.inject([]) do |memo, i| i[0] = File.join(@environment.base, i[0].delete_prefix('!')) if i[0].start_with?('!') && @environment.base @environment.resolve(i[0], File.dirname(@source_file), accept: i[1]).each do |asset| memo << asset end memo end end |
#restat! ⇒ Object
56 57 58 |
# File 'lib/condenser/asset.rb', line 56 def restat! @stat = nil end |
#to_json ⇒ Object
479 480 481 482 483 484 485 486 487 488 |
# File 'lib/condenser/asset.rb', line 479 def to_json { path: path, etag: etag, type: type, size: size, digest: hexdigest, integrity: integrity } end |
#to_s ⇒ Object
433 434 435 436 |
# File 'lib/condenser/asset.rb', line 433 def to_s process @source end |
#write(output_directory) ⇒ Object
490 491 492 493 494 495 |
# File 'lib/condenser/asset.rb', line 490 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 |