middleman/middleman-core/lib/middleman-core/cli/build.rb

244 lines
6.8 KiB
Ruby
Raw Normal View History

2011-12-29 07:52:51 +01:00
# Use Rack::Test for inspecting a running server for output
require "rack"
2011-11-28 05:49:46 +01:00
require "rack/test"
require 'find'
2011-12-29 07:52:51 +01:00
# CLI Module
2011-12-21 21:13:28 +01:00
module Middleman::Cli
2011-12-29 07:52:51 +01:00
# The CLI Build class
2011-12-21 21:13:28 +01:00
class Build < Thor
2011-01-30 23:18:49 +01:00
include Thor::Actions
2011-12-29 07:52:51 +01:00
check_unknown_options!
2011-12-21 21:13:28 +01:00
namespace :build
desc "build [options]", "Builds the static site for deployment"
method_option :relative,
:type => :boolean,
:aliases => "-r",
:default => false,
:desc => 'Force relative urls'
2011-12-21 21:13:28 +01:00
method_option :clean,
:type => :boolean,
:aliases => "-c",
:default => false,
:desc => 'Removes orpahand files or directories from build'
2011-12-21 21:13:28 +01:00
method_option :glob,
:type => :string,
:aliases => "-g",
:default => nil,
:desc => 'Build a subset of the project'
2011-12-29 07:52:51 +01:00
# Core build Thor command
# @return [void]
def build
if !ENV["MM_ROOT"]
$stderr.puts "== Error: Could not find a Middleman project config, perhaps you are in the wrong folder?"
exit(1)
end
if options.has_key?("relative") && options["relative"]
self.class.shared_instance.activate :relative_assets
end
self.class.shared_rack
opts = {}
opts[:glob] = options["glob"] if options.has_key?("glob")
opts[:clean] = options["clean"] if options.has_key?("clean")
2011-12-29 07:52:51 +01:00
action GlobAction.new(self, opts)
self.class.shared_instance.run_hook :after_build, self
end
2011-01-30 23:18:49 +01:00
2011-12-29 07:52:51 +01:00
# Static methods
2011-11-21 06:21:19 +01:00
class << self
2011-12-29 07:52:51 +01:00
# Middleman::Base singleton
#
# @return [Middleman::Base]
2011-11-28 05:49:46 +01:00
def shared_instance
@_shared_instance ||= ::Middleman.server.inst do
set :environment, :build
end
end
2011-12-29 07:52:51 +01:00
# Middleman::Base class singleton
#
# @return [Middleman::Base]
2011-11-28 05:49:46 +01:00
def shared_server
@_shared_server ||= shared_instance.class
end
2011-12-29 07:52:51 +01:00
# Rack::Test::Session singleton
#
# @return [Rack::Test::Session]
2011-11-21 06:21:19 +01:00
def shared_rack
2011-11-28 05:49:46 +01:00
@_shared_rack ||= begin
mock = ::Rack::MockSession.new(shared_server.to_rack_app)
2011-11-21 06:21:19 +01:00
sess = ::Rack::Test::Session.new(mock)
response = sess.get("__middleman__")
sess
end
2011-07-27 23:14:22 +02:00
end
end
2011-12-29 07:52:51 +01:00
# Set the root path to the Middleman::Base's root
source_root(shared_instance.root)
# Ignore following method
desc "", "", :hide => true
2011-12-29 07:52:51 +01:00
# Render a template to a file.
#
# @param [String] source
# @param [String] destination
# @param [Hash] config
# @return [String] the actual destination file path that was created
def tilt_template(source, destination, config={})
build_dir = self.class.shared_instance.build_dir
request_path = destination.sub(/^#{build_dir}/, "")
config[:force] = true
begin
destination, request_path = self.class.shared_instance.reroute_builder(destination, request_path)
response = self.class.shared_rack.get(request_path.gsub(/\s/, "%20"))
create_file(destination, response.body, config)
destination
rescue
say_status :error, destination, :red
abort
2011-11-28 05:49:46 +01:00
end
end
end
2011-12-29 07:52:51 +01:00
# A Thor Action, modular code, which does the majority of the work.
2011-11-08 07:34:02 +01:00
class GlobAction < ::Thor::Actions::EmptyDirectory
2011-01-30 23:18:49 +01:00
attr_reader :source
2011-12-29 07:52:51 +01:00
# Setup the action
#
# @param [Middleman::Cli::Build] base
# @param [Hash] config
def initialize(base, config={})
@app = base.class.shared_instance
2011-11-24 06:59:53 +01:00
source = @app.source
2011-11-08 07:34:02 +01:00
@destination = @app.build_dir
2011-01-30 23:18:49 +01:00
@source = File.expand_path(base.find_in_source_paths(source.to_s))
2011-12-29 07:52:51 +01:00
super(base, @destination, config)
2011-01-30 23:18:49 +01:00
end
2011-12-29 07:52:51 +01:00
# Execute the action
# @return [void]
2011-01-30 23:18:49 +01:00
def invoke!
2011-11-08 07:34:02 +01:00
queue_current_paths if cleaning?
2011-01-30 23:18:49 +01:00
execute!
2011-11-08 07:34:02 +01:00
clean! if cleaning?
2011-01-30 23:18:49 +01:00
end
2011-09-13 01:15:51 +02:00
protected
2011-12-29 07:52:51 +01:00
# Remove files which were not built in this cycle
# @return [void]
2011-11-08 07:34:02 +01:00
def clean!
files = @cleaning_queue.select { |q| q.file? }
directories = @cleaning_queue.select { |q| q.directory? }
2011-01-30 23:18:49 +01:00
2011-11-08 07:34:02 +01:00
files.each do |f|
base.remove_file f, :force => true
end
2011-09-13 01:15:51 +02:00
directories = directories.sort_by {|d| d.to_s.length }.reverse!
2011-09-13 01:15:51 +02:00
2011-11-08 07:34:02 +01:00
directories.each do |d|
base.remove_file d, :force => true if directory_empty? d
2011-09-13 01:15:51 +02:00
end
end
2011-12-29 07:52:51 +01:00
# Whether we should clean the build
# @return [Boolean]
2011-11-08 07:34:02 +01:00
def cleaning?
@config.has_key?(:clean) && @config[:clean]
end
2011-12-29 07:52:51 +01:00
# Whether the given directory is empty
# @param [String] directory
# @return [Boolean]
2011-11-08 07:34:02 +01:00
def directory_empty?(directory)
directory.children.empty?
2011-11-08 07:34:02 +01:00
end
2011-12-29 07:52:51 +01:00
# Get a list of all the paths in the destination folder and save them
# for comparison against the files we build in this cycle
# @return [void]
2011-11-08 07:34:02 +01:00
def queue_current_paths
@cleaning_queue = []
Find.find(@destination) do |path|
next if path.match(/\/\./) && !path.match(/\.htaccess/)
2011-11-08 07:34:02 +01:00
unless path == destination
@cleaning_queue << Pathname.new(path)
2011-11-08 07:34:02 +01:00
end
end if File.exist?(@destination)
2011-09-13 01:15:51 +02:00
end
2011-12-29 07:52:51 +01:00
# Actually build the app
# @return [void]
2011-11-08 07:34:02 +01:00
def execute!
2011-12-29 07:52:51 +01:00
# Sort order, images, fonts, js/css and finally everything else.
sort_order = %w(.png .jpeg .jpg .gif .bmp .svg .svgz .ico .woff .otf .ttf .eot .js .css)
@app.sitemap.all_paths.select do |p|
File.extname(p) == ".css"
end.each do |p|
Middleman::Cli::Build.shared_rack.get("/" + p.gsub(/\s/, "%20"))
end
# Double-check for compass sprites
@app.files.reload_path(File.join(@app.source_dir, @app.images_dir))
2011-12-29 07:52:51 +01:00
# Sort paths to be built by the above order. This is primarily so Compass can
# find files in the build folder when it needs to generate sprites for the
# css files
paths = @app.sitemap.all_paths.sort do |a, b|
a_ext = File.extname(a)
b_ext = File.extname(b)
a_idx = sort_order.index(a_ext) || 100
b_idx = sort_order.index(b_ext) || 100
a_idx <=> b_idx
end
2011-12-29 07:52:51 +01:00
# Loop over all the paths and build them.
2011-11-08 07:34:02 +01:00
paths.each do |path|
file_source = path
file_destination = File.join(given_destination, file_source.gsub(source, '.'))
file_destination.gsub!('/./', '/')
2011-12-29 07:52:51 +01:00
if @app.sitemap.proxied?(file_source)
file_source = @app.sitemap.page(file_source).proxied_to
elsif @app.sitemap.page(file_source).ignored?
next
end
2011-12-29 07:52:51 +01:00
next if @config[:glob] && !File.fnmatch(@config[:glob], file_source)
2011-12-29 07:52:51 +01:00
file_destination = base.tilt_template(file_source, file_destination)
@cleaning_queue.delete(Pathname.new(file_destination).realpath) if cleaning?
2011-08-06 06:37:33 +02:00
end
2011-10-14 20:36:46 +02:00
end
end
2011-12-29 07:52:51 +01:00
# Alias "b" to "build"
Base.map({ "b" => "build" })
end