spec fixes, reorg builder

This commit is contained in:
tdreyno 2009-10-08 10:49:23 -07:00
parent 847377653e
commit 2b085b886c
7 changed files with 91 additions and 98 deletions

View file

@ -1,87 +1,10 @@
#!/usr/bin/env ruby
require 'templater'
ENV['MM_ENV'] = "build"
# Require app
require File.join(File.dirname(__FILE__), "..", "lib", "middleman")
require 'middleman/builder'
Middleman::Base.init!
module Generators
extend Templater::Manifold
desc "Build a staticmatic site"
class Builder < Templater::Generator
# Define source and desintation
def self.source_root; Dir.pwd; end
def destination_root; File.join(Dir.pwd, Middleman::Base.build_dir); end
# Override template to ask middleman for the correct extension to output
def self.template(name, *args, &block)
return if args[0].include?('layout')
args.first.split('/').each do |part|
return if part[0,1] == '_'
end
if (args[0] === args[1])
args[1] = args[0].gsub("#{File.basename(Middleman::Base.views)}/", "")
.gsub("#{File.basename(Middleman::Base.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)
if (args[0] === args[1])
args[1] = args[0].gsub("#{File.basename(Middleman::Base.views)}/", "")
.gsub("#{File.basename(Middleman::Base.public)}/", "")
end
super(name, *args, &block)
end
glob! File.basename(Middleman::Base.public), Middleman::Base.supported_formats
glob! File.basename(Middleman::Base.views), Middleman::Base.supported_formats
if Middleman::Base.slickmap?
template :slickmap, "sitemap.html", "sitemap.html"
end
end
add :build, Builder
end
# 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
::Middleman::Builder.render_file(source, destination)
end
def identical?
if ::File.exists?(destination)
return true if File.exists?(source) && File.mtime(source) < File.mtime(destination)
::File.read(destination) == render
else
false
end
end
end
Generators.run_cli(Dir.pwd, 'mm-build', 1, %w(build --force).concat(ARGV))
require 'middleman/builder'
Middleman::Generators.run_cli(Dir.pwd, 'mm-build', 1, %w(build --force).concat(ARGV))

View file

@ -6,7 +6,7 @@ module Generators
desc "Generator for streamlining staticmatic"
class NewSite < Templater::Generator
desc "Creates a new staticmatic scaffold."
desc "Creates a new middleman scaffold."
first_argument :location, :required => true, :desc => "Project location"
option :css_dir, :desc => 'The path to the css files'

View file

@ -55,7 +55,11 @@ module Middleman
# Base case renderer (do nothing), Should be over-ridden
module StaticRender
def render_path(path)
false
if template_exists?(path, :erb)
erb(path.to_sym)
else
false
end
end
end
include StaticRender
@ -113,7 +117,7 @@ module Middleman
end
# Require the features for this project
def self.init!
def self.init!(quiet=false)
# Built-in helpers
require 'middleman/helpers'
helpers Middleman::Helpers
@ -124,7 +128,7 @@ module Middleman
# Check for and evaluate local configuration
local_config = File.join(self.root, "init.rb")
if File.exists? local_config
puts "== Local config at: #{local_config}"
puts "== Local config at: #{local_config}" unless quiet
class_eval File.read(local_config)
end

View file

@ -1,15 +1,79 @@
# Use Rack::Test to access Sinatra without starting up a full server
require 'rack/test'
require 'templater'
require 'rack/test' # Use Rack::Test to access Sinatra without starting up a full server
# Placeholder for any methods the builder needs to abstract to allow feature integration
module Middleman
class Builder
# The default render just requests the page over Rack and writes the response
def self.render_file(source, destination)
request_path = destination.gsub(File.join(Dir.pwd, Middleman::Base.build_dir), "")
browser = Rack::Test::Session.new(Rack::MockSession.new(Middleman::Base))
browser.get(request_path)
browser.last_response.body
class Builder < ::Templater::Generator
# Define source and desintation
def self.source_root; Dir.pwd; end
def destination_root; File.join(Dir.pwd, Middleman::Base.build_dir); end
# Override template to ask middleman for the correct extension to output
def self.template(name, *args, &block)
return if args[0].include?('layout')
args.first.split('/').each do |part|
return if part[0,1] == '_'
end
if (args[0] === args[1])
args[1] = args[0].gsub("#{File.basename(Middleman::Base.views)}/", "")
.gsub("#{File.basename(Middleman::Base.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)
if (args[0] === args[1])
args[1] = args[0].gsub("#{File.basename(Middleman::Base.views)}/", "")
.gsub("#{File.basename(Middleman::Base.public)}/", "")
end
super(name, *args, &block)
end
glob! File.basename(Middleman::Base.public), Middleman::Base.supported_formats
glob! File.basename(Middleman::Base.views), Middleman::Base.supported_formats
end
module Generators
extend ::Templater::Manifold
desc "Build a static site"
add :build, ::Middleman::Builder
end
end
# 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
# The default render just requests the page over Rack and writes the response
request_path = destination.gsub(File.join(Dir.pwd, Middleman::Base.build_dir), "")
browser = Rack::Test::Session.new(Rack::MockSession.new(Middleman::Base))
browser.get(request_path)
browser.last_response.body
end
def identical?
if File.exists?(destination)
return true if File.exists?(source) && File.mtime(source) < File.mtime(destination)
File.read(destination) == render
else
false
end
end
end

View file

@ -5,12 +5,14 @@ rescue LoadError
puts "Slickmap not available. Install it with: gem install compass-slickmap"
end
if Middleman::Base.environment == "build"
Middleman::Builder.template :slickmap, "sitemap.html", "sitemap.html"
end
Entry = Struct.new(:dir, :children)
class Middleman::Base
def build_sitemap(&block)
html_files = Dir[File.join(File.dirname(Middleman::Base.views), "**", "*.html*")]
def build_sitemap(&block)
@@utility = []
[recurse_sitemap(Middleman::Base.views, &block), @@utility]
end

View file

@ -6,7 +6,7 @@ base.set :root, File.join(File.dirname(__FILE__), "fixtures", "sample")
describe "Cache Buster Feature" do
before do
base.disable :cache_buster
base.init!
base.init!(true)
@app = base.new
end
@ -18,7 +18,7 @@ end
describe "Cache Buster Feature" do
before do
base.enable :cache_buster
base.init!
base.init!(true)
@app = base.new
end

View file

@ -1,2 +1,2 @@
# enable :maruku
#enable :markaby
# enable :markaby