class Sass::Plugin::StalenessChecker
as its instance-level caches are never explicitly expired.
WARNING: It is important not to retain the instance for too long,
the caches should make the whole process significantly faster.
and the instance-level {#stylesheet_needs_update?} method should be used.
a StalenessChecker instance should be created,
* For a series of staleness checks (e.g. checking all files for staleness)
should be used.
the class-level {stylesheet_needs_update?} method
* For a one-off staleness check of a single ‘.sss` file,
Usage:
These are only used by a single StalenessChecker instance.
and one for the parse tree for a file.
one for whether a file is stale during this particular run.
* Three short-lived instance-level caches, one for file mtimes,
This is a long-lived cache that is reused by every StalenessChecker instance.
* A class-level dependency cache which stores @import paths for each file.
To speed things up two level of caches are employed:
The class handles `.sss` file staleness checks via their mtime timestamps.
def self.stylesheet_modified_since?(template_file, mtime, importer = nil)
-
(Boolean)
- Whether the stylesheet has been modified.
Parameters:
-
importer
(Sass::Importers::Base
) -- The importer used to locate the stylesheet. -
mtime
(Fixnum
) -- The modification time to check against. -
template_file
(String
) -- The location of the Sass or SCSS template.
def self.stylesheet_modified_since?(template_file, mtime, importer = nil) new(Plugin.engine_options).stylesheet_modified_since?(template_file, mtime, importer) end
def self.stylesheet_needs_update?(css_file, template_file)
-
(Boolean)
- Whether the stylesheet needs to be updated.
Parameters:
-
template_file
(String
) -- The location of the Sass or SCSS template -
css_file
(String
) -- The location of the CSS file to check.
def self.stylesheet_needs_update?(css_file, template_file) new(Plugin.engine_options).stylesheet_needs_update?(css_file, template_file) end
def compute_dependencies(uri, importer)
def compute_dependencies(uri, importer) tree(uri, importer).grep(Tree::ImportNode) do |n| next if n.css_import? file = n.imported_file key = [file.options[:filename], file.options[:importer]] @parse_trees[key] = file.to_tree key end.compact end
def dependencies(uri, importer)
def dependencies(uri, importer) stored_mtime, dependencies = @dependencies[[uri, importer]] if !stored_mtime || stored_mtime < mtime(uri, importer) dependencies = compute_dependencies(uri, importer) @dependencies[[uri, importer]] = [mtime(uri, importer), dependencies] end dependencies end
def dependencies_stale?(uri, importer, css_mtime)
def dependencies_stale?(uri, importer, css_mtime) timestamps = @dependencies_stale[[uri, importer]] ||= {} timestamps.each_pair do |checked_css_mtime, is_stale| if checked_css_mtime <= css_mtime && !is_stale return false elsif checked_css_mtime > css_mtime && is_stale return true end end timestamps[css_mtime] = dependencies(uri, importer).any?(&dependency_updated?(css_mtime)) rescue Sass::SyntaxError # If there's an error finding dependencies, default to recompiling. true end
def dependency_updated?(css_mtime)
def dependency_updated?(css_mtime) Proc.new do |uri, importer| mtime(uri, importer) > css_mtime || dependencies_stale?(uri, importer, css_mtime) end end
def initialize(options)
-
options
({Symbol => Object}
) --
def initialize(options) @dependencies = self.class.dependencies_cache # Entries in the following instance-level caches are never explicitly expired. # Instead they are supposed to automaticaly go out of scope when a series of staleness checks # (this instance of StalenessChecker was created for) is finished. @mtimes, @dependencies_stale, @parse_trees = {}, {}, {} @options = Sass::Engine.normalize_options(options) end
def mtime(uri, importer)
def mtime(uri, importer) @mtimes[[uri, importer]] ||= begin mtime = importer.mtime(uri, @options) if mtime.nil? @dependencies.delete([uri, importer]) DELETED else mtime.to_i end end end
def stylesheet_modified_since?(template_file, mtime, importer = nil)
-
(Boolean)
- Whether the stylesheet has been modified.
Parameters:
-
importer
(Sass::Importers::Base
) -- The importer used to locate the stylesheet. -
mtime
(Fixnum
) -- The modification time to check against. -
template_file
(String
) -- The location of the Sass or SCSS template.
def stylesheet_modified_since?(template_file, mtime, importer = nil) importer ||= @options[:filesystem_importer].new(".") dependency_updated?(mtime).call(template_file, importer) end
def stylesheet_needs_update?(css_file, template_file)
-
(Boolean)
- Whether the stylesheet needs to be updated.
Parameters:
-
template_file
(String
) -- The location of the Sass or SCSS template -
css_file
(String
) -- The location of the CSS file to check.
def stylesheet_needs_update?(css_file, template_file) template_file = File.expand_path(template_file) begin css_mtime = File.mtime(css_file).to_i rescue Errno::ENOENT return true end stylesheet_modified_since?(template_file, css_mtime) end
def tree(uri, importer)
def tree(uri, importer) @parse_trees[[uri, importer]] ||= importer.find(uri, @options).to_tree end