spec fixes, reorg builder
This commit is contained in:
parent
847377653e
commit
2b085b886c
7 changed files with 91 additions and 98 deletions
81
bin/mm-build
81
bin/mm-build
|
@ -1,87 +1,10 @@
|
||||||
#!/usr/bin/env ruby
|
#!/usr/bin/env ruby
|
||||||
|
|
||||||
require 'templater'
|
|
||||||
|
|
||||||
ENV['MM_ENV'] = "build"
|
ENV['MM_ENV'] = "build"
|
||||||
|
|
||||||
# Require app
|
# Require app
|
||||||
require File.join(File.dirname(__FILE__), "..", "lib", "middleman")
|
require File.join(File.dirname(__FILE__), "..", "lib", "middleman")
|
||||||
require 'middleman/builder'
|
|
||||||
|
|
||||||
Middleman::Base.init!
|
Middleman::Base.init!
|
||||||
|
|
||||||
module Generators
|
require 'middleman/builder'
|
||||||
extend Templater::Manifold
|
Middleman::Generators.run_cli(Dir.pwd, 'mm-build', 1, %w(build --force).concat(ARGV))
|
||||||
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))
|
|
|
@ -6,7 +6,7 @@ module Generators
|
||||||
desc "Generator for streamlining staticmatic"
|
desc "Generator for streamlining staticmatic"
|
||||||
|
|
||||||
class NewSite < Templater::Generator
|
class NewSite < Templater::Generator
|
||||||
desc "Creates a new staticmatic scaffold."
|
desc "Creates a new middleman scaffold."
|
||||||
first_argument :location, :required => true, :desc => "Project location"
|
first_argument :location, :required => true, :desc => "Project location"
|
||||||
|
|
||||||
option :css_dir, :desc => 'The path to the css files'
|
option :css_dir, :desc => 'The path to the css files'
|
||||||
|
|
|
@ -55,7 +55,11 @@ module Middleman
|
||||||
# Base case renderer (do nothing), Should be over-ridden
|
# Base case renderer (do nothing), Should be over-ridden
|
||||||
module StaticRender
|
module StaticRender
|
||||||
def render_path(path)
|
def render_path(path)
|
||||||
false
|
if template_exists?(path, :erb)
|
||||||
|
erb(path.to_sym)
|
||||||
|
else
|
||||||
|
false
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
include StaticRender
|
include StaticRender
|
||||||
|
@ -113,7 +117,7 @@ module Middleman
|
||||||
end
|
end
|
||||||
|
|
||||||
# Require the features for this project
|
# Require the features for this project
|
||||||
def self.init!
|
def self.init!(quiet=false)
|
||||||
# Built-in helpers
|
# Built-in helpers
|
||||||
require 'middleman/helpers'
|
require 'middleman/helpers'
|
||||||
helpers Middleman::Helpers
|
helpers Middleman::Helpers
|
||||||
|
@ -124,7 +128,7 @@ module Middleman
|
||||||
# Check for and evaluate local configuration
|
# Check for and evaluate local configuration
|
||||||
local_config = File.join(self.root, "init.rb")
|
local_config = File.join(self.root, "init.rb")
|
||||||
if File.exists? local_config
|
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)
|
class_eval File.read(local_config)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1,15 +1,79 @@
|
||||||
# Use Rack::Test to access Sinatra without starting up a full server
|
require 'templater'
|
||||||
require 'rack/test'
|
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
|
# Placeholder for any methods the builder needs to abstract to allow feature integration
|
||||||
module Middleman
|
module Middleman
|
||||||
class Builder
|
class Builder < ::Templater::Generator
|
||||||
# The default render just requests the page over Rack and writes the response
|
# Define source and desintation
|
||||||
def self.render_file(source, destination)
|
def self.source_root; Dir.pwd; end
|
||||||
request_path = destination.gsub(File.join(Dir.pwd, Middleman::Base.build_dir), "")
|
def destination_root; File.join(Dir.pwd, Middleman::Base.build_dir); end
|
||||||
browser = Rack::Test::Session.new(Rack::MockSession.new(Middleman::Base))
|
|
||||||
browser.get(request_path)
|
# Override template to ask middleman for the correct extension to output
|
||||||
browser.last_response.body
|
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
|
end
|
||||||
end
|
end
|
|
@ -5,12 +5,14 @@ rescue LoadError
|
||||||
puts "Slickmap not available. Install it with: gem install compass-slickmap"
|
puts "Slickmap not available. Install it with: gem install compass-slickmap"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if Middleman::Base.environment == "build"
|
||||||
|
Middleman::Builder.template :slickmap, "sitemap.html", "sitemap.html"
|
||||||
|
end
|
||||||
|
|
||||||
Entry = Struct.new(:dir, :children)
|
Entry = Struct.new(:dir, :children)
|
||||||
|
|
||||||
class Middleman::Base
|
class Middleman::Base
|
||||||
def build_sitemap(&block)
|
def build_sitemap(&block)
|
||||||
html_files = Dir[File.join(File.dirname(Middleman::Base.views), "**", "*.html*")]
|
|
||||||
|
|
||||||
@@utility = []
|
@@utility = []
|
||||||
[recurse_sitemap(Middleman::Base.views, &block), @@utility]
|
[recurse_sitemap(Middleman::Base.views, &block), @@utility]
|
||||||
end
|
end
|
||||||
|
|
|
@ -6,7 +6,7 @@ base.set :root, File.join(File.dirname(__FILE__), "fixtures", "sample")
|
||||||
describe "Cache Buster Feature" do
|
describe "Cache Buster Feature" do
|
||||||
before do
|
before do
|
||||||
base.disable :cache_buster
|
base.disable :cache_buster
|
||||||
base.init!
|
base.init!(true)
|
||||||
@app = base.new
|
@app = base.new
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -18,7 +18,7 @@ end
|
||||||
describe "Cache Buster Feature" do
|
describe "Cache Buster Feature" do
|
||||||
before do
|
before do
|
||||||
base.enable :cache_buster
|
base.enable :cache_buster
|
||||||
base.init!
|
base.init!(true)
|
||||||
@app = base.new
|
@app = base.new
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
2
spec/fixtures/sample/init.rb
vendored
2
spec/fixtures/sample/init.rb
vendored
|
@ -1,2 +1,2 @@
|
||||||
# enable :maruku
|
# enable :maruku
|
||||||
#enable :markaby
|
# enable :markaby
|
Loading…
Reference in a new issue