allow build to use glob

This commit is contained in:
Thomas Reynolds 2011-09-12 16:15:51 -07:00
parent bc7ee29486
commit 61d7e8abed
6 changed files with 74 additions and 20 deletions

View file

@ -17,6 +17,12 @@ Feature: Builder
Then "images/Child folder/regular_file(example).txt" should exist at "test-app" Then "images/Child folder/regular_file(example).txt" should exist at "test-app"
And cleanup built app at "test-app" And cleanup built app at "test-app"
Scenario: Build glob
Given a built app at "glob-app" with flags "--glob '**/*.sass'"
Then "stylesheets/site.css" should exist at "glob-app" and include "html"
Then "index.html" should not exist at "glob-app"
And cleanup built app at "glob-app"
# Scenario: Force relative assets # Scenario: Force relative assets
# Given a built app at "relative-app" with flags "--relative" # Given a built app at "relative-app" with flags "--relative"
# Then "stylesheets/relative_assets.css" should exist at "relative-app" and include "../" # Then "stylesheets/relative_assets.css" should exist at "relative-app" and include "../"

View file

@ -0,0 +1 @@
page "/index.html", :layout => false

View file

@ -0,0 +1,6 @@
%h1 Welcome
:markdown
## H2
Paragraph

View file

@ -0,0 +1 @@
@import "compass/reset"

View file

@ -38,12 +38,13 @@ module Middleman
@shared_rack ||= begin @shared_rack ||= begin
mock = ::Rack::MockSession.new(SHARED_SERVER) mock = ::Rack::MockSession.new(SHARED_SERVER)
sess = ::Rack::Test::Session.new(mock) sess = ::Rack::Test::Session.new(mock)
sess.get("/") # sess.get("/")
sess sess
end end
end end
class_option :relative, :type => :boolean, :aliases => "-r", :default => false, :desc => 'Override the config.rb file and force relative urls' class_option :relative, :type => :boolean, :aliases => "-r", :default => false, :desc => 'Override the config.rb file and force relative urls'
class_option :glob, :type => :string, :aliases => "-g", :default => nil, :desc => 'Build a subset of the project'
def initialize(*args) def initialize(*args)
super super
@ -62,12 +63,16 @@ module Middleman
def build_all_files def build_all_files
self.class.shared_rack self.class.shared_rack
action Directory.new(self, SHARED_SERVER.views, SHARED_SERVER.build_dir, { :force => true }) if options.has_key?("glob")
action GlobAction.new(self, SHARED_SERVER.views, SHARED_SERVER.build_dir, { :force => true, :glob => options["glob"] })
else
action DirectoryAction.new(self, SHARED_SERVER.views, SHARED_SERVER.build_dir, { :force => true })
SHARED_SERVER.proxied_paths.each do |url, proxy| SHARED_SERVER.proxied_paths.each do |url, proxy|
tilt_template(url.gsub(/^\//, "#{SHARED_SERVER.build_dir}/"), { :force => true }) tilt_template(url.gsub(/^\//, "#{SHARED_SERVER.build_dir}/"), { :force => true })
end end
end end
end
@@hooks = {} @@hooks = {}
def self.after_run(name, &block) def self.after_run(name, &block)
@ -75,6 +80,8 @@ module Middleman
end end
def run_hooks def run_hooks
return if options.has_key?("glob")
@@hooks.each do |name, proc| @@hooks.each do |name, proc|
instance_eval(&proc) instance_eval(&proc)
end end
@ -85,7 +92,7 @@ module Middleman
end end
end end
class Directory < ::Thor::Actions::EmptyDirectory class BaseAction < ::Thor::Actions::EmptyDirectory
attr_reader :source attr_reader :source
def initialize(base, source, destination=nil, config={}, &block) def initialize(base, source, destination=nil, config={}, &block)
@ -95,7 +102,6 @@ module Middleman
end end
def invoke! def invoke!
base.empty_directory given_destination, config
execute! execute!
end end
@ -103,6 +109,51 @@ module Middleman
execute! execute!
end end
protected
def handle_path(file_source)
# Skip partials prefixed with an underscore
return unless file_source.gsub(SHARED_SERVER.root, '').split('/').select { |p| p[0,1] == '_' }.empty?
file_extension = File.extname(file_source)
file_destination = File.join(given_destination, file_source.gsub(source, '.'))
file_destination.gsub!('/./', '/')
handled_by_tilt = ::Tilt.mappings.has_key?(file_extension.gsub(/^\./, ""))
if handled_by_tilt
file_destination.gsub!(file_extension, "")
end
destination = base.tilt_template(file_source, file_destination, config, &@block)
end
end
class GlobAction < BaseAction
protected
def execute!
Dir[File.join(source, @config[:glob])].each do |path|
file_name = path.gsub(SHARED_SERVER.views + "/", "")
if file_name == "layouts"
false
elsif file_name.include?("layout.") && file_name.split(".").length == 2
false
else
next if File.directory?(path)
handle_path(path)
true
end
end
end
end
class DirectoryAction < BaseAction
def invoke!
base.empty_directory given_destination, config
super
end
protected protected
def handle_directory(lookup, &block) def handle_directory(lookup, &block)
lookup = File.join(lookup, '*') lookup = File.join(lookup, '*')
@ -131,19 +182,7 @@ module Middleman
next next
end end
# Skip partials prefixed with an underscore handle_path(file_source)
next unless file_source.gsub(SHARED_SERVER.root, '').split('/').select { |p| p[0,1] == '_' }.empty?
file_extension = File.extname(file_source)
file_destination = File.join(given_destination, file_source.gsub(source, '.'))
file_destination.gsub!('/./', '/')
handled_by_tilt = ::Tilt.mappings.has_key?(file_extension.gsub(/^\./, ""))
if handled_by_tilt
file_destination.gsub!(file_extension, "")
end
destination = base.tilt_template(file_source, file_destination, config, &@block)
end end
end end

View file

@ -49,7 +49,8 @@ module Middleman
end end
desc "build", "Builds the static site for deployment" desc "build", "Builds the static site for deployment"
method_option "relative", :type => :boolean, :aliases => "-r", :default => false, :desc => 'Override the config.rb file and force relative urls' method_option :relative, :type => :boolean, :aliases => "-r", :default => false, :desc => 'Override the config.rb file and force relative urls'
method_option :glob, :type => :string, :aliases => "-g", :default => nil, :desc => 'Build a subset of the project'
def build def build
v1_check v1_check
thor_group = Middleman::Builder.new([], options).invoke_all thor_group = Middleman::Builder.new([], options).invoke_all