middleman/lib/middleman/builder.rb

255 lines
6.8 KiB
Ruby
Raw Normal View History

2011-01-30 23:18:49 +01:00
require "thor"
require "thor/group"
require 'rack/test'
2011-10-14 20:36:46 +02:00
require 'find'
SHARED_SERVER = Middleman.server
SHARED_SERVER.set :environment, :build
2011-09-16 19:16:23 +02:00
module Middleman
2011-01-30 23:18:49 +01:00
module ThorActions
def tilt_template(source, *args, &block)
config = args.last.is_a?(Hash) ? args.pop : {}
destination = args.first || source
# source = File.expand_path(find_in_source_paths(source.to_s))
2011-09-16 19:16:23 +02:00
# context = instance_eval('binding')
2011-07-27 23:14:22 +02:00
request_path = destination.sub(/^#{SHARED_SERVER.build_dir}/, "")
2011-10-14 22:13:21 +02:00
begin
destination, request_path = SHARED_SERVER.reroute_builder(destination, request_path)
2011-08-30 23:09:13 +02:00
request_path.gsub!(/\s/, "%20")
response = Middleman::Builder.shared_rack.get(request_path)
2011-10-16 03:24:19 +02:00
dequeue_file_from destination if cleaning?
create_file destination, nil, config do
2011-08-30 23:09:13 +02:00
response.body
end if response.status == 200
rescue
end
end
2011-10-16 03:24:19 +02:00
def clean!(destination)
return unless cleaning?
queue_current_paths_from destination
add_clean_up_callback
end
def cleaning?
options.has_key?("clean") && options["clean"]
end
def add_clean_up_callback
clean_up_callback = lambda do
files = @cleaning_queue.select { |q| File.file? q }
directories = @cleaning_queue.select { |q| File.directory? q }
files.each { |f| remove_file f, :force => true }
directories = directories.sort_by {|d| d.length }.reverse!
directories.each do |d|
remove_file d, :force => true if directory_empty? d
end
end
self.class.after_run :clean_up_callback do
clean_up_callback.call
end
end
def directory_empty?(directory)
Dir["#{directory}/*"].empty?
end
def queue_current_paths_from(destination)
@cleaning_queue = []
Find.find(destination) do |path|
2011-10-19 07:03:13 +02:00
next if path.match(/\/\./)
2011-10-16 03:24:19 +02:00
unless path == destination
@cleaning_queue << path.sub(destination, destination[/([^\/]+?)$/])
end
end
end
def dequeue_file_from(destination)
@cleaning_queue.delete_if {|q| q == destination }
end
2011-01-30 23:18:49 +01:00
end
2011-01-30 23:23:29 +01:00
class Builder < Thor::Group
2011-01-30 23:18:49 +01:00
include Thor::Actions
include Middleman::ThorActions
2011-07-27 23:14:22 +02:00
def self.shared_rack
2011-09-18 01:49:14 +02:00
@shared_rack ||= begin
2011-07-27 23:14:22 +02:00
mock = ::Rack::MockSession.new(SHARED_SERVER)
sess = ::Rack::Test::Session.new(mock)
2011-09-18 01:49:14 +02:00
response = sess.get("__middleman__")
2011-07-27 23:14:22 +02:00
sess
end
end
class_option :relative, :type => :boolean, :aliases => "-r", :default => false, :desc => 'Override the config.rb file and force relative urls'
2011-09-13 01:15:51 +02:00
class_option :glob, :type => :string, :aliases => "-g", :default => nil, :desc => 'Build a subset of the project'
2011-01-30 23:18:49 +01:00
def initialize(*args)
super
if options.has_key?("relative") && options["relative"]
SHARED_SERVER.activate :relative_assets
end
end
2011-01-30 23:18:49 +01:00
def source_paths
2011-04-20 22:16:12 +02:00
@source_paths ||= [
SHARED_SERVER.root
2011-01-30 23:18:49 +01:00
]
end
def build_all_files
2011-07-27 23:14:22 +02:00
self.class.shared_rack
2011-09-13 01:15:51 +02:00
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 })
2011-09-13 01:15:51 +02:00
SHARED_SERVER.proxied_paths.each do |url, proxy|
tilt_template(url.gsub(/^\//, "#{SHARED_SERVER.build_dir}/"), { :force => true })
end
end
end
2011-01-31 00:11:54 +01:00
@@hooks = {}
def self.after_run(name, &block)
@@hooks[name] = block
end
def run_hooks
2011-09-13 01:15:51 +02:00
return if options.has_key?("glob")
2011-01-31 00:11:54 +01:00
@@hooks.each do |name, proc|
instance_eval(&proc)
end
2011-08-14 22:38:03 +02:00
SHARED_SERVER.after_build_callbacks.each do |proc|
instance_eval(&proc)
2011-01-31 00:11:54 +01:00
end
end
end
2011-09-13 01:15:51 +02:00
class BaseAction < ::Thor::Actions::EmptyDirectory
2011-01-30 23:18:49 +01:00
attr_reader :source
def initialize(base, source, destination=nil, config={}, &block)
2011-01-30 23:18:49 +01:00
@source = File.expand_path(base.find_in_source_paths(source.to_s))
@block = block
super(base, destination, { :recursive => true }.merge(config))
end
def invoke!
2011-10-16 03:24:19 +02:00
base.clean! destination
2011-01-30 23:18:49 +01:00
execute!
end
def revoke!
execute!
end
2011-09-13 01:15:51 +02:00
protected
def handle_path(file_source)
# Skip partials prefixed with an underscore while still handling files prefixed with 2 consecutive underscores
return unless file_source.gsub(SHARED_SERVER.root, '').split('/').select { |p| p[/^_[^_]/] }.empty?
2011-09-13 01:15:51 +02:00
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
2011-01-30 23:18:49 +01:00
2011-09-13 01:15:51 +02:00
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
2011-08-06 06:37:33 +02:00
def handle_directory(lookup, &block)
lookup = File.join(lookup, '*')
results = Dir[lookup].sort do |a, b|
simple_a = a.gsub(SHARED_SERVER.root + "/", '').gsub(SHARED_SERVER.views + "/", '')
simple_b = b.gsub(SHARED_SERVER.root + "/", '').gsub(SHARED_SERVER.views + "/", '')
a_dir = simple_a.split("/").first
b_dir = simple_b.split("/").first
if a_dir == SHARED_SERVER.images_dir
-1
elsif b_dir == SHARED_SERVER.images_dir
1
else
0
end
end
2011-08-06 06:37:33 +02:00
results = results.select(&block) if block_given?
results.each do |file_source|
if File.directory?(file_source)
handle_directory(file_source)
next
end
2011-10-16 03:24:19 +02:00
handle_path(file_source)
2011-01-30 23:18:49 +01:00
end
end
def execute!
2011-08-06 06:37:33 +02:00
handle_directory(source) 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
true
end
end
2011-10-14 20:36:46 +02:00
end
end
2011-01-30 23:18:49 +01:00
end