2010-09-05 22:28:38 -04:00
|
|
|
require 'middleman/server'
|
2011-01-30 17:18:49 -05:00
|
|
|
require "thor"
|
|
|
|
require "thor/group"
|
|
|
|
require 'rack/test'
|
2009-10-22 20:25:15 -04:00
|
|
|
|
2011-01-30 17:18:49 -05:00
|
|
|
module Middleman
|
|
|
|
module ThorActions
|
|
|
|
def tilt_template(source, *args, &block)
|
|
|
|
config = args.last.is_a?(Hash) ? args.pop : {}
|
|
|
|
destination = args.first || source
|
2011-05-31 01:33:11 -04:00
|
|
|
|
2011-01-30 17:18:49 -05:00
|
|
|
source = File.expand_path(find_in_source_paths(source.to_s))
|
|
|
|
context = instance_eval('binding')
|
2009-10-22 20:25:15 -04:00
|
|
|
|
2011-01-30 18:11:54 -05:00
|
|
|
@@rack_test ||= ::Rack::Test::Session.new(::Rack::MockSession.new(Middleman::Server))
|
2009-10-22 20:25:15 -04:00
|
|
|
|
2011-01-30 17:18:49 -05:00
|
|
|
create_file destination, nil, config do
|
|
|
|
# The default render just requests the page over Rack and writes the response
|
2011-05-08 00:49:23 -04:00
|
|
|
request_path = destination.sub(/^#{Middleman::Server.build_dir}/, "")
|
2011-01-30 17:18:49 -05:00
|
|
|
@@rack_test.get(request_path)
|
|
|
|
@@rack_test.last_response.body
|
2009-10-22 20:25:15 -04:00
|
|
|
end
|
|
|
|
end
|
2011-01-30 17:18:49 -05:00
|
|
|
end
|
|
|
|
|
2011-01-30 17:23:29 -05:00
|
|
|
class Builder < Thor::Group
|
2011-01-30 17:18:49 -05:00
|
|
|
include Thor::Actions
|
|
|
|
include Middleman::ThorActions
|
|
|
|
|
2011-04-19 17:24:21 -04:00
|
|
|
class_option :relative, :type => :boolean, :aliases => "-r", :default => false, :desc => 'Override the config.rb file and force relative urls'
|
|
|
|
|
2011-01-30 17:18:49 -05:00
|
|
|
def initialize(*args)
|
|
|
|
super
|
2011-04-19 17:24:21 -04:00
|
|
|
|
|
|
|
Middleman::Server.new
|
2011-05-31 01:33:11 -04:00
|
|
|
|
2011-04-19 17:24:21 -04:00
|
|
|
if options.has_key?("relative") && options["relative"]
|
|
|
|
Middleman::Server.activate :relative_assets
|
|
|
|
end
|
2009-10-22 20:25:15 -04:00
|
|
|
end
|
2011-01-30 17:18:49 -05:00
|
|
|
|
2011-04-20 16:16:12 -04:00
|
|
|
|
2011-01-30 17:18:49 -05:00
|
|
|
def source_paths
|
2011-04-20 16:16:12 -04:00
|
|
|
@source_paths ||= [
|
2011-05-31 01:33:11 -04:00
|
|
|
Middleman::Server.root
|
2011-01-30 17:18:49 -05:00
|
|
|
]
|
2009-10-22 20:25:15 -04:00
|
|
|
end
|
|
|
|
|
2011-05-31 01:33:11 -04:00
|
|
|
def build_all_files
|
|
|
|
action Directory.new(self, Middleman::Server.views, Middleman::Server.build_dir, { :force => true })
|
2009-10-22 20:25:15 -04:00
|
|
|
end
|
2011-01-30 18:11:54 -05:00
|
|
|
|
|
|
|
@@hooks = {}
|
|
|
|
def self.after_run(name, &block)
|
|
|
|
@@hooks[name] = block
|
|
|
|
end
|
|
|
|
|
|
|
|
def run_hooks
|
|
|
|
@@hooks.each do |name, proc|
|
|
|
|
instance_eval(&proc)
|
|
|
|
end
|
|
|
|
end
|
2009-10-22 20:25:15 -04:00
|
|
|
end
|
|
|
|
|
2011-01-30 17:18:49 -05:00
|
|
|
class Directory < ::Thor::Actions::EmptyDirectory
|
|
|
|
attr_reader :source
|
|
|
|
|
2011-05-31 01:33:11 -04:00
|
|
|
def initialize(base, source, destination=nil, config={}, &block)
|
2011-01-30 17:18:49 -05: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!
|
|
|
|
base.empty_directory given_destination, config
|
|
|
|
execute!
|
|
|
|
end
|
|
|
|
|
|
|
|
def revoke!
|
|
|
|
execute!
|
|
|
|
end
|
|
|
|
|
2011-02-06 13:57:28 -05:00
|
|
|
protected
|
2011-04-10 17:37:36 -04:00
|
|
|
def handle_directory(lookup)
|
2011-05-08 00:49:23 -04:00
|
|
|
lookup = File.join(lookup, '*')
|
2011-02-06 13:57:28 -05:00
|
|
|
|
2011-06-24 15:06:28 -04:00
|
|
|
results = Dir[lookup].sort do |a, b|
|
2011-06-27 15:33:21 -04:00
|
|
|
simple_a = a.gsub(Middleman::Server.root + "/", '').gsub(Middleman::Server.views + "/", '')
|
|
|
|
simple_b = b.gsub(Middleman::Server.root + "/", '').gsub(Middleman::Server.views + "/", '')
|
2011-06-24 15:06:28 -04:00
|
|
|
|
|
|
|
a_dir = simple_a.split("/").first
|
|
|
|
b_dir = simple_b.split("/").first
|
|
|
|
|
|
|
|
if a_dir == Middleman::Server.images_dir
|
|
|
|
-1
|
|
|
|
elsif b_dir == Middleman::Server.images_dir
|
|
|
|
1
|
|
|
|
else
|
|
|
|
0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
results.each do |file_source|
|
2011-04-10 17:37:36 -04:00
|
|
|
if File.directory?(file_source)
|
|
|
|
handle_directory(file_source)
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
2011-07-01 13:07:50 -04:00
|
|
|
next if file_source.include?('layout') && !file_source.include?('.css')
|
2011-05-31 01:33:11 -04:00
|
|
|
|
|
|
|
# Skip partials prefixed with an underscore
|
2011-06-06 06:40:14 -04:00
|
|
|
next unless file_source.gsub(Middleman::Server.root, '').split('/').select { |p| p[0,1] == '_' }.empty?
|
2011-05-31 01:33:11 -04:00
|
|
|
|
2011-02-06 13:57:28 -05:00
|
|
|
file_extension = File.extname(file_source)
|
|
|
|
file_destination = File.join(given_destination, file_source.gsub(source, '.'))
|
|
|
|
file_destination.gsub!('/./', '/')
|
2011-04-10 17:37:36 -04:00
|
|
|
|
2011-05-31 01:33:11 -04:00
|
|
|
handled_by_tilt = ::Tilt.mappings.has_key?(file_extension.gsub(/^\./, ""))
|
|
|
|
if handled_by_tilt
|
|
|
|
file_destination.gsub!(file_extension, "")
|
2011-01-30 17:18:49 -05:00
|
|
|
end
|
2011-05-31 01:33:11 -04:00
|
|
|
|
|
|
|
destination = base.tilt_template(file_source, file_destination, config, &@block)
|
2011-01-30 17:18:49 -05:00
|
|
|
end
|
2011-02-06 13:57:28 -05:00
|
|
|
end
|
2011-04-10 17:37:36 -04:00
|
|
|
|
|
|
|
def execute!
|
|
|
|
handle_directory(source)
|
|
|
|
end
|
2009-10-22 20:25:15 -04:00
|
|
|
end
|
2011-01-30 17:18:49 -05:00
|
|
|
end
|