Class: Sass::Plugin::StalenessChecker

Inherits:
Object
  • Object
show all
Defined in:
/var/www/sass-pages/.sass/lib/sass/plugin/staleness_checker.rb

Overview

The class handles .s[ca]ss file staleness checks via their mtime timestamps.

To speed things up two level of caches are employed:

  • A class-level dependency cache which stores @import paths for each file. This is a long-lived cache that is reused by every StalenessChecker instance.
  • Three short-lived instance-level caches, one for file mtimes, one for whether a file is stale during this particular run. and one for the parse tree for a file. These are only used by a single StalenessChecker instance.

Usage:

  • For a one-off staleness check of a single .s[ca]ss file, the class-level stylesheet_needs_update? method should be used.
  • For a series of staleness checks (e.g. checking all files for staleness) a StalenessChecker instance should be created, and the instance-level #stylesheet_needs_update? method should be used. the caches should make the whole process significantly faster. WARNING: It is important not to retain the instance for too long, as its instance-level caches are never explicitly expired.

Class Method Summary

Instance Method Summary

Constructor Details

- (StalenessChecker) initialize(options)

Creates a new StalenessChecker for checking the staleness of several stylesheets at once.

Parameters:



39
40
41
42
43
44
45
46
47
48
49
50
51
# File '/var/www/sass-pages/.sass/lib/sass/plugin/staleness_checker.rb', line 39

def initialize(options)
  @dependencies = self.class.dependencies_cache

  # URIs that are being actively checked for staleness. Protects against
  # import loops.
  @actively_checking = Set.new

  # 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

Class Method Details

+ (Boolean) stylesheet_modified_since?(template_file, mtime, importer = nil)

Returns whether a Sass or SCSS stylesheet has been modified since a given time.

The distinction between this method and the instance-level #stylesheet_modified_since? is that the instance method preserves mtime and stale-dependency caches, so it’s better to use when checking multiple stylesheets at once.

Parameters:

  • (String) template_file — The location of the Sass or SCSS template.
  • (Fixnum) mtime — The modification time to check against.
  • (Sass::Importers::Base) importer (defaults to: nil) — The importer used to locate the stylesheet. Defaults to the filesystem importer.

Returns:

  • (Boolean) — Whether the stylesheet has been modified.


108
109
110
# File '/var/www/sass-pages/.sass/lib/sass/plugin/staleness_checker.rb', line 108

def self.stylesheet_modified_since?(template_file, mtime, importer = nil)
  new(Plugin.engine_options).stylesheet_modified_since?(template_file, mtime, importer)
end

+ (Boolean) stylesheet_needs_update?(css_file, template_file, importer = nil)

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.

Parameters:

  • (String) css_file — The location of the CSS file to check.
  • (String) template_file — The location of the Sass or SCSS template that is compiled to css_file.

Returns:

  • (Boolean) — Whether the stylesheet needs to be updated.


93
94
95
# File '/var/www/sass-pages/.sass/lib/sass/plugin/staleness_checker.rb', line 93

def self.stylesheet_needs_update?(css_file, template_file, importer = nil)
  new(Plugin.engine_options).stylesheet_needs_update?(css_file, template_file, importer)
end

Instance Method Details

- (Boolean) stylesheet_modified_since?(template_file, mtime, importer = nil)

Returns whether a Sass or SCSS stylesheet has been modified since a given time.

Parameters:

  • (String) template_file — The location of the Sass or SCSS template.
  • (Fixnum) mtime — The modification time to check against.
  • (Sass::Importers::Base) importer (defaults to: nil) — The importer used to locate the stylesheet. Defaults to the filesystem importer.

Returns:

  • (Boolean) — Whether the stylesheet has been modified.


77
78
79
80
# File '/var/www/sass-pages/.sass/lib/sass/plugin/staleness_checker.rb', line 77

def stylesheet_modified_since?(template_file, mtime, importer = nil)
  importer ||= @options[:filesystem_importer].new(".")
  dependency_updated?(mtime).call(template_file, importer)
end

- (Boolean) stylesheet_needs_update?(css_file, template_file, importer = nil)

Returns whether or not a given CSS file is out of date and needs to be regenerated.

Parameters:

  • (String) css_file — The location of the CSS file to check.
  • (String) template_file — The location of the Sass or SCSS template that is compiled to css_file.

Returns:

  • (Boolean) — Whether the stylesheet needs to be updated.


60
61
62
63
64
65
66
67
68
# File '/var/www/sass-pages/.sass/lib/sass/plugin/staleness_checker.rb', line 60

def stylesheet_needs_update?(css_file, template_file, importer = nil)
  template_file = File.expand_path(template_file)
  begin
    css_mtime = File.mtime(css_file)
  rescue Errno::ENOENT
    return true
  end
  stylesheet_modified_since?(template_file, css_mtime, importer)
end