Compare commits

..

No commits in common. "master" and "v1.0.0" have entirely different histories.

16 changed files with 131 additions and 162 deletions

View file

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

View file

@ -1,5 +1,4 @@
language: ruby language: ruby
sudo: false
cache: bundler cache: bundler
bundler_args: --without development bundler_args: --without development
rvm: rvm:
@ -11,7 +10,6 @@ rvm:
- 2.0.0 - 2.0.0
- 1.9.3 - 1.9.3
- rbx-2 - rbx-2
before_script: bundle update
matrix: matrix:
fast_finish: true fast_finish: true
allow_failures: allow_failures:

View file

@ -1,12 +1,8 @@
Next Release master
============ ===
* Your contribution here.
2.0.0-alpha (02/08/2015) 1.0.0
================== ===
* [Fixing compatibility issues with middleman v4.0.0.beta.1](https://github.com/middleman-contrib/middleman-deploy/pull/87) - [@emilioforrer](https://github.com/emilioforrer).
1.0.0 (16/07/2014) * Respect user details of git repo. #70
================== * Prevent bad commits deploying. (git) #77
* [Respect user details of git repo](https://github.com/middleman-contrib/middleman-deploy/pull/70) - [@Gee-Bee](https://github.com/gee-bee).
* [Prevent bad commits deploying](https://github.com/middleman-contrib/middleman-deploy/pull/77) - [@karlfreeman](https://github.com/mconnell).

View file

@ -26,9 +26,9 @@ following to `config.rb`:
```ruby ```ruby
activate :deploy do |deploy| activate :deploy do |deploy|
deploy.deploy_method = :rsync deploy.method = :rsync
deploy.host = 'www.example.com' deploy.host = 'www.example.com'
deploy.path = '/srv/www/site' deploy.path = '/srv/www/site'
# Optional Settings # Optional Settings
# deploy.user = 'tvaughan' # no default # deploy.user = 'tvaughan' # no default
# deploy.port = 5309 # ssh port, default: 22 # deploy.port = 5309 # ssh port, default: 22
@ -44,7 +44,7 @@ following to `config.rb`:
```ruby ```ruby
activate :deploy do |deploy| activate :deploy do |deploy|
deploy.deploy_method = :git deploy.method = :git
# Optional Settings # Optional Settings
# deploy.remote = 'custom-remote' # remote name or git url, default: origin # deploy.remote = 'custom-remote' # remote name or git url, default: origin
# deploy.branch = 'custom-branch' # default: gh-pages # deploy.branch = 'custom-branch' # default: gh-pages
@ -70,11 +70,11 @@ Activate the extension by adding the following to `config.rb`:
```ruby ```ruby
activate :deploy do |deploy| activate :deploy do |deploy|
deploy.deploy_method = :ftp deploy.method = :ftp
deploy.host = 'ftp.example.com' deploy.host = 'ftp.example.com'
deploy.path = '/srv/www/site' deploy.path = '/srv/www/site'
deploy.user = 'tvaughan' deploy.user = 'tvaughan'
deploy.password = 'secret' deploy.password = 'secret'
end end
``` ```
@ -84,10 +84,10 @@ Activate the extension by adding the following to `config.rb`:
```ruby ```ruby
activate :deploy do |deploy| activate :deploy do |deploy|
deploy.deploy_method = :sftp deploy.method = :sftp
deploy.host = 'sftp.example.com' deploy.host = 'sftp.example.com'
deploy.port = 22 deploy.port = 22
deploy.path = '/srv/www/site' deploy.path = '/srv/www/site'
# Optional Settings # Optional Settings
# deploy.user = 'tvaughan' # no default # deploy.user = 'tvaughan' # no default
# deploy.password = 'secret' # no default # deploy.password = 'secret' # no default
@ -115,15 +115,15 @@ Deploy your site to more than one configuration using environment variables.
case ENV['TARGET'].to_s.downcase case ENV['TARGET'].to_s.downcase
when 'production' when 'production'
activate :deploy do |deploy| activate :deploy do |deploy|
deploy.deploy_method = :rsync deploy.method = :rsync
deploy.host = 'www.example.com' deploy.host = 'www.example.com'
deploy.path = '/srv/www/production-site' deploy.path = '/srv/www/production-site'
end end
else else
activate :deploy do |deploy| activate :deploy do |deploy|
deploy.deploy_method = :rsync deploy.method = :rsync
deploy.host = 'staging.example.com' deploy.host = 'staging.example.com'
deploy.path = '/srv/www/staging-site' deploy.path = '/srv/www/staging-site'
end end
end end
``` ```

View file

@ -4,5 +4,5 @@ require 'middleman-deploy/commands'
::Middleman::Extensions.register(:deploy) do ::Middleman::Extensions.register(:deploy) do
require 'middleman-deploy/extension' require 'middleman-deploy/extension'
::Middleman::Deploy::Extension ::Middleman::Deploy
end end

View file

@ -1,5 +1,5 @@
require 'middleman-core/cli' require 'middleman-core/cli'
require 'middleman-core/rack' if Middleman::VERSION.to_i > 3
require 'middleman-deploy/pkg-info' require 'middleman-deploy/pkg-info'
require 'middleman-deploy/extension' require 'middleman-deploy/extension'
require 'middleman-deploy/methods' require 'middleman-deploy/methods'
@ -8,52 +8,24 @@ require 'middleman-deploy/strategies'
module Middleman module Middleman
module Cli module Cli
# This class provides a "deploy" command for the middleman CLI. # This class provides a "deploy" command for the middleman CLI.
class Deploy < Thor::Group class Deploy < Thor
include Thor::Actions include Thor::Actions
check_unknown_options! check_unknown_options!
namespace :deploy namespace :deploy
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
# Tell Thor to exit with a nonzero exit code on failure # Tell Thor to exit with a nonzero exit code on failure
def self.exit_on_failure? def self.exit_on_failure?
true true
end end
desc 'deploy [options]', Middleman::Deploy::TAGLINE
method_option 'build_before',
type: :boolean,
aliases: '-b',
desc: 'Run `middleman build` before the deploy step'
def deploy def deploy
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
build_before(options) build_before(options)
process process
end end
@ -61,23 +33,24 @@ module Middleman
protected protected
def build_before(options = {}) def build_before(options = {})
build_enabled = options.fetch('build_before', deploy_options.build_before) build_enabled = options.fetch('build_before', self.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
run("middleman build -e #{options['environment']}") || exit(1) run('middleman build') || exit(1)
end end
end end
def print_usage_and_die(message) def print_usage_and_die(message)
fail StandardError, "ERROR: #{message}\n#{Middleman::Deploy::README}" raise Error, "ERROR: #{message}\n#{Middleman::Deploy::README}"
end end
def process def process
server_instance = @app server_instance = ::Middleman::Application.server.inst
camelized_method = deploy_options.deploy_method.to_s.split('_').map(&:capitalize).join
camelized_method = self.deploy_options.method.to_s.split('_').map { |word| word.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, deploy_options) method_instance = method_class_name.constantize.new(server_instance, self.deploy_options)
method_instance.process method_instance.process
end end
@ -86,19 +59,19 @@ module Middleman
options = nil options = nil
begin begin
options = ::Middleman::Deploy.options options = ::Middleman::Application.server.inst.options
rescue NoMethodError rescue NoMethodError
print_usage_and_die 'You need to activate the deploy extension in config.rb.' print_usage_and_die 'You need to activate the deploy extension in config.rb.'
end end
unless options.deploy_method unless options.method
print_usage_and_die 'The deploy extension requires you to set a method.' print_usage_and_die 'The deploy extension requires you to set a method.'
end end
case options.deploy_method case options.method
when :rsync, :sftp when :rsync, :sftp
unless options.host && options.path unless options.host && options.path
print_usage_and_die "The #{options.deploy_method} method requires host and path to be set." print_usage_and_die "The #{options.method} method requires host and path to be set."
end end
when :ftp when :ftp
unless options.host && options.user && options.password && options.path unless options.host && options.user && options.password && options.path
@ -110,9 +83,6 @@ module Middleman
end end
end end
# Add to CLI
Base.register(Middleman::Cli::Deploy, 'deploy', 'deploy [options]', Middleman::Deploy::TAGLINE)
# Alias "d" to "deploy" # Alias "d" to "deploy"
Base.map('d' => 'deploy') Base.map('d' => 'deploy')
end end

View file

@ -4,32 +4,15 @@ require 'middleman-core'
# Extension namespace # Extension namespace
module Middleman module Middleman
module Deploy module Deploy
@options class Options < Struct.new(:method, :host, :port, :user, :password, :path, :clean, :remote, :branch, :strategy, :build_before, :flags, :commit_message); end
class << self class << self
attr_reader :options def options
@@options
attr_writer :options end
end
class Extension < Extension
option :deploy_method, nil
option :host, nil
option :port, nil
option :user, nil
option :password, nil
option :path, nil
option :clean, nil
option :remote, nil
option :branch, nil
option :strategy, nil
option :build_before, nil
option :flags, nil
option :commit_message, nil
def initialize(app, options_hash = {}, &block)
super
def registered(app, options_hash = {}, &block)
options = Options.new(options_hash)
yield options if block_given? yield options if block_given?
# Default options for the rsync method. # Default options for the rsync method.
@ -40,13 +23,21 @@ 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
@@options = options
app.send :include, Helpers
end end
def after_configuration alias_method :included, :registered
::Middleman::Deploy.options = options end
module Helpers
def options
::Middleman::Deploy.options
end end
end end
end end

View file

@ -9,12 +9,8 @@ module Middleman
@server_instance = server_instance @server_instance = server_instance
end end
def build_dir
server_instance.config.setting(:build_dir).value
end
def process def process
fail NotImplementedError raise 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 #{user}@#{host}:#{path}" puts "## Deploying via ftp to #{self.user}@#{self.host}:#{self.path}"
ftp = open_connection ftp = open_connection
Dir.chdir(build_dir) do Dir.chdir(self.server_instance.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(host) ftp = Net::FTP.new(self.host)
ftp.login(user, pass) ftp.login(self.user, self.pass)
ftp.chdir(path) ftp.chdir(self.path)
ftp.passive = true ftp.passive = true
ftp ftp
@ -76,9 +76,11 @@ module Middleman
end end
def upload_directory(ftp, filename) def upload_directory(ftp, filename)
ftp.mkdir(filename) begin
puts "Created directory #{filename}" ftp.mkdir(filename)
rescue puts "Created directory #{filename}"
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=\"#{options.remote}\" and branch=\"#{options.branch}\"" puts "## Deploying via git to remote=\"#{self.options.remote}\" and branch=\"#{self.options.branch}\""
camelized_strategy = options.strategy.to_s.split('_').map(&:capitalize).join camelized_strategy = self.options.strategy.to_s.split('_').map { |word| word.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(build_dir, options.remote, options.branch, options.commit_message) strategy_instance = strategy_class_name.constantize.new(self.server_instance.build_dir, self.options.remote, self.options.branch, self.options.commit_message)
strategy_instance.process strategy_instance.process
end end

View file

@ -17,15 +17,17 @@ module Middleman
def process def process
# Append "@" to user if provided. # Append "@" to user if provided.
user = "#{self.user}@" if user && !user.empty? user = "#{self.user}@" if self.user && !self.user.empty?
dest_url = "#{user}#{host}:#{path}" dest_url = "#{user}#{self.host}:#{self.path}"
flags = self.flags || '-avz' flags = self.flags || '-avz'
command = "rsync #{flags} '-e ssh -p #{port}' #{build_dir}/ #{dest_url}" command = "rsync #{flags} '-e ssh -p #{self.port}' #{self.server_instance.build_dir}/ #{dest_url}"
command += ' --delete' if clean if self.clean
command += ' --delete'
end
puts "## Deploying via rsync to #{dest_url} port=#{port}" puts "## Deploying via rsync to #{dest_url} port=#{self.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 #{user}@#{host}:#{path}" puts "## Deploying via sftp to #{self.user}@#{self.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(host, user, password: pass, port: port) do |sftp| Net::SFTP.start(self.host, self.user, password: self.pass, port: self.port) do |sftp|
sftp.mkdir(path) sftp.mkdir(self.path)
Dir.chdir(build_dir) do Dir.chdir(self.server_instance.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,15 +26,17 @@ 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
sftp.upload(filename, file_path) if err_code == 550 if err_code == 550
sftp.upload(filename, file_path)
end
end end
def upload_directory(sftp, filename) def upload_directory(sftp, filename)
file_path = "#{path}/#{filename}" file_path = "#{self.path}/#{filename}"
begin begin
sftp.mkdir(file_path) sftp.mkdir(file_path)
@ -44,7 +46,7 @@ module Middleman
end end
def upload_file(sftp, filename) def upload_file(sftp, filename)
file_path = "#{path}/#{filename}" file_path = "#{self.path}/#{filename}"
begin begin
sftp.upload(filename, file_path) sftp.upload(filename, file_path)

View file

@ -1,9 +1,9 @@
module Middleman module Middleman
module Deploy module Deploy
PACKAGE = 'middleman-deploy' PACKAGE = 'middleman-deploy'
VERSION = '2.0.0-alpha' VERSION = '1.0.0'
TAGLINE = 'Deploy a middleman built site over rsync, ftp, sftp, or git (e.g. gh-pages on github).' TAGLINE = 'Deploy a middleman built site over rsync, ftp, sftp, or git (e.g. gh-pages on github).'
README = %{ README = %Q{
You should follow one of the four examples below to setup the deploy You should follow one of the four examples below to setup the deploy
extension in config.rb. extension in config.rb.

View file

@ -15,7 +15,7 @@ module Middleman
end end
def process def process
fail NotImplementedError raise NotImplementedError
end end
protected protected
@ -29,25 +29,24 @@ 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 =~ /#{branch}/i } if `git branch`.split("\n").any? { |b| b =~ /#{self.branch}/i }
`git checkout #{branch}` `git checkout #{self.branch}`
else else
`git checkout -b #{branch}` `git checkout -b #{self.branch}`
end end
end end
def commit_branch(options = '') def commit_branch(options = '')
message = commit_message ? commit_message : add_signature_to_commit_message('Automated commit') message = self.commit_message ? self.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 #{branch}") run_or_fail("git push #{options} origin #{self.branch}")
end end
private private
def run_or_fail(command) def run_or_fail(command)
system(command) || fail("ERROR running: #{command}") system(command) || raise("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(build_dir) do Dir.chdir(self.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 "#{user_name}"` `git config user.name "#{self.user_name}"`
`git config user.email "#{user_email}"` `git config user.name "#{self.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 "#{user_name}"` unless user_name == `git config --get user.name` `git config user.name "#{self.user_name}"` unless self.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 "#{user_email}"` unless user_email == `git config --get user.email` `git config user.email "#{self.user_email}"` unless self.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(build_dir) do Dir.chdir(self.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 #{build_dir}` `git add #{self.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 #{remote}/#{branch}` `git rebase #{self.remote}/#{self.branch}`
`git stash pop` `git stash pop`
if $CHILD_STATUS.exitstatus == 1 if $?.exitstatus == 1
puts "Can't deploy! Please resolve conflicts. Then process to manual commit and push on #{branch} branch." puts "Can't deploy! Please resolve conflicts. Then process to manual commit and push on #{self.branch} branch."
exit exit
end end
end end