Skip to content

Commit

Permalink
Merge pull request livingsocial#73 from wagenet/additional-paths
Browse files Browse the repository at this point in the history
Add support for additional dependencies in filters.
  • Loading branch information
Dudley Flanders committed Mar 22, 2012
2 parents 1f2029c + 7f0f4c0 commit 7635d5f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
10 changes: 9 additions & 1 deletion lib/rake-pipeline/filter.rb
Expand Up @@ -180,13 +180,21 @@ def rake_application
@rake_application || Rake.application
end

# @param [FileWrapper] file wrapper to get paths for
# @return [Array<String>] array of file paths within additional dependencies
def additional_dependencies(input)
[]
end

# Generate the Rake tasks for the output files of this filter.
#
# @see #outputs #outputs (for information on how the output files are determined)
# @return [void]
def generate_rake_tasks
@rake_tasks = outputs.map do |output, inputs|
dependencies = inputs.map(&:fullpath)
dependencies = inputs.map do |input|
[input.fullpath] + additional_dependencies(input)
end.flatten.uniq

dependencies.each { |path| create_file_task(path) }

Expand Down
38 changes: 24 additions & 14 deletions spec/filter_spec.rb
Expand Up @@ -11,7 +11,7 @@ def output_file(path, file_root=output_root)
let(:input_root) { File.join(tmp, "app/assets") }
let(:output_root) { File.join(tmp, "filter1/app/assets") }
let(:input_files) do
%w(jquery.js jquery-ui.js sproutcore.js).map { |f| input_file(f) }
%w(jquery.js jquery-ui.js ember.js).map { |f| input_file(f) }
end

it "accepts a series of FileWrapper objects for the input" do
Expand Down Expand Up @@ -101,10 +101,10 @@ def output_file(path, file_root=output_root)
filter.output_name_generator = output_name_generator
outputs = filter.outputs

outputs.keys.sort.should == [output_file("jquery.js"), output_file("sproutcore.js")]
outputs.values.sort.should == [[input_file("jquery.js"), input_file("jquery-ui.js")], [input_file("sproutcore.js")]]
outputs.keys.sort.should == [output_file("ember.js"), output_file("jquery.js")]
outputs.values.sort.should == [[input_file("ember.js")], [input_file("jquery.js"), input_file("jquery-ui.js")]]

filter.output_files.should == [output_file("jquery.js"), output_file("sproutcore.js")]
filter.output_files.should == [output_file("jquery.js"), output_file("ember.js")]
end
end

Expand All @@ -115,13 +115,17 @@ class TestFilter < Rake::Pipeline::Filter
def generate_output(inputs, output)
generate_output_block.call(inputs, output)
end

def additional_dependencies(input)
return [File.join(input.root, "extras", input.path)]
end
end

let(:filter) { TestFilter.new }
let(:input_root) { File.join(tmp, "app/assets") }
let(:output_root) { File.join(tmp, "filter1/app/assets") }
let(:input_files) do
%w(jquery.js jquery-ui.js sproutcore.js).map do |file|
%w(jquery.js jquery-ui.js ember.js).map do |file|
input_file("javascripts/#{file}")
end
end
Expand Down Expand Up @@ -169,7 +173,7 @@ def input_task(path)
filter.generate_rake_tasks
tasks = filter.rake_tasks
tasks.should == [output_task("javascripts/application.js")]
tasks[0].prerequisites.should == input_files.map { |i| i.fullpath }
tasks[0].prerequisites.should == input_files.map { |i| [i.fullpath, File.join(i.root, "extras", i.path)] }.flatten

tasks.each(&:invoke)

Expand Down Expand Up @@ -201,7 +205,8 @@ def input_task(path)
tasks.each do |task|
name = task.name.sub(/^#{Regexp.escape(output_root)}/, '')
prereq = File.join(input_root, name)
task.prerequisites.should == [prereq]
extra_prereq = File.join(input_root, "extras", name)
task.prerequisites.should == [prereq, extra_prereq]
end

tasks.each(&:invoke)
Expand All @@ -217,9 +222,9 @@ def input_task(path)
if output.path == "javascripts/jquery.js"
inputs.should == [input_file("javascripts/jquery.js"), input_file("javascripts/jquery-ui.js")]
output.should == output_file("javascripts/jquery.js")
elsif output.path == "javascripts/sproutcore.js"
inputs.should == [input_file("javascripts/sproutcore.js")]
output.should == output_file("javascripts/sproutcore.js")
elsif output.path == "javascripts/ember.js"
inputs.should == [input_file("javascripts/ember.js")]
output.should == output_file("javascripts/ember.js")
else
flunk
end
Expand All @@ -229,14 +234,19 @@ def input_task(path)

filter.generate_rake_tasks
tasks = filter.rake_tasks
tasks.sort.should == [output_task("javascripts/jquery.js"), output_task("javascripts/sproutcore.js")].sort
tasks.sort.should == [output_task("javascripts/jquery.js"), output_task("javascripts/ember.js")].sort

tasks.sort[0].prerequisites.should == [
File.join(input_root, "javascripts/jquery.js"),
File.join(input_root, "javascripts/jquery-ui.js")
File.join(input_root, "javascripts/ember.js"),
File.join(input_root, "extras/javascripts/ember.js")
]

tasks.sort[1].prerequisites.should == [File.join(input_root, "javascripts/sproutcore.js")]
tasks.sort[1].prerequisites.should == [
File.join(input_root, "javascripts/jquery.js"),
File.join(input_root, "extras/javascripts/jquery.js"),
File.join(input_root, "javascripts/jquery-ui.js"),
File.join(input_root, "extras/javascripts/jquery-ui.js")
]

tasks.each(&:invoke)

Expand Down

0 comments on commit 7635d5f

Please sign in to comment.