2014-05-29 11:57:30 +02:00
|
|
|
require 'middleman-core/cli'
|
2015-02-23 04:56:53 +01:00
|
|
|
require 'middleman-core/rack' if Middleman::VERSION.to_i > 3
|
2014-05-29 11:57:30 +02:00
|
|
|
require 'middleman-deploy/pkg-info'
|
|
|
|
require 'middleman-deploy/extension'
|
|
|
|
require 'middleman-deploy/methods'
|
|
|
|
require 'middleman-deploy/strategies'
|
2012-08-22 20:45:33 +02:00
|
|
|
|
|
|
|
module Middleman
|
|
|
|
module Cli
|
|
|
|
# This class provides a "deploy" command for the middleman CLI.
|
2015-02-22 22:52:52 +01:00
|
|
|
class Deploy < Thor::Group
|
2012-08-22 20:45:33 +02:00
|
|
|
include Thor::Actions
|
|
|
|
|
|
|
|
check_unknown_options!
|
|
|
|
|
|
|
|
namespace :deploy
|
|
|
|
|
2015-02-22 22:52:52 +01:00
|
|
|
class_option :environment,
|
|
|
|
aliases: '-e',
|
|
|
|
default: ENV['MM_ENV'] || ENV['RACK_ENV'] || 'production',
|
|
|
|
desc: 'The environment Middleman will run under'
|
|
|
|
|
|
|
|
class_option :verbose,
|
|
|
|
type: :boolean,
|
|
|
|
default: false,
|
|
|
|
desc: 'Print debug messages'
|
|
|
|
|
|
|
|
class_option :instrument,
|
|
|
|
type: :string,
|
|
|
|
default: false,
|
|
|
|
desc: 'Print instrument messages'
|
|
|
|
|
|
|
|
class_option :build_before,
|
|
|
|
type: :boolean,
|
|
|
|
aliases: '-b',
|
|
|
|
desc: 'Run `middleman build` before the deploy step'
|
|
|
|
|
|
|
|
def self.subcommand_help options
|
|
|
|
# TODO
|
|
|
|
end
|
|
|
|
|
2012-08-22 20:45:33 +02:00
|
|
|
# Tell Thor to exit with a nonzero exit code on failure
|
|
|
|
def self.exit_on_failure?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2015-02-22 22:52:52 +01:00
|
|
|
|
2012-08-22 20:45:33 +02:00
|
|
|
def deploy
|
2015-02-22 22:52:52 +01:00
|
|
|
env = options['environment'] ? :production : options['environment'].to_s.to_sym
|
|
|
|
verbose = options['verbose'] ? 0 : 1
|
|
|
|
instrument = options['instrument']
|
|
|
|
|
|
|
|
@app = ::Middleman::Application.new do
|
|
|
|
config[:mode] = :build
|
|
|
|
config[:environment] = env
|
|
|
|
::Middleman::Logger.singleton(verbose, instrument)
|
|
|
|
end
|
2014-01-16 15:25:40 +01:00
|
|
|
build_before(options)
|
|
|
|
process
|
2012-08-30 05:50:23 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
2014-05-29 11:57:30 +02:00
|
|
|
def build_before(options = {})
|
2014-01-16 15:25:40 +01:00
|
|
|
build_enabled = options.fetch('build_before', self.deploy_options.build_before)
|
2012-09-04 19:25:21 +02:00
|
|
|
|
2014-01-16 15:25:40 +01:00
|
|
|
if build_enabled
|
|
|
|
# http://forum.middlemanapp.com/t/problem-with-the-build-task-in-an-extension
|
2015-02-22 22:52:52 +01:00
|
|
|
run("middleman build -e #{options['environment']}") || exit(1)
|
2014-01-16 15:25:40 +01:00
|
|
|
end
|
|
|
|
end
|
2013-11-30 21:28:35 +01:00
|
|
|
|
2014-01-16 15:25:40 +01:00
|
|
|
def print_usage_and_die(message)
|
2015-02-22 22:52:52 +01:00
|
|
|
raise StandardError, "ERROR: #{message}\n#{Middleman::Deploy::README}"
|
2014-01-16 15:25:40 +01:00
|
|
|
end
|
2012-11-08 16:53:45 +01:00
|
|
|
|
2013-07-17 04:25:06 +02:00
|
|
|
|
2015-02-22 22:52:52 +01:00
|
|
|
|
|
|
|
def process
|
|
|
|
server_instance = @app
|
|
|
|
camelized_method = self.deploy_options.deploy_method.to_s.split('_').map { |word| word.capitalize}.join
|
2014-01-16 15:25:40 +01:00
|
|
|
method_class_name = "Middleman::Deploy::Methods::#{camelized_method}"
|
|
|
|
method_instance = method_class_name.constantize.new(server_instance, self.deploy_options)
|
2012-08-22 23:09:26 +02:00
|
|
|
|
2014-01-16 15:25:40 +01:00
|
|
|
method_instance.process
|
2013-05-06 02:13:14 +02:00
|
|
|
end
|
|
|
|
|
2012-09-04 19:25:21 +02:00
|
|
|
def deploy_options
|
|
|
|
options = nil
|
|
|
|
|
|
|
|
begin
|
2015-02-22 22:52:52 +01:00
|
|
|
options = ::Middleman::Deploy.options
|
2013-08-24 17:16:22 +02:00
|
|
|
rescue NoMethodError
|
2014-05-29 11:57:30 +02:00
|
|
|
print_usage_and_die 'You need to activate the deploy extension in config.rb.'
|
2012-09-04 19:25:21 +02:00
|
|
|
end
|
|
|
|
|
2015-02-22 22:52:52 +01:00
|
|
|
unless options.deploy_method
|
2014-05-29 11:57:30 +02:00
|
|
|
print_usage_and_die 'The deploy extension requires you to set a method.'
|
2012-08-22 20:45:33 +02:00
|
|
|
end
|
2012-08-22 23:09:26 +02:00
|
|
|
|
2015-02-22 22:52:52 +01:00
|
|
|
case options.deploy_method
|
2013-09-21 16:53:59 +02:00
|
|
|
when :rsync, :sftp
|
2014-01-16 15:25:40 +01:00
|
|
|
unless options.host && options.path
|
2015-02-22 22:52:52 +01:00
|
|
|
print_usage_and_die "The #{options.deploy_method} method requires host and path to be set."
|
2012-09-04 19:25:21 +02:00
|
|
|
end
|
2013-09-12 20:30:09 +02:00
|
|
|
when :ftp
|
2014-01-16 15:25:40 +01:00
|
|
|
unless options.host && options.user && options.password && options.path
|
2014-05-29 11:57:30 +02:00
|
|
|
print_usage_and_die 'The ftp deploy method requires host, path, user, and password to be set.'
|
2013-09-12 20:30:09 +02:00
|
|
|
end
|
2013-01-20 14:51:22 +01:00
|
|
|
end
|
|
|
|
|
2012-09-04 19:25:21 +02:00
|
|
|
options
|
|
|
|
end
|
2012-08-22 20:45:33 +02:00
|
|
|
end
|
|
|
|
|
2015-02-22 22:52:52 +01:00
|
|
|
# Add to CLI
|
|
|
|
Base.register(Middleman::Cli::Deploy, 'deploy', 'deploy [options]', Middleman::Deploy::TAGLINE)
|
|
|
|
|
2012-08-22 20:45:33 +02:00
|
|
|
# Alias "d" to "deploy"
|
2014-05-29 11:57:30 +02:00
|
|
|
Base.map('d' => 'deploy')
|
2012-08-22 20:45:33 +02:00
|
|
|
end
|
|
|
|
end
|