livingsocial / rake-pipeline

An extension to Rake for dealing with a directory of inputs, a number of filters, and a directory of outputs
MIT License
276 stars 38 forks source link

Implement a Pass-Through filter #86

Open ahawkins opened 12 years ago

ahawkins commented 12 years ago

A pass through filter is like a matcher but allows you to match like a matcher and select files outside the glob as well. Also the outputs from the filters do not directly correspond to the inputs. Here's an example:

# This line has to allow all files because eventually
# `index.html` is needed down the line by a filter. 
# `index.html` must be an input so its content can be
# read and its content be replaced.
allow "*.handlebars" do
  # works like a normal filter but by default only uses
  # the pipeline's matched files. Matched files match the
  # the glob specified in `allow`
  handlebars_script_tag

  # This filter takes the outputs of the previous filter
  # HTML script tags and injects them into `index.html`. 
  # This filter must read index.html as an input and 
  # write to it as an output. The trick here is to 
  # keep the matched files the inputs so they can 
  # be manipulated by other filters.
  inject_script_tag "index.html"

  # Other filters can request files outside the main
  # glob as inputs at this point and continue there work.
  # It's important to note that inputs to filters at this
  # point are not "index.html". They are all the files 
  # in the pipeline.
end

Implementing a pass through filter also makes generating a cache manifest easy. Simply match everything and generate an output. The original inputs are still there. This also encompasses the idea that sometimes you don't want to destroy input files, just use them.

I have implemented something like this for use in Iridium. I'm wondering if it can be adapter and pushed upstream.

ahawkins commented 12 years ago

See also: https://github.com/wycats/rake-pipeline-web-filters/pull/43