From 07f495620be4538bb5f92790d788a248be8f1007 Mon Sep 17 00:00:00 2001 From: Nico Hagenburger Date: Sat, 1 Feb 2014 11:27:48 +0100 Subject: [PATCH] set dependencies for sprockets to invalidate caches; fixes #25 --- lib/livingstyleguide/engine.rb | 3 ++- lib/livingstyleguide/importer.rb | 15 +++++++---- lib/livingstyleguide/tilt_template.rb | 36 ++++++++++++++++++++++----- test/integration/sprockets_test.rb | 19 ++++++++++++++ 4 files changed, 61 insertions(+), 12 deletions(-) create mode 100644 test/integration/sprockets_test.rb diff --git a/lib/livingstyleguide/engine.rb b/lib/livingstyleguide/engine.rb index ae7ad32..a8346a6 100644 --- a/lib/livingstyleguide/engine.rb +++ b/lib/livingstyleguide/engine.rb @@ -1,13 +1,14 @@ module LivingStyleGuide class Engine - attr_accessor :markdown, :options + attr_accessor :markdown, :files, :options def initialize(source, options, sass_options) @source = source @options = options @sass_options = sass_options @markdown = '' + @files = [] end def render diff --git a/lib/livingstyleguide/importer.rb b/lib/livingstyleguide/importer.rb index c8556aa..ed5c5d8 100644 --- a/lib/livingstyleguide/importer.rb +++ b/lib/livingstyleguide/importer.rb @@ -6,26 +6,31 @@ def initialize(root) def find_relative(name, base, options, absolute = false) @options = options - find_markdown(File.join(File.dirname(base), name)) - super(name, base, options) + engine = super(name, base, options) + find_markdown(options[:filename]) + engine end def find(name, options) @options = options - find_markdown(name) super(name, options) end private def find_markdown(sass_filename) + files << sass_filename glob = "#{sass_filename.sub(/\.s[ac]ss$/, '')}.md" - glob.sub!(/(.*)\//, '\\1/{_,}') unless glob =~ /\/_/ - glob = '{_,}' + glob unless glob =~ /\// Dir.glob(glob) do |markdown_filename| + files << markdown_filename markdown << File.read(markdown_filename) end end + private + def files + @options[:living_style_guide].files + end + private def markdown @options[:living_style_guide].markdown diff --git a/lib/livingstyleguide/tilt_template.rb b/lib/livingstyleguide/tilt_template.rb index 3287061..b27d79b 100644 --- a/lib/livingstyleguide/tilt_template.rb +++ b/lib/livingstyleguide/tilt_template.rb @@ -11,6 +11,7 @@ def prepare end def evaluate(scope, locals, &block) + @scope = scope parse_options(data) generate_sass render_living_style_guide @@ -25,10 +26,11 @@ def sass_options options[:template_location].each do |path, short| options[:load_paths] << ::LivingStyleGuide::Importer.new(path) end - options[:filename] = eval_file - options[:line] = line - options[:syntax] = @options[:syntax] - options[:importer] = LivingStyleGuide::Importer.new('.') + options[:filename] = eval_file + options[:line] = line + options[:syntax] = @options[:syntax] + options[:importer] = LivingStyleGuide::Importer.new('.') + options[:sprockets] = { context: @scope } options end @@ -51,6 +53,26 @@ def generate_sass ].flatten.join(@options[:syntax] == :sass ? "\n" : ';') end + private + def configure_cache + return unless @scope.respond_to?(:depend_on) + test = /^#{root}/ + @engine.files.uniq.each do |file| + if file =~ test + @scope.depend_on file + end + end + end + + private + def root + if @scope.respond_to?(:environment) + @scope.environment.root + else + File.dirname(@file) + end + end + private def style_variables return unless @options.has_key?(:style) @@ -61,8 +83,10 @@ def style_variables private def render_living_style_guide - engine = ::LivingStyleGuide::Engine.new(@sass, @options, sass_options) - engine.render + @engine = ::LivingStyleGuide::Engine.new(@sass, @options, sass_options) + html = @engine.render + configure_cache + html end end diff --git a/test/integration/sprockets_test.rb b/test/integration/sprockets_test.rb new file mode 100644 index 0000000..5dc49a6 --- /dev/null +++ b/test/integration/sprockets_test.rb @@ -0,0 +1,19 @@ +require 'test_helper' +require 'tilt' + +describe "Sprockets integration" do + + describe "sprockets should know when to invalidate cache" do + template = Tilt.new('test/fixtures/standalone/styleguide.html.lsg') + context = Minitest::Mock.new + %w(style.scss modules/_buttons.scss modules/_buttons.md styleguide.html.lsg).each do |file| + context.expect :depend_on, nil, ["test/fixtures/standalone/#{file}"] + end + + template.render context + + context.verify + end + +end +