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

132 lines
4.0 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
2015-01-04 21:23:35 +01:00
class Build < Thor::Group
2011-01-30 23:18:49 +01:00
include Thor::Actions
check_unknown_options!
2015-01-04 21:23:35 +01:00
class_option :environment,
aliases: '-e',
default: ENV['MM_ENV'] || ENV['RACK_ENV'] || 'production',
desc: 'The environment Middleman will run under'
class_option :clean,
type: :boolean,
default: true,
desc: 'Remove orphaned files from build (--no-clean to disable)'
class_option :glob,
type: :string,
aliases: '-g',
default: nil,
desc: 'Build a subset of the project'
class_option :verbose,
type: :boolean,
default: false,
desc: 'Print debug messages'
class_option :instrument,
type: :string,
default: false,
desc: 'Print instrument messages'
class_option :profile,
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']
2016-01-14 02:16:36 +01:00
builder = nil
2016-01-14 02:16:36 +01:00
::Middleman::Logger.singleton(verbose, instrument)
2016-01-14 20:21:42 +01:00
::Middleman::Util.instrument 'builder_setup' do
2016-01-14 02:16:36 +01:00
@app = ::Middleman::Application.new do
config[:mode] = :build
config[:environment] = env
config[:show_exceptions] = false
end
2016-01-14 02:16:36 +01:00
builder = Middleman::Builder.new(@app,
glob: options['glob'],
clean: options['clean'],
parallel: options['parallel'])
builder.thor = self
builder.on_build_event(&method(:on_event))
end
2016-01-14 20:21:42 +01:00
::Middleman::Util.instrument 'builder_run' do
2016-01-14 02:16:36 +01:00
if builder.run!
clean_directories! if options['clean']
shell.say 'Project built successfully.'
else
msg = 'There were errors during this build'
unless options['verbose']
msg << ', re-run with `middleman build --verbose` to see the full exception.'
end
shell.say msg, :red
exit(1)
end
end
2011-09-13 01:15:51 +02:00
end
2015-01-04 21:23:35 +01:00
protected
2015-01-04 21:23:35 +01:00
# 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
say_status event_type, extra, :blue
end
2015-01-04 21:23:35 +01:00
end
2015-01-04 21:23:35 +01: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], '**', '*')
2015-01-04 21:23:35 +01:00
empty_directories = Dir[all_build_files].select do |d|
File.directory?(d)
end
2015-01-04 21:23:35 +01:00
empty_directories.each do |d|
remove_file d, force: true if Pathname(d).children.empty?
end
end
2015-01-04 21:23:35 +01:00
# Add to CLI
Base.register(self, 'build', 'build [options]', 'Builds the static site for deployment')
# Map "b" to "build"
Base.map('b' => 'build')
end
2012-07-19 08:59:55 +02:00
end