| Class | Sass::Plugin::StalenessChecker |
| In: |
lib/sass/plugin/staleness_checker.rb
|
| Parent: | Object |
The class handles `.s[ca]ss` file staleness checks via their mtime timestamps.
To speed things up two level of caches are employed:
Usage:
| DELETED | = | 1.0/0.0 |
| dependencies_cache | [RW] | @private |
Creates a new StalenessChecker for checking the staleness of several stylesheets at once.
# File lib/sass/plugin/staleness_checker.rb, line 35
35: def initialize
36: @dependencies = self.class.dependencies_cache
37:
38: # Entries in the following instance-level caches are never explicitly expired.
39: # Instead they are supposed to automaticaly go out of scope when a series of staleness checks
40: # (this instance of StalenessChecker was created for) is finished.
41: @mtimes, @dependencies_stale = {}, {}
42: end
Returns whether or not a given CSS file is out of date and needs to be regenerated.
The distinction between this method and the instance-level \{stylesheet_needs_update?} is that the instance method preserves mtime and stale-dependency caches, so it‘s better to use when checking multiple stylesheets at once.
@param css_file [String] The location of the CSS file to check. @param template_file [String] The location of the Sass or SCSS template
that is compiled to `css_file`.
# File lib/sass/plugin/staleness_checker.rb, line 66
66: def self.stylesheet_needs_update?(css_file, template_file)
67: new.stylesheet_needs_update?(css_file, template_file)
68: end
Returns whether or not a given CSS file is out of date and needs to be regenerated.
@param css_file [String] The location of the CSS file to check. @param template_file [String] The location of the Sass or SCSS template
that is compiled to `css_file`.
# File lib/sass/plugin/staleness_checker.rb, line 50
50: def stylesheet_needs_update?(css_file, template_file)
51: template_file, css_mtime = File.expand_path(template_file), mtime(css_file)
52:
53: css_mtime == DELETED || dependency_updated?(css_mtime).call(template_file)
54: end
# File lib/sass/plugin/staleness_checker.rb, line 114
114: def compute_dependencies(filename)
115: Files.tree_for(filename, Plugin.engine_options).grep(Tree::ImportNode) do |n|
116: File.expand_path(n.full_filename) unless n.full_filename =~ /\.css$/
117: end.compact
118: rescue Sass::SyntaxError => e
119: [] # If the file has an error, we assume it has no dependencies
120: end
# File lib/sass/plugin/staleness_checker.rb, line 93
93: def dependencies(filename)
94: stored_mtime, dependencies = @dependencies[filename]
95:
96: if !stored_mtime || stored_mtime < mtime(filename)
97: @dependencies[filename] = [mtime(filename), dependencies = compute_dependencies(filename)]
98: end
99:
100: dependencies
101: end
# File lib/sass/plugin/staleness_checker.rb, line 72
72: def dependencies_stale?(template_file, css_mtime)
73: timestamps = @dependencies_stale[template_file] ||= {}
74: timestamps.each_pair do |checked_css_mtime, is_stale|
75: if checked_css_mtime <= css_mtime && !is_stale
76: return false
77: elsif checked_css_mtime > css_mtime && is_stale
78: return true
79: end
80: end
81: timestamps[css_mtime] = dependencies(template_file).any?(&dependency_updated?(css_mtime))
82: end
# File lib/sass/plugin/staleness_checker.rb, line 103
103: def dependency_updated?(css_mtime)
104: lambda do |dep|
105: begin
106: mtime(dep) > css_mtime || dependencies_stale?(dep, css_mtime)
107: rescue Sass::SyntaxError
108: # If there's an error finding depenencies, default to recompiling.
109: true
110: end
111: end
112: end