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'
|
2009-10-23 02:25:15 +02:00
|
|
|
|
2011-11-18 09:34:56 +01:00
|
|
|
SHARED_SERVER_INST = Middleman.server.inst do
|
|
|
|
set :environment, :build
|
|
|
|
end
|
|
|
|
SHARED_SERVER = SHARED_SERVER_INST.class
|
2011-07-13 09:38:04 +02:00
|
|
|
|
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
|
2011-05-31 07:33:11 +02:00
|
|
|
|
2011-11-18 09:34:56 +01:00
|
|
|
request_path = destination.sub(/^#{SHARED_SERVER_INST.build_dir}/, "")
|
2011-07-24 07:21:52 +02:00
|
|
|
|
2011-11-18 22:38:18 +01:00
|
|
|
begin
|
2011-11-18 23:09:48 +01:00
|
|
|
destination, request_path = SHARED_SERVER_INST.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
|
|
|
|
2011-07-28 04:59:38 +02:00
|
|
|
create_file destination, nil, config do
|
2011-08-30 23:09:13 +02:00
|
|
|
response.body
|
|
|
|
end if response.status == 200
|
2011-11-18 22:38:18 +01:00
|
|
|
rescue
|
|
|
|
say_status :error, destination, :red
|
|
|
|
end
|
2009-10-23 02:25:15 +02:00
|
|
|
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-11-18 09:34:56 +01:00
|
|
|
mock = ::Rack::MockSession.new(SHARED_SERVER.to_rack_app)
|
2011-07-27 23:14:22 +02:00
|
|
|
sess = ::Rack::Test::Session.new(mock)
|
2011-11-18 23:09:48 +01:00
|
|
|
# response = sess.get("__middleman__")
|
2011-07-27 23:14:22 +02:00
|
|
|
sess
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-04-19 23:24:21 +02:00
|
|
|
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-04-19 23:24:21 +02:00
|
|
|
|
2011-01-30 23:18:49 +01:00
|
|
|
def initialize(*args)
|
|
|
|
super
|
2011-04-19 23:24:21 +02:00
|
|
|
|
|
|
|
if options.has_key?("relative") && options["relative"]
|
2011-11-18 09:34:56 +01:00
|
|
|
SHARED_SERVER_INST.activate :relative_assets
|
2011-04-19 23:24:21 +02:00
|
|
|
end
|
2009-10-23 02:25:15 +02:00
|
|
|
end
|
2011-01-30 23:18:49 +01:00
|
|
|
|
|
|
|
def source_paths
|
2011-04-20 22:16:12 +02:00
|
|
|
@source_paths ||= [
|
2011-11-18 09:34:56 +01:00
|
|
|
SHARED_SERVER_INST.root
|
2011-01-30 23:18:49 +01:00
|
|
|
]
|
2009-10-23 02:25:15 +02:00
|
|
|
end
|
|
|
|
|
2011-05-31 07:33:11 +02:00
|
|
|
def build_all_files
|
2011-07-27 23:14:22 +02:00
|
|
|
self.class.shared_rack
|
|
|
|
|
2011-11-08 07:34:02 +01:00
|
|
|
opts = { }
|
|
|
|
opts[:glob] = options["glob"] if options.has_key?("glob")
|
|
|
|
opts[:clean] = options["clean"] if options.has_key?("clean")
|
2011-07-24 07:21:52 +02:00
|
|
|
|
2011-11-18 09:34:56 +01:00
|
|
|
action GlobAction.new(self, SHARED_SERVER_INST, opts)
|
2011-11-09 00:10:53 +01:00
|
|
|
|
2011-11-19 07:57:25 +01:00
|
|
|
SHARED_SERVER_INST.run_hook :after_build, self
|
2011-01-31 00:11:54 +01:00
|
|
|
end
|
2009-10-23 02:25:15 +02:00
|
|
|
end
|
|
|
|
|
2011-11-08 07:34:02 +01:00
|
|
|
class GlobAction < ::Thor::Actions::EmptyDirectory
|
2011-01-30 23:18:49 +01:00
|
|
|
attr_reader :source
|
|
|
|
|
2011-11-08 07:34:02 +01:00
|
|
|
def initialize(base, app, config={}, &block)
|
|
|
|
@app = app
|
|
|
|
source = @app.views
|
|
|
|
@destination = @app.build_dir
|
|
|
|
|
2011-01-30 23:18:49 +01:00
|
|
|
@source = File.expand_path(base.find_in_source_paths(source.to_s))
|
2011-11-08 07:34:02 +01:00
|
|
|
|
|
|
|
super(base, destination, config)
|
2011-01-30 23:18:49 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def invoke!
|
2011-11-08 07:34:02 +01:00
|
|
|
queue_current_paths if cleaning?
|
2011-01-30 23:18:49 +01:00
|
|
|
execute!
|
2011-11-08 07:34:02 +01:00
|
|
|
clean! if cleaning?
|
2011-01-30 23:18:49 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def revoke!
|
|
|
|
execute!
|
|
|
|
end
|
2011-11-08 07:34:02 +01:00
|
|
|
|
2011-09-13 01:15:51 +02:00
|
|
|
protected
|
|
|
|
|
2011-11-08 07:34:02 +01:00
|
|
|
def clean!
|
|
|
|
files = @cleaning_queue.select { |q| File.file? q }
|
|
|
|
directories = @cleaning_queue.select { |q| File.directory? q }
|
2011-01-30 23:18:49 +01:00
|
|
|
|
2011-11-08 07:34:02 +01:00
|
|
|
files.each do |f|
|
|
|
|
base.remove_file f, :force => true
|
|
|
|
end
|
2011-09-13 01:15:51 +02:00
|
|
|
|
2011-11-08 07:34:02 +01:00
|
|
|
directories = directories.sort_by {|d| d.length }.reverse!
|
2011-09-13 01:15:51 +02:00
|
|
|
|
2011-11-08 07:34:02 +01:00
|
|
|
directories.each do |d|
|
|
|
|
base.remove_file d, :force => true if directory_empty? d
|
2011-09-13 01:15:51 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-11-08 07:34:02 +01:00
|
|
|
def cleaning?
|
|
|
|
@config.has_key?(:clean) && @config[:clean]
|
|
|
|
end
|
|
|
|
|
|
|
|
def directory_empty?(directory)
|
|
|
|
Dir["#{directory}/*"].empty?
|
|
|
|
end
|
|
|
|
|
|
|
|
def queue_current_paths
|
|
|
|
@cleaning_queue = []
|
|
|
|
Find.find(@destination) do |path|
|
|
|
|
next if path.match(/\/\./)
|
|
|
|
unless path == destination
|
|
|
|
@cleaning_queue << path.sub(@destination, destination[/([^\/]+?)$/])
|
|
|
|
end
|
|
|
|
end
|
2011-09-13 01:15:51 +02:00
|
|
|
end
|
|
|
|
|
2011-11-08 07:34:02 +01:00
|
|
|
def execute!
|
|
|
|
paths = @app.sitemap.all_paths.sort do |a, b|
|
|
|
|
a_dir = a.split("/").first
|
|
|
|
b_dir = b.split("/").first
|
2011-02-06 19:57:28 +01:00
|
|
|
|
2011-11-08 07:34:02 +01:00
|
|
|
if a_dir == @app.images_dir
|
2011-06-24 21:06:28 +02:00
|
|
|
-1
|
2011-11-08 07:34:02 +01:00
|
|
|
elsif b_dir == @app.images_dir
|
2011-06-24 21:06:28 +02:00
|
|
|
1
|
|
|
|
else
|
|
|
|
0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-11-08 07:34:02 +01:00
|
|
|
paths.each do |path|
|
|
|
|
file_source = path
|
|
|
|
file_destination = File.join(given_destination, file_source.gsub(source, '.'))
|
|
|
|
file_destination.gsub!('/./', '/')
|
|
|
|
|
|
|
|
if @app.sitemap.generic_path?(file_source)
|
|
|
|
# no-op
|
|
|
|
elsif @app.sitemap.proxied_path?(file_source)
|
|
|
|
file_source = @app.sitemap.path_target(file_source)
|
|
|
|
elsif @app.sitemap.ignored_path?(file_source)
|
2011-04-10 23:37:36 +02:00
|
|
|
next
|
|
|
|
end
|
|
|
|
|
2011-11-08 07:34:02 +01:00
|
|
|
@cleaning_queue.delete(file_destination) if cleaning?
|
|
|
|
|
|
|
|
if @config[:glob]
|
|
|
|
next unless File.fnmatch(@config[:glob], file_source)
|
2011-08-06 06:37:33 +02:00
|
|
|
end
|
2011-11-08 07:34:02 +01:00
|
|
|
|
|
|
|
base.tilt_template(file_source, file_destination, { :force => true })
|
2011-08-06 06:37:33 +02:00
|
|
|
end
|
2011-10-14 20:36:46 +02:00
|
|
|
end
|
2009-10-23 02:25:15 +02:00
|
|
|
end
|
2011-01-30 23:18:49 +01:00
|
|
|
end
|