Compare commits

..

No commits in common. "master" and "v0.2.2" have entirely different histories.

30 changed files with 328 additions and 498 deletions

View file

@ -1,3 +0,0 @@
LICENSE.md
README.md
lib/**/*.rb

26
.gitignore vendored
View file

@ -1,18 +1,8 @@
*.gem # Ignore bundler lock file
*.rbc /Gemfile.lock
.bundle
.config # Ignore pkg folder
.yardoc /pkg
Gemfile.lock
InstalledFiles # Ignore coverage folder
_yardoc /coverage
coverage
doc/
lib/bundler/man
pkg
rdoc
spec/reports
test/tmp
test/version_tmp
tmp
bin

View file

@ -1,11 +0,0 @@
AllCops:
Include:
- 'Gemfile'
Exclude:
- 'script/**/*'
- 'vendor/**/*'
- 'bin/**/*'
Documentation:
Enabled: false
ClassAndModuleChildren:
Enabled: false

View file

@ -1,26 +0,0 @@
language: ruby
sudo: false
cache: bundler
bundler_args: --without development
rvm:
- ruby-head
- ruby
- jruby-head
- jruby
- 2.1.0
- 2.0.0
- 1.9.3
- rbx-2
before_script: bundle update
matrix:
fast_finish: true
allow_failures:
- rvm: ruby-head
- rvm: ruby
- rvm: jruby-head
- rvm: jruby
- rvm: rbx-2
notifications:
email: false
env:
- CODECLIMATE_REPO_TOKEN=5eee8e8624962f963a52a1d2313dc7407e3b8006291e3704346c786642cc073b

View file

@ -1,6 +0,0 @@
--markup markdown
-
CHANGELOG.md
CONTRIBUTING.md
LICENSE.md
README.md

View file

