2013-12-28 01:26:31 +01:00
|
|
|
require 'fileutils'
|
2013-10-20 02:46:14 +02:00
|
|
|
require 'set'
|
2012-05-25 01:51:36 +02:00
|
|
|
|
2011-12-29 07:52:51 +01:00
|
|
|
# CLI Module
|
2011-12-21 21:13:28 +01:00
|
|
|
module Middleman::Cli
|
2013-10-20 02:46:14 +02:00
|
|
|
# Alias "b" to "build"
|
2014-04-29 19:50:21 +02:00
|
|
|
Base.map('b' => 'build')
|
2012-08-14 00:39:06 +02:00
|
|
|
|
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
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2012-06-05 19:24:00 +02:00
|
|
|
attr_reader :debugging
|
2013-10-20 02:46:14 +02:00
|
|
|
attr_accessor :had_errors
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2011-12-21 20:03:45 +01:00
|
|
|
check_unknown_options!
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2011-12-21 21:13:28 +01:00
|
|
|
namespace :build
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2013-12-28 01:26:31 +01:00
|
|
|
desc 'build [options]', 'Builds the static site for deployment'
|
2014-06-07 00:32:00 +02:00
|
|
|
method_option :environment,
|
|
|
|
aliases: '-e',
|
|
|
|
default: ENV['MM_ENV'] || ENV['RACK_ENV'] || 'development',
|
|
|
|
desc: 'The environment Middleman will run under'
|
2013-04-14 03:58:54 +02:00
|
|
|
method_option :clean,
|
2014-04-29 19:50:21 +02:00
|
|
|
type: :boolean,
|
|
|
|
default: true,
|
|
|
|
desc: 'Remove orphaned files from build (--no-clean to disable)'
|
2012-08-14 00:39:06 +02:00
|
|
|
method_option :glob,
|
2014-04-29 19:50:21 +02:00
|
|
|
type: :string,
|
|
|
|
aliases: '-g',
|
|
|
|
default: nil,
|
|
|
|
desc: 'Build a subset of the project'
|
2012-02-01 06:30:45 +01:00
|
|
|
method_option :verbose,
|
2014-04-29 19:50:21 +02:00
|
|
|
type: :boolean,
|
|
|
|
default: false,
|
|
|
|
desc: 'Print debug messages'
|
2012-07-15 20:04:45 +02:00
|
|
|
method_option :instrument,
|
2014-04-29 19:50:21 +02:00
|
|
|
type: :string,
|
|
|
|
default: false,
|
|
|
|
desc: 'Print instrument messages'
|
2012-07-19 07:10:02 +02:00
|
|
|
method_option :profile,
|
2014-04-29 19:50:21 +02:00
|
|
|
type: :boolean,
|
|
|
|
default: false,
|
|
|
|
desc: 'Generate profiling report for the build'
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2011-12-29 07:52:51 +01:00
|
|
|
# Core build Thor command
|
|
|
|
# @return [void]
|
2011-12-21 20:03:45 +01:00
|
|
|
def build
|
2014-04-29 19:50:21 +02:00
|
|
|
unless ENV['MM_ROOT']
|
2013-12-28 01:26:31 +01:00
|
|
|
raise Thor::Error, 'Error: Could not find a Middleman project config, perhaps you are in the wrong folder?'
|
2011-12-24 20:30:41 +01:00
|
|
|
end
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2014-03-14 04:19:08 +01:00
|
|
|
require 'middleman-core'
|
|
|
|
require 'middleman-core/logger'
|
2014-07-05 21:14:58 +02:00
|
|
|
require 'middleman-core/rack'
|
2014-03-14 04:19:08 +01:00
|
|
|
|
2013-12-28 01:26:31 +01:00
|
|
|
require 'rack'
|
2014-01-03 02:25:31 +01:00
|
|
|
require 'rack/mock'
|
2012-05-24 20:03:58 +02:00
|
|
|
|
|
|
|
require 'find'
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2012-06-21 02:53:14 +02:00
|
|
|
@debugging = Middleman::Cli::Base.respond_to?(:debugging) && Middleman::Cli::Base.debugging
|
2013-10-20 02:46:14 +02:00
|
|
|
self.had_errors = false
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2014-06-07 00:32:00 +02:00
|
|
|
env = options['environment'].to_sym
|
|
|
|
verbose = options['verbose'] ? 0 : 1
|
|
|
|
instrument = options['instrument']
|
|
|
|
|
2014-07-05 21:14:58 +02:00
|
|
|
app = ::Middleman::Application.new do
|
2014-06-07 00:32:00 +02:00
|
|
|
config[:mode] = :build
|
|
|
|
config[:environment] = env
|
|
|
|
::Middleman::Logger.singleton(verbose, instrument)
|
|
|
|
end
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2011-12-21 20:03:45 +01:00
|
|
|
opts = {}
|
2014-04-29 19:50:21 +02:00
|
|
|
opts[:glob] = options['glob'] if options.key?('glob')
|
2013-12-28 01:26:31 +01:00
|
|
|
opts[:clean] = options['clean']
|
2011-12-21 20:03:45 +01:00
|
|
|
|
2014-06-07 00:32:00 +02:00
|
|
|
app.run_hook :before_build, self
|
2014-02-05 06:03:24 +01:00
|
|
|
|
2014-06-07 00:32:00 +02:00
|
|
|
action BuildAction.new(self, app, opts)
|
2011-12-21 20:03:45 +01:00
|
|
|
|
2014-06-07 00:32:00 +02:00
|
|
|
app.run_hook :after_build, self
|
|
|
|
app.config_context.execute_after_build_callbacks(self)
|
2013-05-04 19:57:17 +02:00
|
|
|
|
2014-04-29 19:50:21 +02:00
|
|
|
if had_errors && !debugging
|
2013-12-28 01:26:31 +01:00
|
|
|
msg = 'There were errors during this build'
|
|
|
|
unless options['verbose']
|
|
|
|
msg << ', re-run with `middleman build --verbose` to see the full exception.'
|
2013-05-04 19:57:17 +02:00
|
|
|
end
|
2014-04-29 19:50:21 +02:00
|
|
|
shell.say msg, :red
|
2012-06-05 19:24:00 +02:00
|
|
|
end
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2014-04-29 19:50:21 +02:00
|
|
|
exit(1) if had_errors
|
2011-12-21 20:03:45 +01:00
|
|
|
end
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2011-12-29 07:52:51 +01:00
|
|
|
# Static methods
|
2011-11-21 06:21:19 +01:00
|
|
|
class << self
|
2012-03-19 00:30:25 +01:00
|
|
|
def exit_on_failure?
|
|
|
|
true
|
|
|
|
end
|
2012-01-16 23:55:08 +01:00
|
|
|
end
|
2009-10-23 02:25:15 +02:00
|
|
|
end
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2011-12-29 07:52:51 +01:00
|
|
|
# A Thor Action, modular code, which does the majority of the work.
|
2013-10-20 02:46:14 +02:00
|
|
|
class BuildAction < ::Thor::Actions::EmptyDirectory
|
2011-01-30 23:18:49 +01:00
|
|
|
attr_reader :source
|
2012-07-15 20:04:45 +02:00
|
|
|
attr_reader :logger
|
2011-01-30 23:18:49 +01:00
|
|
|
|
2011-12-29 07:52:51 +01:00
|
|
|
# Setup the action
|
|
|
|
#
|
|
|
|
# @param [Middleman::Cli::Build] base
|
|
|
|
# @param [Hash] config
|
2014-06-07 00:32:00 +02:00
|
|
|
def initialize(base, app, config={})
|
|
|
|
@app = app
|
2013-10-20 02:46:14 +02:00
|
|
|
@source_dir = Pathname(@app.source_dir)
|
2014-01-01 03:21:30 +01:00
|
|
|
@build_dir = Pathname(@app.config[:build_dir])
|
2013-10-20 02:46:14 +02:00
|
|
|
@to_clean = Set.new
|
2011-12-21 20:03:45 +01:00
|
|
|
|
2013-10-20 02:46:14 +02:00
|
|
|
@logger = @app.logger
|
2014-07-05 21:14:58 +02:00
|
|
|
rack_app = ::Middleman::Rack.new(@app).to_app
|
|
|
|
@rack = ::Rack::MockRequest.new(rack_app)
|
2011-12-21 20:03:45 +01:00
|
|
|
|
2013-10-20 02:46:14 +02:00
|
|
|
super(base, @build_dir, config)
|
2011-01-30 23:18:49 +01:00
|
|
|
end
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2011-12-29 07:52:51 +01:00
|
|
|
# Execute the action
|
|
|
|
# @return [void]
|
2011-01-30 23:18:49 +01:00
|
|
|
def invoke!
|
2013-10-20 02:46:14 +02:00
|
|
|
queue_current_paths if should_clean?
|
2011-01-30 23:18:49 +01:00
|
|
|
execute!
|
2013-10-20 02:46:14 +02:00
|
|
|
clean! if should_clean?
|
2011-01-30 23:18:49 +01:00
|
|
|
end
|
|
|
|
|
2014-04-29 19:50:21 +02:00
|
|
|
protected
|
2012-08-14 00:39:06 +02:00
|
|
|
|
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!
|
2013-10-20 02:46:14 +02:00
|
|
|
@to_clean.each do |f|
|
2014-04-29 19:50:21 +02:00
|
|
|
base.remove_file f, force: true
|
2011-11-08 07:34:02 +01:00
|
|
|
end
|
2011-09-13 01:15:51 +02:00
|
|
|
|
2014-04-29 19:50:21 +02:00
|
|
|
Dir[@build_dir.join('**', '*')].select { |d| File.directory?(d) }.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-21 20:03:45 +01:00
|
|
|
|
2011-12-29 07:52:51 +01:00
|
|
|
# Whether we should clean the build
|
|
|
|
# @return [Boolean]
|
2013-10-20 02:46:14 +02:00
|
|
|
def should_clean?
|
|
|
|
@config[:clean]
|
2011-11-08 07:34:02 +01:00
|
|
|
end
|
|
|
|
|
2011-12-29 07:52:51 +01:00
|
|
|
# Whether the given directory is empty
|
2013-10-20 02:46:14 +02:00
|
|
|
# @param [String, Pathname] directory
|
2011-12-29 07:52:51 +01:00
|
|
|
# @return [Boolean]
|
2011-11-08 07:34:02 +01:00
|
|
|
def directory_empty?(directory)
|
2013-10-20 02:46:14 +02:00
|
|
|
Pathname(directory).children.empty?
|
2011-11-08 07:34:02 +01:00
|
|
|
end
|
|
|
|
|
2013-10-20 02:46:14 +02:00
|
|
|
# Get a list of all the file paths in the destination folder and save them
|
2011-12-29 07:52:51 +01:00
|
|
|
# for comparison against the files we build in this cycle
|
|
|
|
# @return [void]
|
2011-11-08 07:34:02 +01:00
|
|
|
def queue_current_paths
|
2013-10-20 02:46:14 +02:00
|
|
|
return unless File.exist?(@build_dir)
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2013-10-20 02:46:14 +02:00
|
|
|
paths = ::Middleman::Util.all_files_under(@build_dir).map(&:realpath).select(&:file?)
|
2013-12-18 23:54:07 +01:00
|
|
|
|
2013-10-20 02:46:14 +02:00
|
|
|
@to_clean += paths.select do |path|
|
2013-02-09 09:20:37 +01:00
|
|
|
path.to_s !~ /\/\./ || path.to_s =~ /\.(htaccess|htpasswd)/
|
2012-08-14 00:39:06 +02:00
|
|
|
end
|
2013-12-11 07:11:59 +01:00
|
|
|
|
2014-07-02 19:11:52 +02:00
|
|
|
return unless RUBY_PLATFORM =~ /darwin/
|
|
|
|
|
|
|
|
# handle UTF-8-MAC filename on MacOS
|
|
|
|
@to_clean = @to_clean.map { |path| path.to_s.encode('UTF-8', 'UTF-8-MAC') }
|
2011-09-13 01:15:51 +02:00
|
|
|
end
|
2011-12-21 20:03:45 +01:00
|
|
|
|
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.
|
2011-11-27 05:53:05 +01:00
|
|
|
sort_order = %w(.png .jpeg .jpg .gif .bmp .svg .svgz .ico .woff .otf .ttf .eot .js .css)
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2012-02-09 08:00:29 +01:00
|
|
|
# Pre-request CSS to give Compass a chance to build sprites
|
2013-12-28 01:26:31 +01:00
|
|
|
logger.debug '== Prerendering CSS'
|
2012-04-23 10:12:52 +02:00
|
|
|
|
2012-04-04 19:26:07 +02:00
|
|
|
@app.sitemap.resources.select do |resource|
|
2013-12-28 01:26:31 +01:00
|
|
|
resource.ext == '.css'
|
2014-04-07 18:38:00 +02:00
|
|
|
end.each(&method(:build_resource))
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2014-04-07 21:43:16 +02:00
|
|
|
logger.debug '== Checking for generated images'
|
2012-04-23 10:12:52 +02:00
|
|
|
|
2014-04-07 21:43:16 +02:00
|
|
|
# Double-check for generated images
|
2014-03-26 00:54:16 +01:00
|
|
|
@app.files.find_new_files((@source_dir + @app.config[:images_dir]).relative_path_from(@app.root_path))
|
2012-09-16 07:24:39 +02:00
|
|
|
@app.sitemap.ensure_resource_list_updated!
|
2011-12-21 20:03:45 +01:00
|
|
|
|
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
|
2012-02-03 09:14:49 +01:00
|
|
|
|
2013-12-28 01:26:31 +01:00
|
|
|
logger.debug '== Building files'
|
2012-04-23 10:12:52 +02:00
|
|
|
|
2012-07-19 10:17:50 +02:00
|
|
|
resources = @app.sitemap.resources.sort_by do |r|
|
|
|
|
sort_order.index(r.ext) || 100
|
2011-06-24 21:06:28 +02:00
|
|
|
end
|
2011-12-21 20:03:45 +01:00
|
|
|
|
2013-10-20 02:46:14 +02:00
|
|
|
if @build_dir.expand_path.relative_path_from(@source_dir).to_s =~ /\A[.\/]+\Z/
|
|
|
|
raise ":build_dir (#{@build_dir}) cannot be a parent of :source_dir (#{@source_dir})"
|
|
|
|
end
|
|
|
|
|
2011-12-29 07:52:51 +01:00
|
|
|
# Loop over all the paths and build them.
|
2014-04-07 18:38:00 +02:00
|
|
|
resources.reject do |resource|
|
|
|
|
resource.ext == '.css'
|
|
|
|
end.each(&method(:build_resource))
|
|
|
|
|
|
|
|
::Middleman::Profiling.report('build')
|
|
|
|
end
|
2011-12-21 20:03:45 +01:00
|
|
|
|
2014-04-07 18:38:00 +02:00
|
|
|
def build_resource(resource)
|
|
|
|
return if @config[:glob] && !File.fnmatch(@config[:glob], resource.destination_path)
|
2011-12-01 07:47:07 +01:00
|
|
|
|
2014-04-07 18:38:00 +02:00
|
|
|
output_path = render_to_file(resource)
|
2013-12-18 23:54:07 +01:00
|
|
|
|
2014-07-02 19:11:52 +02:00
|
|
|
return unless should_clean? && output_path.exist?
|
2014-04-07 18:38:00 +02:00
|
|
|
|
2014-07-02 19:11:52 +02:00
|
|
|
if RUBY_PLATFORM =~ /darwin/
|
|
|
|
# handle UTF-8-MAC filename on MacOS
|
|
|
|
|
|
|
|
@to_clean.delete(output_path.realpath.to_s.encode('UTF-8', 'UTF-8-MAC'))
|
|
|
|
else
|
|
|
|
@to_clean.delete(output_path.realpath)
|
2011-08-06 06:37:33 +02:00
|
|
|
end
|
2011-10-14 20:36:46 +02:00
|
|
|
end
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2013-10-20 02:46:14 +02:00
|
|
|
# Render a resource to a file.
|
|
|
|
#
|
|
|
|
# @param [Middleman::Sitemap::Resource] resource
|
|
|
|
# @return [Pathname] The full path of the file that was written
|
|
|
|
def render_to_file(resource)
|
|
|
|
output_file = @build_dir + resource.destination_path.gsub('%20', ' ')
|
|
|
|
|
|
|
|
if resource.binary?
|
|
|
|
if !output_file.exist?
|
|
|
|
base.say_status :create, output_file, :green
|
|
|
|
elsif FileUtils.compare_file(resource.source_file, output_file)
|
|
|
|
base.say_status :identical, output_file, :blue
|
|
|
|
return output_file
|
|
|
|
else
|
|
|
|
base.say_status :update, output_file, :yellow
|
|
|
|
end
|
|
|
|
|
|
|
|
output_file.dirname.mkpath
|
|
|
|
FileUtils.cp(resource.source_file, output_file)
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
response = @rack.get(URI.escape(resource.request_path))
|
|
|
|
|
|
|
|
if response.status == 200
|
|
|
|
base.create_file(output_file, binary_encode(response.body))
|
|
|
|
else
|
|
|
|
handle_error(output_file, response.body)
|
|
|
|
end
|
|
|
|
rescue => e
|
|
|
|
handle_error(output_file, "#{e}\n#{e.backtrace.join("\n")}", e)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
output_file
|
|
|
|
end
|
|
|
|
|
|
|
|
def handle_error(file_name, response, e=Thor::Error.new(response))
|
|
|
|
base.had_errors = true
|
|
|
|
|
|
|
|
base.say_status :error, file_name, :red
|
|
|
|
if base.debugging
|
|
|
|
raise e
|
2013-12-28 01:26:31 +01:00
|
|
|
elsif base.options['verbose']
|
2013-10-20 02:46:14 +02:00
|
|
|
base.shell.say response, :red
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def binary_encode(string)
|
2014-07-02 19:11:52 +02:00
|
|
|
string.force_encoding('ascii-8bit') if string.respond_to?(:force_encoding)
|
2013-10-20 02:46:14 +02:00
|
|
|
string
|
|
|
|
end
|
|
|
|
end
|
2012-01-04 09:21:44 +01:00
|
|
|
end
|
2012-06-05 19:24:00 +02:00
|
|
|
|
|
|
|
# Quiet down create file
|
|
|
|
class ::Thor::Actions::CreateFile
|
|
|
|
def on_conflict_behavior(&block)
|
2012-06-17 21:11:07 +02:00
|
|
|
if identical?
|
|
|
|
say_status :identical, :blue
|
|
|
|
else
|
2012-06-20 04:40:57 +02:00
|
|
|
say_status :update, :yellow
|
2012-06-17 21:11:07 +02:00
|
|
|
block.call unless pretend?
|
|
|
|
end
|
2012-06-05 19:24:00 +02:00
|
|
|
end
|
2012-07-19 08:59:55 +02:00
|
|
|
end
|