Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

NeuterFilter works with additional_dependencies #46

Open
wants to merge 7 commits into from

2 participants

@ahawkins

All tests pass at the moment. I'm currently looking for a way to refactor the relationship between the NeuterFilter, NeuterWrapper and NeuterBatch because the code feels very odd. It's especially odd since now there additional responsibilities to implement additional_dependencies.

@wagenet
Collaborator

Does additional_dependencies not work as it stands?

@ahawkins

@wagenet it did IIRC if you explicitly declared all the required files. This PR takes that step out and make require detection automatic.

@ahawkins

@wagenet @wycats ping, this should really be merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 7, 2012
  1. Update NeuterFilter#additional_dependencies

    twinturbo authored
  2. Refactor the neuterfilter

    twinturbo authored
  3. More refactoring

    twinturbo authored
  4. Add a test for no dependencies

    twinturbo authored
  5. Tests passing

    twinturbo authored
  6. Remove method

    twinturbo authored
  7. Refactor NeuterFilter internals

    twinturbo authored
This page is out of date. Refresh to see the latest.
View
68 lib/rake-pipeline-web-filters/neuter_filter.rb
@@ -1,9 +1,8 @@
module Rake::Pipeline::Web::Filters
class NeuterBatch
- def initialize(config, known_files)
+ def initialize(config)
@config = config || {}
- @known_files = known_files
@required = []
end
@@ -15,9 +14,6 @@ def file_wrapper(klass, *args)
end
def required(req)
- unless @known_files.include?(req)
- warn "Included '#{req}', which is not listed in :additional_dependencies. The pipeline may not invalidate properly."
- end
@required << req
end
@@ -25,12 +21,8 @@ def required?(req)
@required.include?(req)
end
- def strip_requires(source)
- requires = []
- regexp = @config[:require_regexp] || %r{^\s*require\(['"]([^'"]*)['"]\);?\s*}
- # FIXME: This $1 may not be reliable with other regexps
- source.gsub!(regexp){ requires << $1; '' }
- requires
+ def regexp
+ @config[:require_regexp] || %r{^\s*require\(['"]([^'"]*)['"]\);?\s*}
end
def transform_path(path, input)
@@ -49,24 +41,38 @@ def filename_comment(input)
module NeuterWrapper
def batch(batch)
@batch = batch
- @batch.required(fullpath)
end
- def read
- source = super
+ def neuter
+ return if required?
- required_files = @batch.strip_requires(source).map do |req|
- req_path = @batch.transform_path(req, self)
- if req_path && !@batch.required?(File.expand_path(req_path, root))
- @batch.file_wrapper(self.class, root, req_path, encoding).read
- else
- nil
- end
- end.compact
+ @batch.required fullpath
+
+ files_to_inject = dependencies.reject(&:required?)
+ dependent_content = files_to_inject.map(&:neuter).compact
+
+ this_file = @batch.filename_comment(self) + @batch.closure_wrap(stripped_source)
- file = @batch.filename_comment(self) + @batch.closure_wrap(source)
+ [dependent_content, this_file].reject(&:empty?).join("\n\n")
+ end
+
+ def required?
+ @batch.required? fullpath
+ end
- (required_files << file).join("\n\n")
+ def requires
+ read.scan(@batch.regexp).flatten
+ end
+
+ def dependencies
+ requires.map do |req|
+ req_path = @batch.transform_path(req, self)
+ @batch.file_wrapper(self.class, root, req_path, encoding)
+ end
+ end
+
+ def stripped_source
+ read.gsub @batch.regexp, ''
end
end
@@ -95,16 +101,20 @@ def initialize(string=nil, config={}, &block)
def generate_output(inputs, output)
inputs.each do |input|
- known_files = [input.fullpath] + additional_dependencies(input)
- batch = NeuterBatch.new(@config, known_files)
+ batch = NeuterBatch.new @config
file = batch.file_wrapper(file_wrapper_class, input.root, input.path, input.encoding)
- output.write file.read
+ output.write file.neuter
end
end
def additional_dependencies(input)
- method = @config[:additional_dependencies]
- method ? method.call(input).map{|p| File.expand_path(p, input.root) } : []
+ dependent_files(input).map(&:fullpath)
+ end
+
+ def dependent_files(input)
+ batch = NeuterBatch.new @config
+ wrapper = batch.file_wrapper(file_wrapper_class, input.root, input.path, input.encoding)
+ wrapper.dependencies
end
end
end
View
46 spec/neuter_filter_spec.rb
@@ -14,7 +14,6 @@ def make_data(name, data)
def make_filter(input_files, *args)
opts = args.last.is_a?(Hash) ? args.pop : {}
- opts[:additional_dependencies] ||= proc{|input| %w(b c) }
args.push(opts)
filter = Rake::Pipeline::Web::Filters::NeuterFilter.new(*args)
@@ -83,7 +82,7 @@ def capture(*streams)
["lib/a", "require('lib/b');\nA"],
["lib/b", "require('lib/c');\nB"],
["lib/c", "C"]
- ], :additional_dependencies => proc{ %w(lib/b lib/c) })
+ ])
output_file.body.should == "C\n\nB\n\nA"
end
@@ -140,8 +139,7 @@ def capture(*streams)
["lib/a.js", "require('b');\nA"],
["lib/b.js", "require('c');\nB"],
["lib/c.js", "C"]
- ], :path_transform => proc{|path| "lib/#{path}.js" },
- :additional_dependencies => proc{ %w(lib/b.js lib/c.js) })
+ ], :path_transform => proc{|path| "lib/#{path}.js" })
output_file.body.should == "C\n\nB\n\nA"
end
@@ -175,29 +173,29 @@ def capture(*streams)
end
describe "additional_dependencies" do
- it "warns if required file is not contained" do
- output = capture(:stderr) do
- make_filter_with_inputs([
- ["d", "require('e');\nD"],
- ["e", "require('f');\nE"],
- ["f", "F"]
- ])
- end
-
- output.should include("Included '/path/to/input/e', which is not listed in :additional_dependencies. The pipeline may not invalidate properly.")
- output.should include("Included '/path/to/input/f', which is not listed in :additional_dependencies. The pipeline may not invalidate properly.")
+ let!(:main_input_file) { make_input("a", %Q{require("b");}) }
+ let!(:required_input_file) { make_input("b", "foo") }
+
+ it "determines them from require statments in the file" do
+ filter = Rake::Pipeline::Web::Filters::NeuterFilter.new
+ filter.file_wrapper_class = MemoryFileWrapper
+ filter.input_files = []
+ filter.output_root = "/path/to/output"
+ filter.rake_application = Rake::Application.new
+
+ dependencies = filter.additional_dependencies(main_input_file)
+ dependencies.should include(required_input_file.fullpath)
end
- it "does not warn if full paths are provided" do
- output = capture(:stderr) do
- make_filter_with_inputs([
- ["d", "require('e');\nD"],
- ["e", "require('f');\nE"],
- ["f", "F"]
- ], :additional_dependencies => proc{ %w(/path/to/input/e /path/to/input/f) })
- end
+ it "determines there are no dependencies" do
+ filter = Rake::Pipeline::Web::Filters::NeuterFilter.new
+ filter.file_wrapper_class = MemoryFileWrapper
+ filter.output_root = "/path/to/output"
+ filter.rake_application = Rake::Application.new
+ filter.input_files = []
- output.should == ""
+ dependencies = filter.additional_dependencies(required_input_file)
+ dependencies.should == []
end
end
end
View
1  spec/sass_filter_spec.rb
@@ -143,5 +143,4 @@ def write_input_file(filename, contents='', root=tmp)
tasks.each(&:invoke)
end
end
-
end
Something went wrong with that request. Please try again.