Skip to content

Commit

Permalink
Working on requires
Browse files Browse the repository at this point in the history
  • Loading branch information
kdonovan committed Nov 13, 2011
0 parents commit 440832a
Show file tree
Hide file tree
Showing 9 changed files with 186 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
@@ -0,0 +1,4 @@
*.gem
.bundle
Gemfile.lock
pkg/*
4 changes: 4 additions & 0 deletions Gemfile
@@ -0,0 +1,4 @@
source "http://rubygems.org"

# Specify your gem's dependencies in capistrano-conditional.gemspec
gemspec
1 change: 1 addition & 0 deletions Rakefile
@@ -0,0 +1 @@
require "bundler/gem_tasks"
24 changes: 24 additions & 0 deletions capistrano-conditional.gemspec
@@ -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
5 changes: 5 additions & 0 deletions lib/capistrano-conditional.rb
@@ -0,0 +1,5 @@
require "capistrano-conditional/version"
require "capistrano-conditional/unit"
require "capistrano-conditional/deploy"

require "capistrano-conditional/integration"
70 changes: 70 additions & 0 deletions lib/capistrano-conditional/deploy.rb
@@ -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
19 changes: 19 additions & 0 deletions lib/capistrano-conditional/integration.rb
@@ -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
54 changes: 54 additions & 0 deletions lib/capistrano-conditional/unit.rb
@@ -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
5 changes: 5 additions & 0 deletions lib/capistrano-conditional/version.rb
@@ -0,0 +1,5 @@
module Capistrano
module Conditional
VERSION = "0.0.1"
end
end

0 comments on commit 440832a

Please sign in to comment.