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

130 lines
3.9 KiB
Ruby
Raw Normal View History

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
check_unknown_options!
2011-12-21 21:13:28 +01:00
namespace :build
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'
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)'
method_option :glob,
2014-04-29 19:50:21 +02:00
type: :string,
aliases: '-g',
default: nil,
desc: 'Build a subset of the project'
method_option :verbose,
2014-04-29 19:50:21 +02:00
type: :boolean,
default: false,
desc: 'Print debug messages'
method_option :instrument,
2014-04-29 19:50:21 +02:00
type: :string,
default: false,
desc: 'Print instrument messages'
method_option :profile,
2014-04-29 19:50:21 +02:00
type: :boolean,
default: false,
desc: 'Generate profiling report for the build'
2011-12-29 07:52:51 +01:00
# Core build Thor command
# @return [void]
def build
2014-04-29 19:50:21 +02:00
unless ENV['MM_ROOT']
raise Thor::Error, 'Error: Could not find a Middleman project config, perhaps you are in the wrong folder?'
end
require 'middleman-core'
require 'middleman-core/logger'
2014-07-08 04:43:22 +02:00
require 'middleman-core/builder'
require 'fileutils'
2014-06-07 00:32:00 +02:00
env = options['environment'].to_sym
verbose = options['verbose'] ? 0 : 1
instrument = options['instrument']
2014-07-08 04:43:22 +02:00
@app = ::Middleman::Application.new do
2014-06-07 00:32:00 +02:00
config[:mode] = :build
config[:environment] = env
2014-07-08 04:43:22 +02:00
config[:show_exceptions] = false
2014-06-07 00:32:00 +02:00
::Middleman::Logger.singleton(verbose, instrument)
end
2014-07-08 04:43:22 +02:00
builder = Middleman::Builder.new(@app,
glob: options['glob'],
clean: options['clean'],
parallel: options['parallel'])
2014-07-08 04:43:22 +02:00
builder.on_build_event(&method(:on_event))
2014-07-08 04:43:22 +02:00
if builder.run!
clean_directories! if options['clean']
else
msg = 'There were errors during this build'
unless options['verbose']
msg << ', re-run with `middleman build --verbose` to see the full exception.'
end
2014-04-29 19:50:21 +02:00
shell.say msg, :red
2014-07-08 04:43:22 +02:00
exit(1)
end
2011-09-13 01:15:51 +02:00
end
2014-07-08 04:43:22 +02:00
# Tell Thor to send an exit status on a failure.
def self.exit_on_failure?
true
2014-04-07 18:38:00 +02:00
end
2014-07-08 04:43:22 +02:00
no_tasks do
# Handles incoming events from the builder.
# @param [Symbol] event_type The type of event.
# @param [String] contents The event contents.
# @param [String] extra The extra information.
# @return [void]
def on_event(event_type, target, extra=nil)
case event_type
when :error
say_status :error, target, :red
shell.say extra, :red if options['verbose']
when :deleted
say_status :remove, target, :green
when :created
say_status :create, target, :green
when :identical
say_status :identical, target, :blue
when :updated
say_status :updated, target, :yellow
else
2014-07-08 04:43:22 +02:00
say_status event_type, extra, :blue
end
end
2014-07-08 04:43:22 +02:00
# Find empty directories in the build folder and remove them.
# @return [Boolean]
def clean_directories!
all_build_files = File.join(@app.config[:build_dir], '**', '*')
2014-07-08 04:43:22 +02:00
empty_directories = Dir[all_build_files].select do |d|
File.directory?(d)
end
2014-07-08 04:43:22 +02:00
empty_directories.each do |d|
remove_file d, force: true if Pathname(d).children.empty?
end
end
end
end
2014-07-08 04:43:22 +02:00
# Alias "b" to "build"
Base.map('b' => 'build')
2012-07-19 08:59:55 +02:00
end