1
0
Fork 0
mirror of https://github.com/middleman/middleman.git synced 2022-11-09 12:20:27 -05:00
middleman--middleman/lib/middleman/builder.rb

178 lines
4.4 KiB
Ruby
Raw Normal View History

2011-01-30 17:18:49 -05:00
require "thor"
require "thor/group"
require 'rack/test'
2011-10-14 14:36:46 -04:00
require 'find'
2011-11-08 18:10:53 -05:00
require 'hooks'
SHARED_SERVER = Middleman.server
SHARED_SERVER.set :environment, :build
2011-09-16 13:16:23 -04:00
module Middleman
2011-01-30 17:18:49 -05:00
module ThorActions
def tilt_template(source, *args, &block)
config = args.last.is_a?(Hash) ? args.pop : {}
destination = args.first || source
2011-07-27 17:14:22 -04:00
request_path = destination.sub(/^#{SHARED_SERVER.build_dir}/, "")
2011-10-14 16:13:21 -04:00
begin
destination, request_path = SHARED_SERVER.reroute_builder(destination, request_path)
2011-08-30 17:09:13 -04:00
request_path.gsub!(/\s/, "%20")
response = Middleman::Builder.shared_rack.get(request_path)
2011-10-15 21:24:19 -04:00
create_file destination, nil, config do
2011-08-30 17:09:13 -04:00
response.body
end if response.status == 200
rescue
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-11-08 18:10:53 -05:00
include ::Hooks
define_hook :after_build
2011-01-30 17:18:49 -05:00
2011-07-27 17:14:22 -04:00
def self.shared_rack
2011-09-17 19:49:14 -04:00
@shared_rack ||= begin
2011-07-27 17:14:22 -04:00
mock = ::Rack::MockSession.new(SHARED_SERVER)
sess = ::Rack::Test::Session.new(mock)
2011-09-17 19:49:14 -04:00
response = sess.get("__middleman__")
2011-07-27 17:14:22 -04: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-12 19:15:51 -04:00
class_option :glob, :type => :string, :aliases => "-g", :default => nil, :desc => 'Build a subset of the project'
2011-01-30 17:18:49 -05:00
def initialize(*args)
super
if options.has_key?("relative") && options["relative"]
SHARED_SERVER.activate :relative_assets
end
end
2011-01-30 17:18:49 -05:00
def source_paths
2011-04-20 16:16:12 -04:00
@source_paths ||= [
SHARED_SERVER.root
2011-01-30 17:18:49 -05:00
]
end
def build_all_files
2011-07-27 17:14:22 -04:00
self.class.shared_rack
2011-11-08 01:34:02 -05:00
opts = { }
opts[:glob] = options["glob"] if options.has_key?("glob")
opts[:clean] = options["clean"] if options.has_key?("clean")
2011-11-08 01:34:02 -05:00
action GlobAction.new(self, SHARED_SERVER, opts)
2011-11-08 18:10:53 -05:00
run_hook :after_build
end
2011-01-30 18:11:54 -05:00
2011-11-08 18:10:53 -05:00
# Old API
2011-01-30 18:11:54 -05:00
def self.after_run(name, &block)
2011-11-08 18:10:53 -05:00
after_build(&block)
2011-01-30 18:11:54 -05:00
end
end
2011-11-08 01:34:02 -05:00
class GlobAction < ::Thor::Actions::EmptyDirectory
2011-01-30 17:18:49 -05:00
attr_reader :source
2011-11-08 01:34:02 -05:00
def initialize(base, app, config={}, &block)
@app = app
source = @app.views
@destination = @app.build_dir
2011-01-30 17:18:49 -05:00
@source = File.expand_path(base.find_in_source_paths(source.to_s))
2011-11-08 01:34:02 -05:00
super(base, destination, config)
2011-01-30 17:18:49 -05:00
end
def invoke!
2011-11-08 01:34:02 -05:00
queue_current_paths if cleaning?
2011-01-30 17:18:49 -05:00
execute!
2011-11-08 01:34:02 -05:00
clean! if cleaning?
2011-01-30 17:18:49 -05:00
end
def revoke!
execute!
end
2011-11-08 01:34:02 -05:00
2011-09-12 19:15:51 -04:00
protected
2011-11-08 01:34:02 -05:00
def clean!
files = @cleaning_queue.select { |q| File.file? q }
directories = @cleaning_queue.select { |q| File.directory? q }
2011-01-30 17:18:49 -05:00
2011-11-08 01:34:02 -05:00
files.each do |f|
base.remove_file f, :force => true
end
2011-09-12 19:15:51 -04:00
2011-11-08 01:34:02 -05:00
directories = directories.sort_by {|d| d.length }.reverse!
2011-09-12 19:15:51 -04:00
2011-11-08 01:34:02 -05:00
directories.each do |d|
base.remove_file d, :force => true if directory_empty? d
2011-09-12 19:15:51 -04:00
end
end
2011-11-08 01:34:02 -05: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-12 19:15:51 -04:00
end
2011-11-08 01:34:02 -05:00
def execute!
paths = @app.sitemap.all_paths.sort do |a, b|
a_dir = a.split("/").first
b_dir = b.split("/").first
2011-11-08 01:34:02 -05:00
if a_dir == @app.images_dir
-1
2011-11-08 01:34:02 -05:00
elsif b_dir == @app.images_dir
1
else
0
end
end
2011-11-08 01:34:02 -05: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)
next
end
2011-11-08 01:34:02 -05:00
@cleaning_queue.delete(file_destination) if cleaning?
if @config[:glob]
next unless File.fnmatch(@config[:glob], file_source)
2011-08-06 00:37:33 -04:00
end
2011-11-08 01:34:02 -05:00
base.tilt_template(file_source, file_destination, { :force => true })
2011-08-06 00:37:33 -04:00
end
2011-10-14 14:36:46 -04:00
end
end
2011-01-30 17:18:49 -05:00
end