master
Karl Freeman 2015-08-16 19:06:10 +01:00
parent 751c11f6eb
commit 86f76b2a53
11 changed files with 58 additions and 92 deletions

View File

@ -1,24 +1,11 @@
AllCops: AllCops:
Include: Include:
- Rakefile - 'Gemfile'
- Gemfile
Exclude: Exclude:
- script/**/* - 'script/**/*'
- vendor/**/* - 'vendor/**/*'
- bin/**/* - 'bin/**/*'
LineLength:
Enabled: false
MethodLength:
Enabled: false
ClassLength:
Enabled: false
Documentation: Documentation:
Enabled: false Enabled: false
Encoding: ClassAndModuleChildren:
Enabled: false Enabled: false
Blocks:
Enabled: false
AlignParameters:
Enabled: false
HashSyntax:
EnforcedStyle: ruby19

View File

@ -35,7 +35,7 @@ module Middleman
aliases: '-b', aliases: '-b',
desc: 'Run `middleman build` before the deploy step' desc: 'Run `middleman build` before the deploy step'
def self.subcommand_help options def self.subcommand_help(_options)
# TODO # TODO
end end
@ -44,7 +44,6 @@ module Middleman
true true
end end
def deploy def deploy
env = options['environment'] ? :production : options['environment'].to_s.to_sym env = options['environment'] ? :production : options['environment'].to_s.to_sym
verbose = options['verbose'] ? 0 : 1 verbose = options['verbose'] ? 0 : 1
@ -62,7 +61,7 @@ module Middleman
protected protected
def build_before(options = {}) def build_before(options = {})
build_enabled = options.fetch('build_before', self.deploy_options.build_before) build_enabled = options.fetch('build_before', deploy_options.build_before)
if build_enabled if build_enabled
# http://forum.middlemanapp.com/t/problem-with-the-build-task-in-an-extension # http://forum.middlemanapp.com/t/problem-with-the-build-task-in-an-extension
@ -71,16 +70,14 @@ module Middleman
end end
def print_usage_and_die(message) def print_usage_and_die(message)
raise StandardError, "ERROR: #{message}\n#{Middleman::Deploy::README}" fail StandardError, "ERROR: #{message}\n#{Middleman::Deploy::README}"
end end
def process def process
server_instance = @app server_instance = @app
camelized_method = self.deploy_options.deploy_method.to_s.split('_').map { |word| word.capitalize}.join camelized_method = deploy_options.deploy_method.to_s.split('_').map(&:capitalize).join
method_class_name = "Middleman::Deploy::Methods::#{camelized_method}" method_class_name = "Middleman::Deploy::Methods::#{camelized_method}"
method_instance = method_class_name.constantize.new(server_instance, self.deploy_options) method_instance = method_class_name.constantize.new(server_instance, deploy_options)
method_instance.process method_instance.process
end end

View File

@ -4,23 +4,15 @@ require 'middleman-core'
# Extension namespace # Extension namespace
module Middleman module Middleman
module Deploy module Deploy
@options @options
class << self class << self
attr_reader :options
def options attr_writer :options
@options
end
def options= options
@options = options
end
end end
class Extension < Extension class Extension < Extension
option :deploy_method, nil option :deploy_method, nil
option :host, nil option :host, nil
option :port, nil option :port, nil
@ -35,8 +27,6 @@ module Middleman
option :flags, nil option :flags, nil
option :commit_message, nil option :commit_message, nil
def initialize(app, options_hash = {}, &block) def initialize(app, options_hash = {}, &block)
super super
@ -50,17 +40,14 @@ module Middleman
options.remote ||= 'origin' options.remote ||= 'origin'
options.branch ||= 'gh-pages' options.branch ||= 'gh-pages'
options.strategy ||= :force_push options.strategy ||= :force_push
options.commit_message ||= nil options.commit_message ||= nil
options.build_before ||= false options.build_before ||= false
end end
def after_configuration def after_configuration
::Middleman::Deploy.options = options ::Middleman::Deploy.options = options
end end
end end
end end
end end

View File

@ -10,11 +10,11 @@ module Middleman
end end
def build_dir def build_dir
self.server_instance.config.setting(:build_dir).value server_instance.config.setting(:build_dir).value
end end
def process def process
raise NotImplementedError fail NotImplementedError
end end
end end
end end

View File

