thor-based builder

This commit is contained in:
tdreyno 2011-01-30 14:18:49 -08:00
parent 0b23bb28d4
commit b18584cfd4
5 changed files with 91 additions and 83 deletions

View file

@ -4,12 +4,10 @@ ENV['MM_ENV'] = "build"
# Require app
require File.join(File.dirname(__FILE__), "..", "lib", "middleman")
require 'middleman/builder'
require 'middleman/thor_builder'
# Initialize server once so features are loaded
Middleman::Server.new
# Middleman::Server.init!
Middleman::Builder.init!
Middleman::Generators.run_cli(Dir.pwd, 'mm-build', 1, %w(build --force).concat(ARGV))
# Middleman::ThorBuilder.source_root(Dir.pwd)
Middleman::ThorBuilder.start

View file

@ -1,59 +1,95 @@
require 'middleman/server'
require 'templater'
require 'middleman/templater+dynamic_renderer.rb'
require "thor"
require "thor/group"
require 'rack/test'
# Placeholder for any methods the builder needs to abstract to allow feature integration
module Middleman
class Builder < ::Templater::Generator
# Define source and desintation
def self.source_root; Dir.pwd; end
def destination_root; File.join(Dir.pwd, Middleman::Server.build_dir); end
module Middleman
module ThorActions
def tilt_template(source, *args, &block)
config = args.last.is_a?(Hash) ? args.pop : {}
destination = args.first || source
# Override template to ask middleman for the correct extension to output
def self.template(name, *args, &block)
return if args[0].include?('layout')
source = File.expand_path(find_in_source_paths(source.to_s))
context = instance_eval('binding')
args.first.split('/').each do |part|
return if part[0,1] == '_'
@@rack_test ||= Rack::Test::Session.new(Rack::MockSession.new(Middleman::Server))
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
if (args[0] === args[1])
args[1] = args[0].gsub("#{File.basename(Middleman::Server.views)}/", "").gsub("#{File.basename(Middleman::Server.public)}/", "")
if File.extname(args[1]) != ".js"
args[1] = args[1].gsub!(File.extname(args[1]), "") if File.basename(args[1]).split('.').length > 2
end
end
super(name, *args, &block)
end
def self.file(name, *args, &block)
file_ext = File.extname(args[0])
return unless ::Tilt[file_ext].nil?
if (args[0] === args[1])
args[1] = args[0].gsub("#{File.basename(Middleman::Server.views)}/", "").gsub("#{File.basename(Middleman::Server.public)}/", "")
end
super(name, *args, &block)
end
def self.init!
# Support all Tilt-enabled templates and treat js like a template
@@template_extensions ||= ::Tilt.mappings.keys << "js"
glob! File.basename(Middleman::Server.public), @@template_extensions
glob! File.basename(Middleman::Server.views), @@template_extensions
end
def after_run
end
end
module Generators
extend ::Templater::Manifold
desc "Build a static site"
add :build, ::Middleman::Builder
class ThorBuilder < Thor::Group
include Thor::Actions
include Middleman::ThorActions
def initialize(*args)
::Tilt.mappings.keys << "js"
super
end
def source_paths
[
Middleman::Server.public,
Middleman::Server.views
]
end
def build_static_files
action Directory.new(self, Middleman::Server.public, Middleman::Server.build_dir)
end
def build_dynamic_files
action Directory.new(self, Middleman::Server.views, Middleman::Server.build_dir)
end
end
end
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
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?
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_destination.gsub!(file_extension, "")
destination = base.tilt_template(file_source, file_destination, config, &@block)
else
destination = base.copy_file(file_source, file_destination, config, &@block)
end
end
end
end
end

View file

@ -1,26 +0,0 @@
require 'rack/test' # Use Rack::Test to access Sinatra without starting up a full server
# Monkey-patch to use a dynamic renderer
class Templater::Actions::File
def identical?
if exists?
return true if File.mtime(source) < File.mtime(destination)
FileUtils.identical?(source, destination)
else
false
end
end
end
class Templater::Actions::Template
def render
@@rack_test ||= Rack::Test::Session.new(Rack::MockSession.new(Middleman::Server))
@render_cache ||= begin
# The default render just requests the page over Rack and writes the response
request_path = destination.gsub(File.join(Dir.pwd, Middleman::Server.build_dir), "")
@@rack_test.get(request_path)
@@rack_test.last_response.body
end
end
end

View file

@ -1,3 +1,3 @@
module Middleman
VERSION = "1.1.0.beta.0"
VERSION = "1.1.0.beta.1"
end

View file

@ -21,7 +21,7 @@ Gem::Specification.new do |s|
s.add_runtime_dependency("rack", ["~> 1.0"])
s.add_runtime_dependency("thin", ["~> 1.2.0"])
s.add_runtime_dependency("shotgun", ["~> 0.8.0"])
s.add_runtime_dependency("templater", ["~> 1.0.0"])
s.add_runtime_dependency("thor", ["~> 0.14.0"])
s.add_runtime_dependency("tilt", ["~> 1.1"])
s.add_runtime_dependency("sinatra", ["~> 1.0"])
s.add_runtime_dependency("padrino-core", ["~> 0.9.0"])