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

105 lines
3 KiB
Ruby
Raw Normal View History

require 'middleman/server'
2011-01-30 17:18:49 -05:00
require "thor"
require "thor/group"
require 'rack/test'
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-01-30 17:18:49 -05:00
source = File.expand_path(find_in_source_paths(source.to_s))
context = instance_eval('binding')
2011-01-30 18:11:54 -05:00
@@rack_test ||= ::Rack::Test::Session.new(::Rack::MockSession.new(Middleman::Server))
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
request_path = destination.gsub(Middleman::Server.build_dir, "")
@@rack_test.get(request_path)
@@rack_test.last_response.body
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
def initialize(*args)
2011-01-30 18:11:54 -05:00
Middleman::Server.new
2011-01-30 17:18:49 -05:00
super
end
2011-01-30 17:18:49 -05:00
def source_paths
[
Middleman::Server.public,
Middleman::Server.views
]
end
2011-01-30 17:18:49 -05:00
def build_static_files
action Directory.new(self, Middleman::Server.public, Middleman::Server.build_dir, { :force => true })
2011-01-30 17:18:49 -05:00
end
def build_dynamic_files
action Directory.new(self, Middleman::Server.views, Middleman::Server.build_dir, { :force => true })
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
end
2011-01-30 17:18:49 -05:00
class Directory < ::Thor::Actions::EmptyDirectory
attr_reader :source
def initialize(base, source, destination=nil, config={}, &block)
@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
protected
2011-01-30 17:18:49 -05:00
def execute!
lookup = config[:recursive] ? File.join(source, '**') : source
lookup = File.join(lookup, '{*,.[a-z]*}')
Dir[lookup].sort.each do |file_source|
next if File.directory?(file_source)
next if file_source.include?('layout')
next unless file_source.split('/').select { |p| p[0,1] == '_' }.empty?
2011-01-30 17:18:49 -05: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.keys.include?(file_extension.gsub(/^\./, ""))
if handled_by_tilt || (file_extension == ".js")
file_destination.gsub!(file_extension, "") unless file_extension == ".js"
destination = base.tilt_template(file_source, file_destination, config, &@block)
else
destination = base.copy_file(file_source, file_destination, config, &@block)
2011-01-30 17:18:49 -05:00
end
end
end
end
2011-01-30 17:18:49 -05:00
end