middleman/bin/mm-build
2009-09-18 09:59:07 -07:00

120 lines
3.4 KiB
Ruby
Executable file

#!/usr/bin/env ruby
# Require app
require 'templater'
require "yui/compressor"
require "sprockets"
MIDDLEMAN_BUILDER = true
require File.join(File.dirname(__FILE__), '..', 'lib', 'middleman')
require File.join(File.dirname(__FILE__), '..', 'vendor', 'rack-test', 'lib', 'rack', 'test')
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, 'build'); end
# Override template to ask middleman for the correct extension to output
def self.template(name, *args, &block)
return if args.first.include?('layout')
args.first.split('/').each do |part|
return if part[0,1] == '_'
end
if (args[0] === args[1])
file_name_parts = File.basename(args.first).split('.')
if file_name_parts.length > 2
# static ext embedded in filename
newext = ""
else
# use defaults
newext = case file_name_parts.last
when 'haml', 'erb', 'mab', 'maruku'
'.html'
when 'sass'
'.css'
end
end
args[1] = args[0].gsub(".#{file_name_parts.last}", newext).gsub('views/', '')
end
super(name, *args, &block)
end
def self.file(name, *args, &block)
args[1] = args[0].gsub('views/', '') if (args[0] === args[1])
super(name, *args, &block)
end
public_files_glob = File.join(source_root, "public", '**/*')
Dir[public_files_glob].each do |action|
next if File.directory?(action)
action = action.sub("#{source_root}/", '')
template_sym = action.downcase.gsub(/[^a-z0-9]+/, '_').to_sym
if File.extname(action) == '.js' && !action.include?('min')
template(template_sym, action, action.gsub('public/', ''))
else
file(template_sym, action, action.gsub('public/', ''))
end
end
glob! "views", (Middleman.supported_formats << "sass")
end
add :build, Builder
end
class BuildConfig
def self.render(source, destination)
renderer.render(source, destination)
end
def self.renderer
@@renderer ||= BuildRenderer
end
def self.renderer=(val)
@@renderer = val
end
end
# Monkey-patch to use a dynamic renderer
class Templater::Actions::Template
def render
BuildConfig.render(source, destination)
end
end
# Default render through middleman
class BuildRenderer
def self.render(source, destination)
request_path = destination.gsub(File.join(Dir.pwd, 'build'), "")
browser = Rack::Test::Session.new(Rack::MockSession.new(Middleman))
browser.get(request_path)
browser.last_response.body
end
end
class SprocketsRenderer < BuildRenderer
def self.render(source, destination)
if File.extname(source) == '.js'
secretary = Sprockets::Secretary.new( :asset_root => "public",
:load_path => ["public/assets/javascripts/**/*.js"],
:source_files => [source] )
compressor = YUI::JavaScriptCompressor.new(:munge => true)
compressor.compress(secretary.concatenation.to_s)
else
super
end
end
end
BuildConfig.renderer = SprocketsRenderer
Generators.run_cli(Dir.pwd, 'mm-build', 1, %w(build --force).concat(ARGV))