diff --git a/lib/rake-pipeline-web-filters.rb b/lib/rake-pipeline-web-filters.rb index 277668e..b026eec 100644 --- a/lib/rake-pipeline-web-filters.rb +++ b/lib/rake-pipeline-web-filters.rb @@ -26,4 +26,5 @@ module Filters require "rake-pipeline-web-filters/chained_filter" require "rake-pipeline-web-filters/less_filter" require "rake-pipeline-web-filters/handlebars_filter" +require "rake-pipeline-web-filters/erb_filter" require "rake-pipeline-web-filters/helpers" diff --git a/lib/rake-pipeline-web-filters/erb_filter.rb b/lib/rake-pipeline-web-filters/erb_filter.rb new file mode 100644 index 0000000..ed02505 --- /dev/null +++ b/lib/rake-pipeline-web-filters/erb_filter.rb @@ -0,0 +1,49 @@ +require 'rake-pipeline-web-filters/filter_with_dependencies' + +module Rake::Pipeline::Web::Filters + # Evalute each file as an ERB template + # + # @example + # !!!ruby + # Rake::Pipeline.build do + # input "app/assets", "**/*.js.erb" + # output "public" + # + # # Compile each JS file under the app/assets + # # directory as an ERB template + # filter Rake::Pipeline::Web::Filters::ErbFilter + # end + class ErbFilter < Rake::Pipeline::Filter + include Rake::Pipeline::Web::Filters::FilterWithDependencies + + # @param [Binding] binding to evaluate the template in + # @param [Proc] block a block to use as the Filter's + # {#output_name_generator}. + def initialize(binding = binding, &block) + @binding = binding + block ||= proc { |input| input.gsub(/\.erb$/, '') } + super(&block) + end + + # Implement the {#generate_output} method required by + # the {Filter} API. Parses each file as an ERB template + # and evaluates it against the {binding} + # + # @param [Array] inputs an Array of + # {FileWrapper} objects representing the inputs to + # this filter. + # @param [FileWrapper] output a single {FileWrapper} + # object representing the output. + def generate_output(inputs, output) + inputs.each do |input| + output.write ERB.new(input.read).result(@binding) + end + end + + private + + def external_dependencies + [ 'erb' ] + end + end +end diff --git a/lib/rake-pipeline-web-filters/helpers.rb b/lib/rake-pipeline-web-filters/helpers.rb index 76aa7c6..d955b7f 100644 --- a/lib/rake-pipeline-web-filters/helpers.rb +++ b/lib/rake-pipeline-web-filters/helpers.rb @@ -98,6 +98,12 @@ def less(*args, &block) def handlebars(*args, &block) filter(Rake::Pipeline::Web::Filters::HandlebarsFilter, *args, &block) end + # + # Add a new {ErbFilter} to the pipeline. + # @see ErbFilter#initialize + def erb(*args, &block) + filter(Rake::Pipeline::Web::Filters::ErbFilter, *args, &block) + end end module ProjectHelpers diff --git a/spec/erb_filter_spec.rb b/spec/erb_filter_spec.rb new file mode 100644 index 0000000..212cb46 --- /dev/null +++ b/spec/erb_filter_spec.rb @@ -0,0 +1,47 @@ +describe "ErbFilter" do + ErbFilter ||= Rake::Pipeline::Web::Filters::ErbFilter + MemoryFileWrapper ||= Rake::Pipeline::SpecHelpers::MemoryFileWrapper + User = Struct.new(:name) + + let(:input) { "<%= user.name %>" } + let(:user) { User.new "Adam" } + + def input_file(name, content) + MemoryFileWrapper.new("/path/to/input", name, "UTF-8", content) + end + + def output_file(name) + MemoryFileWrapper.new("/path/to/output", name, "UTF-8") + end + + def setup_filter(filter) + filter.file_wrapper_class = MemoryFileWrapper + filter.input_files = [input_file("test.html.erb", input)] + filter.output_root = "/path/to/output" + filter.rake_application = Rake::Application.new + filter + end + + it "generates output" do + filter = setup_filter ErbFilter.new(binding) + filter.output_files.should == [output_file("test.html")] + tasks = filter.generate_rake_tasks + tasks.each(&:invoke) + + file = MemoryFileWrapper.files["/path/to/output/test.html"] + file.body.should == user.name + file.encoding.should == "UTF-8" + end + + describe "naming output files" do + it "strips .erb extensions by default" do + filter = setup_filter ErbFilter.new + filter.output_files.first.path.should == "test.html" + end + + it "accepts a block to customize output file names" do + filter = setup_filter(ErbFilter.new { |input| "octopus" }) + filter.output_files.first.path.should == "octopus" + end + end +end diff --git a/spec/helpers_spec.rb b/spec/helpers_spec.rb index 3d1a8c5..1b029bc 100644 --- a/spec/helpers_spec.rb +++ b/spec/helpers_spec.rb @@ -102,6 +102,13 @@ def filter filter.should be_kind_of(Rake::Pipeline::Web::Filters::HandlebarsFilter) end end + + describe "#erb" do + it "creates ErbFilter" do + dsl.erb + filter.should be_kind_of(Rake::Pipeline::Web::Filters::ErbFilter) + end + end end describe "ProjectHelpers" do