From 3858354842736923aa62149c7669a075bbaa05fe Mon Sep 17 00:00:00 2001 From: Joe Fiorini Date: Thu, 25 Oct 2012 20:57:18 -0400 Subject: [PATCH] Allow specifying default pipeline options --- lib/rake-pipeline/middleware.rb | 4 ++-- lib/rake-pipeline/project.rb | 18 +++++++++++++----- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/lib/rake-pipeline/middleware.rb b/lib/rake-pipeline/middleware.rb index 29355eb..33009e4 100644 --- a/lib/rake-pipeline/middleware.rb +++ b/lib/rake-pipeline/middleware.rb @@ -19,9 +19,9 @@ class Middleware # @param [#call] app a Rack application # @param [String|Rake::Pipeline] pipeline either a path to an # Assetfile to use to build a pipeline, or an existing pipeline. - def initialize(app, pipeline) + def initialize(app, pipeline, &defaults) @app = app - @project = Rake::Pipeline::Project.new(pipeline) + @project = Rake::Pipeline::Project.new(pipeline, &defaults) end # Automatically compiles your assets if required and diff --git a/lib/rake-pipeline/project.rb b/lib/rake-pipeline/project.rb index 8cdda85..cd3de86 100644 --- a/lib/rake-pipeline/project.rb +++ b/lib/rake-pipeline/project.rb @@ -27,6 +27,8 @@ class Project # write their outputs by default attr_reader :default_output_root + attr_reader :defaults + # @return [Array] a list of filters to be applied before # the specified filters in every pipeline attr_writer :before_filters @@ -86,14 +88,17 @@ def add_to_digest(str) # @param [String|Pipeline] assetfile_or_pipeline # if this a String, create a Pipeline from the Assetfile at # that path. If it's a Pipeline, just wrap that pipeline. - def initialize(assetfile_or_pipeline=nil) + def initialize(assetfile_or_pipeline=nil, &defaults) reset! + if assetfile_or_pipeline.kind_of?(String) @assetfile_path = File.expand_path(assetfile_or_pipeline) - rebuild_from_assetfile(@assetfile_path) + @defaults = defaults + rebuild_from_assetfile(@assetfile_path, &defaults) elsif assetfile_or_pipeline @pipelines << assetfile_or_pipeline end + end # Evaluate a block using the Rake::Pipeline::DSL::ProjectDSL @@ -113,7 +118,7 @@ def invoke if assetfile_path source = File.read(assetfile_path) if digest(source) != assetfile_digest - rebuild_from_assetfile(assetfile_path, source) + rebuild_from_assetfile(assetfile_path, source, &defaults) end end @@ -241,12 +246,15 @@ def reset! # evaluated instead of reading the file at assetfile_path. # # @return [void] - def rebuild_from_assetfile(path, source=nil) + def rebuild_from_assetfile(path, source=nil, &default_config) reset! source ||= File.read(path) @assetfile_digest = digest(source) @assetfile_path = path - build { instance_eval(source, path, 1) } + build do + instance_eval &default_config if block_given? + instance_eval(source, path, 1) + end end # Setup the pipeline so its output files will be up to date.