@ -1,12 +0,0 @@
Next Release
============
* Your contribution here.
2.0.0-alpha (02/08/2015)
==================
* [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](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

@ -1,44 +0,0 @@
## Contributing
In the spirit of [free software][free-sw], **everyone** is encouraged to help
improve this project.
[free-sw]: http://www.fsf.org/licensing/essays/free-sw.html
Here are some ways *you* can contribute:
* by using alpha, beta, and prerelease versions
* by reporting bugs
* by suggesting new features
* by writing or editing documentation
* by writing specifications
* by writing code (**no patch is too small**: fix typos, add comments, clean up
inconsistent whitespace)
* by refactoring code
* by closing [issues][]
* by reviewing patches
[issues]: https://github.com/karlfreeman/middleman-deploy/issues
## Submitting an Issue
We use the [GitHub issue tracker][issues] to track bugs and features. Before
submitting a bug report or feature request, check to make sure it hasn't
already been submitted. When submitting a bug report, please include a [Gist][]
that includes a stack trace and any details that may be necessary to reproduce
the bug, including your gem version, Ruby version, and operating system.
Ideally, a bug report should include a pull request with failing specs.
[gist]: https://gist.github.com/
## Submitting a Pull Request
1. [Fork the repository.][fork]
2. [Create a topic branch.][branch]
3. Add specs for your unimplemented feature or bug fix.
4. Run `bundle exec rake cucumber`. If your specs pass, return to step 3.
5. Implement your feature or bug fix.
6. Run `bundle exec rake cucumber`. If your specs fail, return to step 5.
7. Add, commit, and push your changes.
9. [Submit a pull request.][pr]
[fork]: http://help.github.com/fork-a-repo/
[branch]: http://learn.github.com/p/branching.html
[pr]: http://help.github.com/send-pull-requests/

View file

@ -1,6 +1,4 @@
Copyright (c) 2012-2014 Tom Vaughan, Karl Freeman Copyright (c) 2012 Tom Vaughan <thomas.david.vaughan@gmail.com>
MIT License
Permission is hereby granted, free of charge, to any person obtaining Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the a copy of this software and associated documentation files (the
@ -10,13 +8,13 @@ distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to permit persons to whom the Software is furnished to do so, subject to
the following conditions: the following conditions:
The above copyright notice and this permission notice shall be The above copyright notice and this permission notice shall be included
included in all copies or substantial portions of the Software. in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

20
Gemfile
View file

@ -1,11 +1,19 @@
source 'https://rubygems.org' source 'https://rubygems.org'
# Specify your gem's dependencies in middleman-deploy.gemspec
gemspec gemspec
group :test do group :development do
gem 'rake', '~> 10.0' gem "rake"
gem 'cucumber', '~> 1.3' gem "rdoc"
gem 'aruba', '~> 0.5' gem "yard"
gem 'fivemat' end
gem 'codeclimate-test-reporter'
group :test do
gem "compass"
gem "cucumber"
gem "fivemat"
gem "aruba"
gem "rspec"
gem "simplecov"
end end

143
README.md
View file

@ -1,13 +1,18 @@
# Middleman Deploy # middleman-deploy [![Gem Version](https://badge.fury.io/rb/middleman-deploy.png)](http://badge.fury.io/rb/middleman-deploy)
Deploy your [Middleman](http://middlemanapp.com/) build via **rsync**, **ftp**, **sftp**, or **git** (e.g. [gh-pages on github](https://help.github.com/articles/creating-project-pages-manually)). Deploys a [middleman](http://middlemanapp.com/) built site via **rsync**,
**ftp**, **sftp**, or **git** (e.g. gh-pages on github).
## Installation ## Installation
Add this to the Gemfile of the repository of your middleman site:
```ruby ```ruby
gem 'middleman-deploy', '~> 1.0' gem "middleman-deploy"
``` ```
and run `bundle install`.
## Usage ## Usage
``` ```
@ -15,25 +20,37 @@ $ middleman build [--clean]
$ middleman deploy [--build-before] $ middleman deploy [--build-before]
``` ```
To automatically run `middleman build` during `middleman deploy`, turn on the
`build_before` option while activating the deploy extension:
```ruby
activate :deploy do |deploy|
# ...
deploy.build_before = true # default: false
end
```
## Possible Configurations ## Possible Configurations
Middleman-deploy can deploy a site via rsync, ftp, sftp, or git. Checkout [the wiki](https://github.com/tvaughan/middleman-deploy/wiki/_pages) for advanced set-up options. Middleman-deploy can deploy a site via rsync, ftp, sftp, or git.
### Rsync Checkout [the wiki](https://github.com/tvaughan/middleman-deploy/wiki/_pages) for advanced set-up options.
### rsync
Make sure that `rsync` is installed, and activate the extension by adding the Make sure that `rsync` is installed, and activate the extension by adding the
following to `config.rb`: 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
# deploy.clean = true # remove orphaned files on remote host, default: false # deploy.clean = true # remove orphaned files on remote host, default: false
# deploy.flags = '-rltgoDvzO --no-p --del' # add custom flags, default: -avz # deploy.flags = "-rltgoDvzO --no-p --del" # add custom flags, default: -avz
end end
``` ```
@ -44,12 +61,11 @@ 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
# deploy.strategy = :submodule # commit strategy: can be :force_push or :submodule, default: :force_push # deploy.strategy = :submodule # commit strategy: can be :force_push or :submodule, default: :force_push
# deploy.commit_message = 'custom-message' # commit message (can be empty), default: Automated commit at `timestamp` by middleman-deploy `version`
end end
``` ```
@ -70,11 +86,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,25 +100,13 @@ 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
end
```
### Run Automatically
To automatically run `middleman build` during `middleman deploy`, turn on the
`build_before` option while activating the deploy extension:
```ruby
activate :deploy do |deploy|
# ...
deploy.build_before = true # default: false
end end
``` ```
@ -115,15 +119,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
``` ```
@ -133,7 +137,7 @@ end
namespace :deploy do namespace :deploy do
def deploy(env) def deploy(env)
puts "Deploying to #{env}" puts "Deploying to #{env}"
system "TARGET=#{env} bundle exec middleman deploy" exec "TARGET=#{env} bundle exec middleman deploy"
end end
task :staging do task :staging do
@ -146,41 +150,28 @@ namespace :deploy do
end end
``` ```
``` $ rake deploy:staging
$ rake deploy:staging $ rake deploy:production
$ rake deploy:production
```
## Badges ## Breaking Changes
[![Gem Version](http://img.shields.io/gem/v/middleman-deploy.svg)][gem] * `v0.1.0`
[![Build Status](http://img.shields.io/travis/karlfreeman/middleman-deploy.svg)][travis] - Removed the `--clean` command-line option. This option only applied to
[![Code Quality](http://img.shields.io/codeclimate/github/karlfreeman/middleman-deploy.svg)][codeclimate] the rsync deploy method. The idea going forward is that command-line
[![Code Coverage](http://img.shields.io/codeclimate/coverage/github/karlfreeman/middleman-deploy.svg)][codeclimate] options must apply to all deploy methods. Options that are specific to a
[![Gittip](http://img.shields.io/gittip/karlfreeman.svg)][gittip] deploy method will only be available in `config.rb`.
- Removed `deploy` from the `after_build` hook. This caused a `deploy` to
be run each time `build` was called. This workflow never made
sense. `deploy` was added to the `after_build` hook simply because it
was available.
## Supported Ruby Versions ## Thanks!
This library aims to support and is [tested against][travis] the following Ruby A **BIG** thanks to
implementations: [everyone who has contributed](https://github.com/tvaughan/middleman-deploy/graphs/contributors)!
Almost all pull requests are accepted.
- Ruby 2.1.0 ## Other
- Ruby 2.0.0
- Ruby 1.9.3
- [JRuby][jruby]
- [Rubinius][rubinius]
# Credits Inspired by the rsync task in
[Octopress](https://github.com/imathis/octopress).
A **BIG** thanks to [everyone who has contributed](https://github.com/karlfreeman/middleman-deploy/graphs/contributors)! Almost all pull requests are accepted.
Inspiration:
- The rsync task in [Octopress](https://github.com/imathis/octopress)
[gem]: https://rubygems.org/gems/middleman-deploy
[travis]: http://travis-ci.org/karlfreeman/middleman-deploy
[codeclimate]: https://codeclimate.com/github/karlfreeman/middleman-deploy
[gittip]: https://www.gittip.com/karlfreeman
[jruby]: http://www.jruby.org
[rubinius]: http://rubini.us

View file

@ -1,25 +1,14 @@
require 'bundler' require 'bundler'
Bundler.setup
Bundler::GemHelper.install_tasks Bundler::GemHelper.install_tasks
require 'cucumber/rake/task' require 'cucumber/rake/task'
Cucumber::Rake::Task.new(:cucumber, 'Run features that should pass') do |t| Cucumber::Rake::Task.new(:cucumber, 'Run features that should pass') do |t|
exempt_tags = ['--tags ~@wip'] t.cucumber_opts = "--color --tags ~@wip --strict --format #{ENV['CUCUMBER_FORMAT'] || 'Fivemat'}"
t.cucumber_opts = "--color #{exempt_tags.join(' ')} --strict --format #{ENV['CUCUMBER_FORMAT'] || 'Fivemat'}"
end end
begin require 'rake/clean'
require 'yard'
YARD::Rake::YardocTask.new
rescue LoadError
end
begin task :test => ["cucumber"]
require 'rubocop/rake_task'
desc 'Run rubocop'
RuboCop::RakeTask.new(:rubocop)
rescue LoadError
end
task default: :cucumber task :default => :test
task test: :cucumber

56
USAGE Normal file
View file

@ -0,0 +1,56 @@
You should follow one of the four examples below to setup the deploy
extension in config.rb.
# To deploy the build directory to a remote host via rsync:
activate :deploy do |deploy|
deploy.method = :rsync
# host and path *must* be set
deploy.host = "www.example.com"
deploy.path = "/srv/www/site"
# user is optional (no default)
deploy.user = "tvaughan"
# port is optional (default is 22)
deploy.port = 5309
# clean is optional (default is false)
deploy.clean = true
# flags is optional (default is -avze)
deploy.flags = "-rltgoDvzO --no-p --del -e"
end
# To deploy to a remote branch via git (e.g. gh-pages on github):
activate :deploy do |deploy|
deploy.method = :git
# remote is optional (default is "origin")
# run `git remote -v` to see a list of possible remotes
deploy.remote = "some-other-remote-name"
# branch is optional (default is "gh-pages")
# run `git branch -a` to see a list of possible branches
deploy.branch = "some-other-branch-name"
# strategy is optional (default is :force_push)
deploy.strategy = :submodule
end
# To deploy the build directory to a remote host via ftp:
activate :deploy do |deploy|
deploy.method = :ftp
# host, user, passwword and path *must* be set
deploy.host = "ftp.example.com"
deploy.path = "/srv/www/site"
deploy.user = "tvaughan"
deploy.password = "secret"
end
# To deploy the build directory to a remote host via sftp:
activate :deploy do |deploy|
deploy.method = :sftp
# host, user, passwword and path *must* be set
deploy.host = "sftp.example.com"
deploy.port = 22
deploy.path = "/srv/www/site"
# user is optional (no default)
deploy.user = "tvaughan"
# password is optional (no default)
deploy.password = "secret"
end

View file

@ -1,8 +1,6 @@
require "simplecov"
SimpleCov.start
PROJECT_ROOT_PATH = File.dirname(File.dirname(File.dirname(__FILE__))) PROJECT_ROOT_PATH = File.dirname(File.dirname(File.dirname(__FILE__)))
require 'middleman-core' require "middleman-core"
require 'middleman-core/step_definitions' require "middleman-core/step_definitions"
require 'codeclimate-test-reporter'
CodeClimate::TestReporter.start
require File.join(PROJECT_ROOT_PATH, 'lib', 'middleman-deploy') require File.join(PROJECT_ROOT_PATH, 'lib', 'middleman-deploy')

View file

@ -1,6 +0,0 @@
Feature: test
Scenario: whilst testing
Given the Server is running at "test-app"
When I go to "/index.html"
Then I should see "<h1>Test</h1>"

View file

@ -1 +0,0 @@
<h1>Test</h1>

View file

@ -1,8 +1,8 @@
require 'middleman-core' require "middleman-core"
require 'middleman-deploy/commands' 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,83 +1,60 @@
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/extension"
require 'middleman-deploy/extension' require "middleman-deploy/methods"
require 'middleman-deploy/methods' require "middleman-deploy/strategies"
require 'middleman-deploy/strategies' require "middleman-deploy/pkg-info"
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
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}" usage_path = File.join(File.dirname(__FILE__), '..', '..', 'USAGE')
usage_message = File.read(usage_path)
raise Error, "ERROR: #{message}\n#{usage_message}"
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,23 +63,23 @@ 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
print_usage_and_die 'The ftp deploy method requires host, path, user, and password to be set.' print_usage_and_die "The ftp deploy method requires host, path, user, and password to be set."
end end
end end
@ -110,10 +87,7 @@ 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
end end

View file

@ -1,35 +1,20 @@
# Require core library # Require core library
require 'middleman-core' require "middleman-core"
# Extension namespace # Extension namespace
module Middleman module Middleman
module Deploy module Deploy
@options
class Options < Struct.new(:whatisthis, :method, :host, :port, :user, :password, :path, :clean, :remote, :branch, :strategy, :build_before, :flags); end
class << self class << self
attr_reader :options
attr_writer :options def options
end @@options
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.
@ -37,17 +22,26 @@ module Middleman
options.clean ||= false options.clean ||= false
# Default options for the git method. # Default options for the git method.
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.build_before ||= false options.build_before ||= false
@@options = options
app.send :include, Helpers
end end
def after_configuration alias :included :registered
::Middleman::Deploy.options = options
end
module Helpers
def options
::Middleman::Deploy.options
end end
end end
end end
end end

View file

@ -4,18 +4,15 @@ module Middleman
class Base class Base
attr_reader :options, :server_instance attr_reader :options, :server_instance
def initialize(server_instance, options = {}) def initialize(server_instance, options={})
@options = options @options = options
@server_instance = server_instance @server_instance = server_instance
end end
def build_dir def process
server_instance.config.setting(:build_dir).value raise NotImplementedError
end end
def process
fail NotImplementedError
end
end end
end end
end end

View file

@ -5,9 +5,10 @@ module Middleman
module Deploy module Deploy
module Methods module Methods
class Ftp < Base class Ftp < Base
attr_reader :host, :port, :pass, :path, :user attr_reader :host, :port, :pass, :path, :user
def initialize(server_instance, options = {}) def initialize(server_instance, options={})
super(server_instance, options) super(server_instance, options)
@host = self.options.host @host = self.options.host
@ -18,15 +19,15 @@ 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)
else else
upload_binary(ftp, filename) upload_binary(ftp, filename)
end end
end end
@ -35,7 +36,7 @@ module Middleman
ftp.close ftp.close
end end
protected protected
def filtered_files def filtered_files
files = Dir.glob('**/*', File::FNM_DOTMATCH) files = Dir.glob('**/*', File::FNM_DOTMATCH)
@ -45,7 +46,7 @@ module Middleman
def handle_exception(exception, ftp, filename) def handle_exception(exception, ftp, filename)
reply = exception.message reply = exception.message
err_code = reply[0, 3].to_i err_code = reply[0,3].to_i
if err_code == 550 if err_code == 550
if File.binary?(filename) if File.binary?(filename)
@ -57,9 +58,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,10 +77,14 @@ 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
end end

View file

@ -2,15 +2,17 @@ module Middleman
module Deploy module Deploy
module Methods module Methods
class Git < Base class Git < Base
def process
puts "## Deploying via git to remote=\"#{options.remote}\" and branch=\"#{options.branch}\""
camelized_strategy = options.strategy.to_s.split('_').map(&:capitalize).join def process
puts "## Deploying via git to remote=\"#{self.options.remote}\" and branch=\"#{self.options.branch}\""
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)
strategy_instance.process strategy_instance.process
end end
end end
end end
end end

View file

@ -2,9 +2,10 @@ module Middleman
module Deploy module Deploy
module Methods module Methods
class Rsync < Base class Rsync < Base
attr_reader :clean, :flags, :host, :path, :port, :user attr_reader :clean, :flags, :host, :path, :port, :user
def initialize(server_instance, options = {}) def initialize(server_instance, options={})
super(server_instance, options) super(server_instance, options)
@clean = self.options.clean @clean = self.options.clean
@ -17,17 +18,20 @@ 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 run command
end end
end end
end end
end end

View file

@ -5,14 +5,15 @@ module Middleman
module Deploy module Deploy
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)
@ -24,17 +25,19 @@ module Middleman
end end
end end
protected protected
def handle_exception(exception, filename, file_path) def handle_exception(exception)
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,17 +47,19 @@ 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)
rescue Exception => exception rescue Exception => exception
handle_exception(exception, filename, file_path) handle_exception(exception, file_path)
end end
puts "Copied #{filename}" puts "Copied #{filename}"
end end
end end
end end
end end
end end

View file

@ -1,64 +1,7 @@
module Middleman module Middleman
module Deploy module Deploy
PACKAGE = 'middleman-deploy' PACKAGE = "middleman-deploy"
VERSION = '2.0.0-alpha' VERSION = "0.2.2"
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 = %{
You should follow one of the four examples below to setup the deploy
extension in config.rb.
# To deploy the build directory to a remote host via rsync:
activate :deploy do |deploy|
deploy.method = :rsync
# host and path *must* be set
deploy.host = "www.example.com"
deploy.path = "/srv/www/site"
# user is optional (no default)
deploy.user = "tvaughan"
# port is optional (default is 22)
deploy.port = 5309
# clean is optional (default is false)
deploy.clean = true
# flags is optional (default is -avze)
deploy.flags = "-rltgoDvzO --no-p --del -e"
end
# To deploy to a remote branch via git (e.g. gh-pages on github):
activate :deploy do |deploy|
deploy.method = :git
# remote is optional (default is "origin")
# run `git remote -v` to see a list of possible remotes
deploy.remote = "some-other-remote-name"
# branch is optional (default is "gh-pages")
# run `git branch -a` to see a list of possible branches
deploy.branch = "some-other-branch-name"
# strategy is optional (default is :force_push)
deploy.strategy = :submodule
end
# To deploy the build directory to a remote host via ftp:
activate :deploy do |deploy|
deploy.method = :ftp
# host, user, passwword and path *must* be set
deploy.host = "ftp.example.com"
deploy.path = "/srv/www/site"
deploy.user = "tvaughan"
deploy.password = "secret"
end
# To deploy the build directory to a remote host via sftp:
activate :deploy do |deploy|
deploy.method = :sftp
# host, user, passwword and path *must* be set
deploy.host = "sftp.example.com"
deploy.port = 22
deploy.path = "/srv/www/site"
# user is optional (no default)
deploy.user = "tvaughan"
# password is optional (no default)
deploy.password = "secret"
end}
end end
end end

View file

@ -3,22 +3,19 @@ module Middleman
module Strategies module Strategies
module Git module Git
class Base class Base
attr_accessor :branch, :build_dir, :remote, :commit_message, :user_name, :user_email attr_accessor :branch, :build_dir, :remote
def initialize(build_dir, remote, branch, commit_message) def initialize(build_dir, remote, branch)
self.branch = branch self.branch = branch
self.build_dir = build_dir self.build_dir = build_dir
self.remote = remote self.remote = remote
self.commit_message = commit_message
self.user_name = `git config --get user.name`
self.user_email = `git config --get user.email`
end end
def process def process
fail NotImplementedError raise NotImplementedError
end end
protected protected
def add_signature_to_commit_message(base_message) def add_signature_to_commit_message(base_message)
signature = "#{Middleman::Deploy::PACKAGE} #{Middleman::Deploy::VERSION}" signature = "#{Middleman::Deploy::PACKAGE} #{Middleman::Deploy::VERSION}"
@ -29,26 +26,21 @@ 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 = add_signature_to_commit_message('Automated commit')
run_or_fail('git add -A') `git add -A`
run_or_fail("git commit --allow-empty -am \"#{message}\"") `git commit --allow-empty -am "#{message}"`
run_or_fail("git push #{options} origin #{branch}") `git push #{options} origin #{self.branch}`
end end
private
def run_or_fail(command)
system(command) || fail("ERROR running: #{command}")
end
end end
end end
end end

View file

@ -3,34 +3,29 @@ module Middleman
module Strategies module Strategies
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')
end end
end end
private private
def add_remote_url def add_remote_url
url = get_remote_url url = get_remote_url
unless File.exist?('.git') unless File.exists?('.git')
`git init` `git init`
`git remote add origin #{url}` `git remote add origin #{url}`
`git config user.name "#{user_name}"`
`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
`git remote rm origin` `git remote rm origin`
`git remote add origin #{url}` `git remote add origin #{url}`
end end
# check if the user name has changed
`git config user.name "#{user_name}"` unless user_name == `git config --get user.name`
# check if the user email has changed
`git config user.email "#{user_email}"` unless user_email == `git config --get user.email`
end end
end end
@ -51,6 +46,7 @@ module Middleman
url url
end end
end end
end end
end end

View file

@ -3,8 +3,9 @@ module Middleman
module Strategies module Strategies
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
@ -13,13 +14,13 @@ module Middleman
commit_submodule commit_submodule
end end
private private
def commit_submodule def commit_submodule
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,14 +28,15 @@ 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
end end
end end
end end

View file

@ -1 +1 @@
require 'middleman-deploy' require "middleman-deploy"

View file

@ -1,32 +1,27 @@
# coding: utf-8 # -*- encoding: utf-8 -*-
lib = File.expand_path('../lib', __FILE__) $:.push File.expand_path("../lib", __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require "middleman-deploy/pkg-info"
require 'middleman-deploy/pkg-info'
Gem::Specification.new do |spec| Gem::Specification.new do |s|
spec.name = Middleman::Deploy::PACKAGE s.name = Middleman::Deploy::PACKAGE
spec.version = Middleman::Deploy::VERSION s.version = Middleman::Deploy::VERSION
spec.authors = ['Tom Vaughan', 'Karl Freeman'] s.platform = Gem::Platform::RUBY
spec.email = ['thomas.david.vaughan@gmail.com', 'karlfreeman@gmail.com'] s.authors = ["Tom Vaughan"]
spec.summary = Middleman::Deploy::TAGLINE s.email = ["thomas.david.vaughan@gmail.com"]
spec.description = Middleman::Deploy::TAGLINE s.homepage = "http://github.com/tvaughan/middleman-deploy"
spec.homepage = 'https://github.com/karlfreeman/middleman-deploy' s.summary = Middleman::Deploy::TAGLINE
spec.license = 'MIT' s.description = Middleman::Deploy::TAGLINE
s.license = "MIT"
spec.files = `git ls-files -z`.split("\x0") s.files = `git ls-files`.split("\n")
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
spec.require_paths = ['lib'] s.require_paths = ["lib"]
spec.required_ruby_version = '>= 1.9.3'
spec.add_dependency 'middleman-core', '>= 3.2' # The version of middleman-core your extension depends on
spec.add_dependency 'ptools' s.add_runtime_dependency("middleman-core", [">= 3.0.0"])
spec.add_dependency 'net-sftp'
spec.add_development_dependency 'bundler', '~> 1.5' # Additional dependencies
spec.add_development_dependency 'rake', '~> 10.0' s.add_runtime_dependency("ptools")
spec.add_development_dependency 'kramdown', '>= 0.14' s.add_runtime_dependency("net-sftp")
spec.add_development_dependency 'rubocop', '~> 0.19'
spec.add_development_dependency 'pry'
spec.add_development_dependency 'yard'
end end