Skip to content

Commit

Permalink
Add #register to ProjectHelper
Browse files Browse the repository at this point in the history
This feature makes it possible to register that a
file extension should be processed using a
specified filter.

It is implemented by adding a before_filter to all
PipelineDSLs that use the ChainedFilter to
process input files before they are passed to the
rest of the filters.
  • Loading branch information
wycats committed Mar 24, 2012
1 parent bbc5e88 commit d821391
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 7 deletions.
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ source "http://rubygems.org"
# Specify your gem's dependencies in rake-pipeline-web-filters.gemspec # Specify your gem's dependencies in rake-pipeline-web-filters.gemspec
gemspec gemspec


gem "rake-pipeline", :git => "git://github.com/livingsocial/rake-pipeline.git" gem "rake-pipeline", :path => "~/Code/rake-pipeline"
#gem "rake-pipeline", :git => "git://github.com/livingsocial/rake-pipeline.git"
gem "pry" gem "pry"
1 change: 1 addition & 0 deletions Rakefile
Original file line number Original file line Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env rake #!/usr/bin/env rake
require "bundler/gem_tasks" require "bundler/gem_tasks"
require "bundler/setup"


desc "run the specs" desc "run the specs"
task :spec do task :spec do
Expand Down
7 changes: 7 additions & 0 deletions lib/rake-pipeline-web-filters/chained_filter.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -47,7 +47,14 @@ def write(contents)
# a file like +application.js.coffee.erb+ will first # a file like +application.js.coffee.erb+ will first
# apply the +ErbFilter+, then the +CoffeeFilter+, and # apply the +ErbFilter+, then the +CoffeeFilter+, and
# then output +application.js+. # then output +application.js+.
#
# This filter is largely designed for use with the
# {ProjectHelpers#register register} helper, which
# will transparently add a ChainedFilter before each
# input block with the registered extensions.
class ChainedFilter < Rake::Pipeline::Filter class ChainedFilter < Rake::Pipeline::Filter
attr_reader :filters

# @param [Hash] options # @param [Hash] options
# @option options [Hash] :types # @option options [Hash] :types
# A hash of file extensions and their associated # A hash of file extensions and their associated
Expand Down
26 changes: 24 additions & 2 deletions lib/rake-pipeline-web-filters/helpers.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module Rake::Pipeline::Web::Filters
# match("*.scss") do # match("*.scss") do
# sass :syntax => :sass # sass :syntax => :sass
# end # end
module Helpers module PipelineHelpers
# Add a new {MinispadeFilter} to the pipeline. # Add a new {MinispadeFilter} to the pipeline.
# @see MinispadeFilter#initialize # @see MinispadeFilter#initialize
def minispade(*args, &block) def minispade(*args, &block)
Expand Down Expand Up @@ -81,6 +81,28 @@ def handlebars(*args, &block)
filter(Rake::Pipeline::Web::Filters::HandlebarsFilter, *args, &block) filter(Rake::Pipeline::Web::Filters::HandlebarsFilter, *args, &block)
end end
end end

module ProjectHelpers
# Register a filter class for a particular file extension
# and add a ChainedFilter as a before filter.
#
# If this is the first use of +register+, it will set up
# the before filter. Subsequent uses will just update the
# types hash.
#
# @see ChainedFilter
def register(extension, klass)
if @types_hash
@types_hash[extension] = klass
else
@types_hash = { extension => klass }
before_filter ChainedFilter, { :types => @types_hash }
end
end
end
end end


Rake::Pipeline::DSL::PipelineDSL.send(:include, Rake::Pipeline::Web::Filters::Helpers) require "rake-pipeline/dsl"

Rake::Pipeline::DSL::PipelineDSL.send(:include, Rake::Pipeline::Web::Filters::PipelineHelpers)
Rake::Pipeline::DSL::ProjectDSL.send(:include, Rake::Pipeline::Web::Filters::ProjectHelpers)
10 changes: 7 additions & 3 deletions spec/coffee_script_filter_spec.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ def output_file(name)
MemoryFileWrapper.new("/path/to/output", name, "UTF-8") MemoryFileWrapper.new("/path/to/output", name, "UTF-8")
end end


def should_match(expected, output)
"#{expected}\n".gsub(/\n+/, "\n").should == "#{output}\n".gsub(/\n+/, "\n")
end

def setup_filter(filter) def setup_filter(filter)
filter.file_wrapper_class = MemoryFileWrapper filter.file_wrapper_class = MemoryFileWrapper
filter.input_files = [input_file("input.coffee", coffee_input)] filter.input_files = [input_file("input.coffee", coffee_input)]
Expand All @@ -57,7 +61,7 @@ def setup_filter(filter)
tasks.each(&:invoke) tasks.each(&:invoke)


file = MemoryFileWrapper.files["/path/to/output/input.js"] file = MemoryFileWrapper.files["/path/to/output/input.js"]
file.body.should == expected_coffee_output should_match file.body, expected_coffee_output
file.encoding.should == "UTF-8" file.encoding.should == "UTF-8"
end end


Expand All @@ -70,7 +74,7 @@ def setup_filter(filter)
tasks.each(&:invoke) tasks.each(&:invoke)


file = MemoryFileWrapper.files["/path/to/output/input.js"] file = MemoryFileWrapper.files["/path/to/output/input.js"]
file.body.should == expected_unwrapped_coffee_output should_match file.body, expected_unwrapped_coffee_output
file.encoding.should == "UTF-8" file.encoding.should == "UTF-8"
end end


Expand Down Expand Up @@ -98,7 +102,7 @@ def setup_filter(filter)
tasks = filter.generate_rake_tasks tasks = filter.generate_rake_tasks
lambda { lambda {
tasks.each(&:invoke) tasks.each(&:invoke)
}.should raise_error(ExecJS::RuntimeError, "Error compiling input.coffee. reserved word \"function\" on line 1") }.should raise_error(ExecJS::RuntimeError, /Error compiling input.coffee. reserved word "function" on line 1/i)
end end
end end


Expand Down
32 changes: 32 additions & 0 deletions spec/helpers_spec.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -90,3 +90,35 @@ def filter
end end
end end
end end

describe "ProjectHelpers" do
def project
@project ||= Rake::Pipeline::Project.new
end

def dsl
@dsl ||= Rake::Pipeline::DSL::ProjectDSL.new(project)
end

describe "register" do
it "registers filters per file name" do
dsl.register :coffee, Rake::Pipeline::Web::Filters::CoffeeScriptFilter
dsl.register :handlebars, Rake::Pipeline::Web::Filters::HandlebarsFilter

dsl.input "lib" do
concat "lib.js"
end

dsl.input "tests" do
concat "tests.js"
end

project.pipelines.size.should == 2

project.pipelines.each do |pipeline|
pipeline.filters.first.should be_kind_of Rake::Pipeline::Web::Filters::ChainedFilter
pipeline.filters.first.filters.keys.should == [:coffee, :handlebars]
end
end
end
end
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,6 @@
require "pry"
require "rake-pipeline" require "rake-pipeline"
require "rake-pipeline-web-filters" require "rake-pipeline-web-filters"
require "pry"


class Rake::Pipeline class Rake::Pipeline
module SpecHelpers module SpecHelpers
Expand Down

0 comments on commit d821391

Please sign in to comment.