Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add support for routing via map dsl in Assetfile

  • Loading branch information...
commit 185039c10589c7a7377c9c3a5c6053a4c8bc68eb 1 parent b70ca6c
Alex Kwiatkowski & Daniel Yankowsky authored
View
4 lib/rake-pipeline/dsl/project_dsl.rb
@@ -98,6 +98,10 @@ def input(*inputs, &block)
project.build_pipeline(*inputs, &block)
end
alias inputs input
+
+ def map(path, &block)
+ project.maps[path] = block
+ end
end
end
end
View
4 lib/rake-pipeline/middleware.rb
@@ -33,6 +33,10 @@ def call(env)
project.invoke_clean
path = env["PATH_INFO"]
+ if project.maps.has_key?(path)
+ return project.maps[path].call(env)
+ end
+
if filename = file_for(path)
if File.directory?(filename)
index = File.join(filename, "index.html")
View
3  lib/rake-pipeline/project.rb
@@ -9,6 +9,8 @@ class Project
# @return [Pipeline] the list of pipelines in the project
attr_reader :pipelines
+ attr_reader :maps
+
# @return [String|nil] the path to the project's Assetfile
# or nil if it was created without an Assetfile.
attr_reader :assetfile_path
@@ -216,6 +218,7 @@ def build_pipeline(input, glob=nil, &block)
# @return [void]
def reset!
@pipelines = []
+ @maps = {}
@tmpdir = "tmp"
@invoke_mutex = Mutex.new
@default_output_root = @assetfile_digest = @assetfile_path = nil
View
8 spec/dsl/project_dsl_spec.rb
@@ -27,7 +27,15 @@
project.should_receive(:build_pipeline)
dsl.input("app") {}
end
+ end
+ describe "#map" do
+ it "saves the block in a hash on the project" do
+ project.maps.keys.size.should == 0
+ run_me = lambda { }
+ dsl.map("foo", &run_me)
+ dsl.project.maps["foo"].should == run_me
+ end
end
end
View
159 spec/middleware_spec.rb
@@ -29,6 +29,11 @@
assetfile_source = <<-HERE.gsub(/^ {4}/, '')
require "#{tmp}/../support/spec_helpers/filters"
output "public"
+
+ map "/dynamic-request.js" do
+ [200, { "Content-Type" => "text/plain" }, ["I am dynamic!"]]
+ end
+
input "#{tmp}", "app/**/*" do
match "*.js" do
concat "javascripts/application.js"
@@ -43,6 +48,7 @@
modified_assetfile_source = <<-HERE.gsub(/^ {4}/, '')
require "#{tmp}/../support/spec_helpers/filters"
output "public"
+
input "#{tmp}", "app/**/*" do
match "*.js" do
concat { "javascripts/app.js" }
@@ -56,125 +62,136 @@
app = middleware = nil
+ let(:app) { middleware }
+
before do
assetfile_path = File.join(tmp, "Assetfile")
File.open(assetfile_path, "w") { |file| file.write(assetfile_source) }
app = lambda { |env| [404, {}, ['not found']] }
-
middleware = Rake::Pipeline::Middleware.new(app, assetfile_path)
+ end
- inputs.each do |name, string|
- path = File.join(tmp, name)
- if string
- mkdir_p File.dirname(path)
- File.open(path, "w") { |file| file.write(string) }
- else
- mkdir_p path
- end
- end
+ describe "dynamic requests" do
+ it "returns the value from the given block for paths that have been mapped" do
+ get "/dynamic-request.js"
- get "/javascripts/application.js"
+ last_response.should be_ok
+ last_response.headers["Content-Type"].should == "text/plain"
+ last_response.body.should == "I am dynamic!"
+ end
end
- let(:app) { middleware }
-
- it "returns files relative to the output directory" do
- last_response.should be_ok
+ describe "static requests" do
+ before do
+ inputs.each do |name, string|
+ path = File.join(tmp, name)
+ if string
+ mkdir_p File.dirname(path)
+ File.open(path, "w") { |file| file.write(string) }
+ else
+ mkdir_p path
+ end
+ end
- last_response.body.should == expected_output
- last_response.headers["Content-Type"].should == "application/javascript"
- end
+ get "/javascripts/application.js"
+ end
- it "updates the output when files change" do
- age_existing_files
+ it "returns files relative to the output directory" do
+ last_response.should be_ok
- File.open(File.join(tmp, "app/javascripts/jquery.js"), "w") do |file|
- file.write "var jQuery = {};\njQuery.trim = function() {};\n"
+ last_response.body.should == expected_output
+ last_response.headers["Content-Type"].should == "application/javascript"
end
- expected = <<-HERE.gsub(/^ {6}/, '')
+ it "updates the output when files change" do
+ age_existing_files
+
+ File.open(File.join(tmp, "app/javascripts/jquery.js"), "w") do |file|
+ file.write "var jQuery = {};\njQuery.trim = function() {};\n"
+ end
+
+ expected = <<-HERE.gsub(/^ {6}/, '')
var jQuery = {};
jQuery.trim = function() {};
var SC = {};
SC.hi = function() { console.log("hi"); };
- HERE
+ HERE
- get "/javascripts/application.js"
+ get "/javascripts/application.js"
- last_response.body.should == expected
- last_response.headers["Content-Type"].should == "application/javascript"
- end
+ last_response.body.should == expected
+ last_response.headers["Content-Type"].should == "application/javascript"
+ end
- it "updates the output when new files are added" do
- age_existing_files
+ it "updates the output when new files are added" do
+ age_existing_files
- File.open(File.join(tmp, "app/javascripts/history.js"), "w") do |file|
- file.write "var History = {};\n"
- end
+ File.open(File.join(tmp, "app/javascripts/history.js"), "w") do |file|
+ file.write "var History = {};\n"
+ end
- expected = <<-HERE.gsub(/^ {6}/, '')
+ expected = <<-HERE.gsub(/^ {6}/, '')
var History = {};
var jQuery = {};
var SC = {};
SC.hi = function() { console.log("hi"); };
- HERE
+ HERE
- get "/javascripts/application.js"
+ get "/javascripts/application.js"
- last_response.body.should == expected
- last_response.headers["Content-Type"].should == "application/javascript"
- end
+ last_response.body.should == expected
+ last_response.headers["Content-Type"].should == "application/javascript"
+ end
- it "returns index.html for directories" do
- get "/"
+ it "returns index.html for directories" do
+ get "/"
- last_response.body.should == "<html>HI</html>"
- last_response.headers["Content-Type"].should == "text/html"
+ last_response.body.should == "<html>HI</html>"
+ last_response.headers["Content-Type"].should == "text/html"
- get "/javascripts"
+ get "/javascripts"
- last_response.body.should == "<html>JAVASCRIPT</html>"
- last_response.headers["Content-Type"].should == "text/html"
- end
+ last_response.body.should == "<html>JAVASCRIPT</html>"
+ last_response.headers["Content-Type"].should == "text/html"
+ end
- it "ignores directories without index.html" do
- get "/empty_dir"
+ it "ignores directories without index.html" do
+ get "/empty_dir"
- last_response.body.should == "not found"
- last_response.status.should == 404
- end
+ last_response.body.should == "not found"
+ last_response.status.should == 404
+ end
- it "falls back to the app" do
- get "/zomg.notfound"
+ it "falls back to the app" do
+ get "/zomg.notfound"
- last_response.body.should == "not found"
- last_response.status.should == 404
- end
+ last_response.body.should == "not found"
+ last_response.status.should == 404
+ end
- it "recreates the pipeline when the Assetfile changes" do
- get "/javascripts/app.js"
- last_response.body.should == "not found"
- last_response.status.should == 404
+ it "recreates the pipeline when the Assetfile changes" do
+ get "/javascripts/app.js"
+ last_response.body.should == "not found"
+ last_response.status.should == 404
- File.open(File.join(tmp, "Assetfile"), "w") do |file|
- file.write(modified_assetfile_source)
- end
+ File.open(File.join(tmp, "Assetfile"), "w") do |file|
+ file.write(modified_assetfile_source)
+ end
- expected = <<-HERE.gsub(/^ {6}/, '')
+ expected = <<-HERE.gsub(/^ {6}/, '')
var jQuery = {};
var SC = {};
SC.hi = function() { console.log("hi"); };
- HERE
+ HERE
- get "/javascripts/app.js"
+ get "/javascripts/app.js"
- last_response.body.should == expected
- last_response.headers["Content-Type"].should == "application/javascript"
+ last_response.body.should == expected
+ last_response.headers["Content-Type"].should == "application/javascript"
+ end
end
-
-
end
View
1  spec/project_spec.rb
@@ -85,6 +85,7 @@ def assetfile_digest
describe "constructor" do
it "creates pipelines from an Assetfile given an Assetfile path" do
project = Rake::Pipeline::Project.new(assetfile_path)
+ project.maps.should == {}
pipeline = project.pipelines.last
pipeline.inputs.should == { "app/assets" => "**/*" }
pipeline.output_root.should == File.join(tmp, "public")
Please sign in to comment.
Something went wrong with that request. Please try again.