@ -18,11 +18,11 @@ module Middleman
end end
def process def process
puts "## Deploying via ftp to #{self.user}@#{self.host}:#{self.path}" puts "## Deploying via ftp to #{user}@#{host}:#{path}"
ftp = open_connection ftp = open_connection
Dir.chdir(self.build_dir) do Dir.chdir(build_dir) do
filtered_files.each do |filename| filtered_files.each do |filename|
if File.directory?(filename) if File.directory?(filename)
upload_directory(ftp, filename) upload_directory(ftp, filename)
@ -57,9 +57,9 @@ module Middleman
end end
def open_connection def open_connection
ftp = Net::FTP.new(self.host) ftp = Net::FTP.new(host)
ftp.login(self.user, self.pass) ftp.login(user, pass)
ftp.chdir(self.path) ftp.chdir(path)
ftp.passive = true ftp.passive = true
ftp ftp
@ -76,11 +76,9 @@ module Middleman
end end
def upload_directory(ftp, filename) def upload_directory(ftp, filename)
begin ftp.mkdir(filename)
ftp.mkdir(filename) puts "Created directory #{filename}"
puts "Created directory #{filename}" rescue
rescue
end
end end
end end
end end

View File

@ -3,11 +3,11 @@ module Middleman
module Methods module Methods
class Git < Base class Git < Base
def process def process
puts "## Deploying via git to remote=\"#{self.options.remote}\" and branch=\"#{self.options.branch}\"" puts "## Deploying via git to remote=\"#{options.remote}\" and branch=\"#{options.branch}\""
camelized_strategy = self.options.strategy.to_s.split('_').map { |word| word.capitalize}.join camelized_strategy = options.strategy.to_s.split('_').map(&:capitalize).join
strategy_class_name = "Middleman::Deploy::Strategies::Git::#{camelized_strategy}" strategy_class_name = "Middleman::Deploy::Strategies::Git::#{camelized_strategy}"
strategy_instance = strategy_class_name.constantize.new(self.build_dir, self.options.remote, self.options.branch, self.options.commit_message) strategy_instance = strategy_class_name.constantize.new(build_dir, options.remote, options.branch, options.commit_message)
strategy_instance.process strategy_instance.process
end end

View File

@ -17,17 +17,15 @@ module Middleman
def process def process
# Append "@" to user if provided. # Append "@" to user if provided.
user = "#{self.user}@" if self.user && !self.user.empty? user = "#{self.user}@" if user && !user.empty?
dest_url = "#{user}#{self.host}:#{self.path}" dest_url = "#{user}#{host}:#{path}"
flags = self.flags || '-avz' flags = self.flags || '-avz'
command = "rsync #{flags} '-e ssh -p #{self.port}' #{self.build_dir}/ #{dest_url}" command = "rsync #{flags} '-e ssh -p #{port}' #{build_dir}/ #{dest_url}"
if self.clean command += ' --delete' if clean
command += ' --delete'
end
puts "## Deploying via rsync to #{dest_url} port=#{self.port}" puts "## Deploying via rsync to #{dest_url} port=#{port}"
exec command exec command
end end
end end

View File

@ -6,13 +6,13 @@ module Middleman
module Methods module Methods
class Sftp < Ftp class Sftp < Ftp
def process def process
puts "## Deploying via sftp to #{self.user}@#{self.host}:#{path}" puts "## Deploying via sftp to #{user}@#{host}:#{path}"
# `nil` is a valid value for user and/or pass. # `nil` is a valid value for user and/or pass.
Net::SFTP.start(self.host, self.user, password: self.pass, port: self.port) do |sftp| Net::SFTP.start(host, user, password: pass, port: port) do |sftp|
sftp.mkdir(self.path) sftp.mkdir(path)
Dir.chdir(self.build_dir) do Dir.chdir(build_dir) do
filtered_files.each do |filename| filtered_files.each do |filename|
if File.directory?(filename) if File.directory?(filename)
upload_directory(sftp, filename) upload_directory(sftp, filename)
@ -26,17 +26,15 @@ module Middleman
protected protected
def handle_exception(exception,filename, file_path) def handle_exception(exception, filename, file_path)
reply = exception.message reply = exception.message
err_code = reply[0, 3].to_i err_code = reply[0, 3].to_i
if err_code == 550 sftp.upload(filename, file_path) if err_code == 550
sftp.upload(filename, file_path)
end
end end
def upload_directory(sftp, filename) def upload_directory(sftp, filename)
file_path = "#{self.path}/#{filename}" file_path = "#{path}/#{filename}"
begin begin
sftp.mkdir(file_path) sftp.mkdir(file_path)
@ -46,7 +44,7 @@ module Middleman
end end
def upload_file(sftp, filename) def upload_file(sftp, filename)
file_path = "#{self.path}/#{filename}" file_path = "#{path}/#{filename}"
begin begin
sftp.upload(filename, file_path) sftp.upload(filename, file_path)

