Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 440832a
Showing
9 changed files
with
186 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
*.gem | ||
.bundle | ||
Gemfile.lock | ||
pkg/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
source "http://rubygems.org" | ||
|
||
# Specify your gem's dependencies in capistrano-conditional.gemspec | ||
gemspec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require "bundler/gem_tasks" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# -*- encoding: utf-8 -*- | ||
$:.push File.expand_path("../lib", __FILE__) | ||
require "capistrano-conditional/version" | ||
|
||
Gem::Specification.new do |s| | ||
s.name = "capistrano-conditional" | ||
s.version = Capistrano::Conditional::VERSION | ||
s.authors = ["Kali Donovan"] | ||
s.email = ["kali@deviantech.com"] | ||
s.homepage = "" | ||
s.summary = %q{Adds support for conditional deployment tasks} | ||
s.description = %q{Allows making tasks for git-based projects conditional based on the specific files to be deployed.} | ||
|
||
s.rubyforge_project = "capistrano-conditional" | ||
|
||
s.files = `git ls-files`.split("\n") | ||
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") | ||
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } | ||
s.require_paths = ["lib"] | ||
|
||
# specify any dependencies here; for example: | ||
# s.add_development_dependency "rspec" | ||
s.add_runtime_dependency "git" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require "capistrano-conditional/version" | ||
require "capistrano-conditional/unit" | ||
require "capistrano-conditional/deploy" | ||
|
||
require "capistrano-conditional/integration" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
puts "Loading Deploy" | ||
|
||
module Capistrano | ||
class Configuration | ||
class ConditionalDeploy | ||
|
||
@@conditionals = [] | ||
|
||
def self.register(name, opts, &block) | ||
@@conditionals << ConditionalUnit.new(name, opts, block) | ||
end | ||
|
||
|
||
def self.apply_conditions!(deployed) | ||
conditional = self.new(deployed) | ||
conditional.ensure_local_up_to_date | ||
conditional.run_conditionals | ||
conditional.report_plan | ||
|
||
abort "Done" | ||
end | ||
|
||
|
||
|
||
def initialize(compare_to = 'HEAD^') | ||
@verbose = true | ||
@git = Git.open('.') | ||
@diff = @git.diff('HEAD', compare_to) | ||
@changed = @diff.stats[:files].keys.sort | ||
@to_run = [] | ||
end | ||
|
||
def ensure_local_up_to_date | ||
s = @git.status | ||
no_changes = %w(changed added deleted untracked).all? { |attrib| s.send(attrib).empty? } | ||
|
||
unless no_changes | ||
abort "Your working copy contains local changes not yet committed to git. \nPlease commit all changes before deploying.\n\n" | ||
end | ||
end | ||
|
||
def report_plan | ||
if @verbose | ||
puts "\n" * 3 | ||
puts "Conditional Deploy -- Files Modified:" | ||
@changed.each {|f| puts "\t- #{f}"} | ||
end | ||
puts "\n" * 3 | ||
puts "Conditional Deploy Plan:" | ||
if @to_run.empty? | ||
puts "\t* No conditional tasks have been added" | ||
else | ||
@to_run.each do |job| | ||
puts "\t* Running #{job.name}" | ||
end | ||
end | ||
puts "\n" * 3 | ||
end | ||
|
||
def run_conditionals | ||
@@conditionals.each do |job| | ||
force = job.name && ENV["RUN_#{job.name.to_s.upcase}"] | ||
next unless force || job.applies?(@changed) | ||
@to_run << job | ||
end | ||
end | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Capistrano::Configuration.instance(:must_exist).load do | ||
abort "capistrano-conditional is not compatible with Capistrano 1.x." unless respond_to?(:namespace) | ||
|
||
require 'git' | ||
abort "Git is not defined (are you in a git repository, with the Git gem installed?)" unless defined?(Git) | ||
|
||
namespace :conditional do | ||
desc "Initializes the conditional deployment functionality" | ||
task :apply do | ||
log = capture("cd #{current_path} && git log --format=oneline -n 1", :pty => false) | ||
hash = log.split[0] | ||
puts "\nLast deployed git commit: #{log.gsub(hash, '')}\n" | ||
ConditionalDeploy.apply_conditions!(hash) | ||
end | ||
end | ||
|
||
before 'deploy', 'conditional:apply' | ||
before 'deploy:migrations', 'conditional:apply' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
puts "Loading Unit" | ||
|
||
module Capistrano | ||
module Conditional | ||
class Unit | ||
attr_accessor :name, :conditions, :block | ||
|
||
def initialize(name, opts, block) | ||
@name = name | ||
@block = block | ||
@conditions = {} | ||
opts.each do |k,v| | ||
@conditions[k] = v | ||
end | ||
end | ||
|
||
def applies?(changed) | ||
@changed = changed | ||
watchlist_applies? && if_applies? && unless_applies? | ||
end | ||
|
||
protected | ||
|
||
def watchlist_applies? | ||
Array(conditions[:watchlist]).any? do |watched| | ||
@changed.any? do |path| | ||
path[watched] | ||
end | ||
end | ||
end | ||
|
||
def if_applies? | ||
return true if conditions[:if].nil? | ||
condition_true?(:if) | ||
end | ||
|
||
def unless_applies? | ||
return true if conditions[:unless].nil? | ||
!condition_true?(:unless) | ||
end | ||
|
||
def condition_true?(label) | ||
c = conditions[label] | ||
case c.arity | ||
when 0 then c.call | ||
when 1 then c.call(@changed) | ||
else 2 | ||
c.call(@changed, @git) | ||
end | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module Capistrano | ||
module Conditional | ||
VERSION = "0.0.1" | ||
end | ||
end |