Question

I want to run sass from within Ruby script. Compiling a scss file scss can be done by doing:

require "sass"
Sass::Engine.new(File.read(scss), options).render

with some appropriate hash assigned to options, but I want to compile it only if scss or any of its partial (imported) files if updated.

Within options, I have sass caching turned on. Caching keeps all the update information of the relevant files in a certain directory. I feel that, by doing up to:

engine = Sass::Engine.new(File.read(scss), options)

there must be information available in engine by which I can tell if scss or any of its partial files is updated. Only in such case, I should run:

engine.render

to do the compiling. How can I detect the file update based on the sass cache?

No correct solution

OTHER TIPS

You can use:

Sass::Engine.for_file(scss, options).render

This will check whether there is a cached parse tree in the cache directory for the scss file hash. See the documentation for Sass::Engine.for_file here: http://sass-lang.com/docs/yardoc/Sass/Engine.html.

The cache lookup is performed in the Sass::Engine#_to_tree method, source code available here: https://github.com/nex3/sass/blob/stable/lib/sass/engine.rb.

EDIT

First few lines of Sass::Engine#_to_tree:

def _to_tree
  if (@options[:cache] || @options[:read_cache]) &&
      @options[:filename] && @options[:importer]
    key = sassc_key
    sha = Digest::SHA1.hexdigest(@template)

    if root = @options[:cache_store].retrieve(key, sha)
      root.options = @options
      return root
    end
  end
  # ...
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top