class Sprockets::ProcessedAsset
def build_dependency_paths(environment, context)
def build_dependency_paths(environment, context) dependency_paths = {} context._dependency_paths.each do |path| dep = DependencyFile.new(path, environment.stat(path).mtime, environment.file_digest(path).hexdigest) dependency_paths[dep] = true end context._dependency_assets.each do |path| if path == self.pathname.to_s dep = DependencyFile.new(pathname, environment.stat(path).mtime, environment.file_digest(path).hexdigest) dependency_paths[dep] = true elsif asset = environment.find_asset(path, :bundle => false) asset.dependency_paths.each do |d| dependency_paths[d] = true end end end @dependency_paths = dependency_paths.keys end
def build_required_assets(environment, context)
def build_required_assets(environment, context) @required_assets = resolve_dependencies(environment, context._required_paths + [pathname.to_s]) - resolve_dependencies(environment, context._stubbed_assets.to_a) end
def compute_dependency_digest(environment)
def compute_dependency_digest(environment) required_assets.inject(environment.digest) { |digest, asset| digest.update asset.digest }.hexdigest end
def encode_with(coder)
def encode_with(coder) super coder['source'] = source coder['dependency_digest'] = dependency_digest coder['required_paths'] = required_assets.map { |a| relativize_root_path(a.pathname).to_s } coder['dependency_paths'] = dependency_paths.map { |d| { 'path' => relativize_root_path(d.pathname).to_s, 'mtime' => d.mtime.iso8601, 'digest' => d.digest } } end
def fresh?(environment)
Checks if Asset is stale by comparing the actual mtime and
def fresh?(environment) # Check freshness of all declared dependencies @dependency_paths.all? { |dep| dependency_fresh?(environment, dep) } end
def init_with(environment, coder)
def init_with(environment, coder) super @source = coder['source'] @dependency_digest = coder['dependency_digest'] @required_assets = coder['required_paths'].map { |p| p = expand_root_path(p) unless environment.paths.detect { |path| p[path] } raise UnserializeError, "#{p} isn't in paths" end p == pathname.to_s ? self : environment.find_asset(p, :bundle => false) } @dependency_paths = coder['dependency_paths'].map { |h| DependencyFile.new(expand_root_path(h['path']), h['mtime'], h['digest']) } end
def initialize(environment, logical_path, pathname)
def initialize(environment, logical_path, pathname) super start_time = Time.now.to_f context = environment.context_class.new(environment, logical_path, pathname) @source = context.evaluate(pathname) @length = Rack::Utils.bytesize(source) @digest = environment.digest.update(source).hexdigest build_required_assets(environment, context) build_dependency_paths(environment, context) @dependency_digest = compute_dependency_digest(environment) elapsed_time = ((Time.now.to_f - start_time) * 1000).to_i environment.logger.debug "Compiled #{logical_path} (#{elapsed_time}ms) (pid #{Process.pid})" end
def resolve_dependencies(environment, paths)
def resolve_dependencies(environment, paths) assets = [] cache = {} paths.each do |path| if path == self.pathname.to_s unless cache[self] cache[self] = true assets << self end elsif asset = environment.find_asset(path, :bundle => false) asset.required_assets.each do |asset_dependency| unless cache[asset_dependency] cache[asset_dependency] = true assets << asset_dependency end end end end assets end