View File

@ -15,7 +15,7 @@ module Middleman
end end
def process def process
raise NotImplementedError fail NotImplementedError
end end
protected protected
@ -29,24 +29,25 @@ module Middleman
def checkout_branch def checkout_branch
# if there is a branch with that name, switch to it, otherwise create a new one and switch to it # if there is a branch with that name, switch to it, otherwise create a new one and switch to it
if `git branch`.split("\n").any? { |b| b =~ /#{self.branch}/i } if `git branch`.split("\n").any? { |b| b =~ /#{branch}/i }
`git checkout #{self.branch}` `git checkout #{branch}`
else else
`git checkout -b #{self.branch}` `git checkout -b #{branch}`
end end
end end
def commit_branch(options = '') def commit_branch(options = '')
message = self.commit_message ? self.commit_message : add_signature_to_commit_message('Automated commit') message = commit_message ? commit_message : add_signature_to_commit_message('Automated commit')
run_or_fail("git add -A") run_or_fail('git add -A')
run_or_fail("git commit --allow-empty -am \"#{message}\"") run_or_fail("git commit --allow-empty -am \"#{message}\"")
run_or_fail("git push #{options} origin #{self.branch}") run_or_fail("git push #{options} origin #{branch}")
end end
private private
def run_or_fail(command) def run_or_fail(command)
system(command) || raise("ERROR running: #{command}") system(command) || fail("ERROR running: #{command}")
end end
end end
end end

View File

@ -4,7 +4,7 @@ module Middleman
module Git module Git
class ForcePush < Base class ForcePush < Base
def process def process
Dir.chdir(self.build_dir) do Dir.chdir(build_dir) do
add_remote_url add_remote_url
checkout_branch checkout_branch
commit_branch('-f') commit_branch('-f')
@ -19,8 +19,8 @@ module Middleman
unless File.exist?('.git') unless File.exist?('.git')
`git init` `git init`
`git remote add origin #{url}` `git remote add origin #{url}`
`git config user.name "#{self.user_name}"` `git config user.name "#{user_name}"`
`git config user.email "#{self.user_email}"` `git config user.email "#{user_email}"`
else else
# check if the remote repo has changed # check if the remote repo has changed
unless url == `git config --get remote.origin.url`.chop unless url == `git config --get remote.origin.url`.chop
@ -28,9 +28,9 @@ module Middleman
`git remote add origin #{url}` `git remote add origin #{url}`
end end
# check if the user name has changed # check if the user name has changed
`git config user.name "#{self.user_name}"` unless self.user_name == `git config --get user.name` `git config user.name "#{user_name}"` unless user_name == `git config --get user.name`
# check if the user email has changed # check if the user email has changed
`git config user.email "#{self.user_email}"` unless self.user_email == `git config --get user.email` `git config user.email "#{user_email}"` unless user_email == `git config --get user.email`
end end
end end

View File

@ -4,7 +4,7 @@ module Middleman
module Git module Git
class Submodule < Base class Submodule < Base
def process def process
Dir.chdir(self.build_dir) do Dir.chdir(build_dir) do
checkout_branch checkout_branch
pull_submodule pull_submodule
commit_branch commit_branch
@ -19,7 +19,7 @@ module Middleman
current_branch = `git rev-parse --abbrev-ref HEAD` current_branch = `git rev-parse --abbrev-ref HEAD`
message = add_signature_to_commit_message('Deployed') message = add_signature_to_commit_message('Deployed')
`git add #{self.build_dir}` `git add #{build_dir}`
`git commit --allow-empty -m "#{message}"` `git commit --allow-empty -m "#{message}"`
`git push origin #{current_branch}` `git push origin #{current_branch}`
end end
@ -27,11 +27,11 @@ module Middleman
def pull_submodule def pull_submodule
`git fetch` `git fetch`
`git stash` `git stash`
`git rebase #{self.remote}/#{self.branch}` `git rebase #{remote}/#{branch}`
`git stash pop` `git stash pop`
if $?.exitstatus == 1 if $CHILD_STATUS.exitstatus == 1
puts "Can't deploy! Please resolve conflicts. Then process to manual commit and push on #{self.branch} branch." puts "Can't deploy! Please resolve conflicts. Then process to manual commit and push on #{branch} branch."
exit exit
end end
end end