Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTML5 cache manifest filter #43

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/rake-pipeline-web-filters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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/manifest_filter"
require "rake-pipeline-web-filters/helpers"
18 changes: 17 additions & 1 deletion lib/rake-pipeline-web-filters/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def sass(*args, &block)
filter(Rake::Pipeline::Web::Filters::SassFilter, *args, &block)
end
alias_method :scss, :sass

# Add a new {StylusFilter} to the pipeline.
# @see StylusFilter#initialize
def stylus(*args, &block)
Expand Down Expand Up @@ -98,6 +98,22 @@ def less(*args, &block)
def handlebars(*args, &block)
filter(Rake::Pipeline::Web::Filters::HandlebarsFilter, *args, &block)
end
#
# Add a new {ManifestFilter} to the pipeline.
# @see ManifestFilter#initialize
def manifest(*args, &block)
# Duplicate all current items in the pipeline into a new directory.
# We must work with duplicates otherwise the initial ones will
# be blown away. The manifest filter will strip out the manifest
# directory when generating the file.
match "**/*" do
copy { |name| [name, "manifest/#{name}"] }
end

match "manifest/**/*" do
filter(Rake::Pipeline::Web::Filters::ManifestFilter, *args, &block)
end
end
end

module ProjectHelpers
Expand Down
68 changes: 68 additions & 0 deletions lib/rake-pipeline-web-filters/manifest_filter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
module Rake::Pipeline::Web::Filters
# Take all inputs and output an HTML5 cache manifest.
#
# @example
# !!!ruby
# Rake::Pipeline.build do
# input "app/assets", "**/*.js"
# output "public"
#
# # NOTE: You must match and copy othwerise files will be deleted!
# match "**/*" do
# copy { |input| [input, "manifest/#{input}" ]
# end
#
# match "manifest/**/*" do
# filter Rake::Pipeline::Web::Filters::UglifyFilter
# end
# end
class ManifestFilter < Rake::Pipeline::Filter
include Rake::Pipeline::Web::Filters::FilterWithDependencies

# @param [String] Generated file name
# @param [Proc] block a block to use as the Filter's
# {#output_name_generator}.
def initialize(name = 'cache.manifest', &block)
block ||= proc { |input| name }
super(&block)
end

# Implement the {#generate_output} method required by
# the {Filter} API. Generates a proper HTML5 cache manifest
# with all inputs inside the cache section, a unique tag,
# and states that all other requests require internet access.
#
# @param [Array<FileWrapper>] 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)
assets = inputs.map { |i| i.path.gsub('manifest/', '') }.join("\n")

output.write ERB.new(template).result(binding)
end

private

def external_dependencies
[ "erb" ]
end

def template
<<-erb
CACHE MANIFEST

# Automatically generated by rake-pipeline-web-filters
# Tag: <%= Time.now.to_i %> (<%= Time.now %>)

CACHE:
<%= assets %>

NETWORK:
# All other resources require network access
*
erb
end
end
end
13 changes: 13 additions & 0 deletions spec/helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,19 @@ def filter
filter.should be_kind_of(Rake::Pipeline::Web::Filters::HandlebarsFilter)
end
end

describe "#manifest" do
it "setups a proper manifest pipeline" do
dsl.manifest

pipeline.filters.first.should be_kind_of(Rake::Pipeline::Matcher)
pipeline.filters.first.glob.should == "**/*"

filter.should be_kind_of(Rake::Pipeline::Matcher)
filter.glob.should == "manifest/**/*"
filter.filters.first.should be_kind_of(Rake::Pipeline::Web::Filters::ManifestFilter)
end
end
end

describe "ProjectHelpers" do
Expand Down
62 changes: 62 additions & 0 deletions spec/manifest_filter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
describe "ManifestFilter" do
ManifestFilter ||= Rake::Pipeline::Web::Filters::ManifestFilter
MemoryFileWrapper ||= Rake::Pipeline::SpecHelpers::MemoryFileWrapper

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("manifest/index.html", ""),
input_file("manifest/application.js", ""),
input_file("manifest/application.css", "")
]
filter.output_root = "/path/to/output"
filter.rake_application = Rake::Application.new
filter
end

it "generates a proper cache manifest for output" do
filter = setup_filter ManifestFilter.new("cache.manifest")

filter.output_files.should == [output_file("cache.manifest")]

tasks = filter.generate_rake_tasks
tasks.each(&:invoke)

file = MemoryFileWrapper.files["/path/to/output/cache.manifest"]

file.encoding.should == "UTF-8"

file.body.lines.first.chomp.should == 'CACHE MANIFEST'

file.body.should match(%r{# Tag: \d+})

file.body.should match(/^index\.html/)
file.body.should match(/^application\.css/)
file.body.should match(/^application\.js/)
end

describe "naming output files" do
it "should use cache.manifest by default" do
filter = setup_filter ManifestFilter.new
filter.output_files.first.path.should == "cache.manifest"
end

it "accepts a string to set the output file name" do
filter = setup_filter(ManifestFilter.new("octopus"))
filter.output_files.first.path.should == "octopus"
end

it "accepts a block to customize output file names" do
filter = setup_filter(ManifestFilter.new { |input| "octopus" })
filter.output_files.first.path.should == "octopus"
end
end
end