Class: ConditionalDeploy

Inherits:
Object
  • Object
show all
Defined in:
lib/capistrano-conditional/deploy.rb

Overview

This class handles the logic associated with checking if each conditional statement applies to a given deploy and, if so, applying them.

The only publicly-useful method is ConditionalDeploy.register, which is used in deploy.rb to add conditional elements (see README for details).

Constant Summary collapse

@@conditionals =
[]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(compare_to = 'HEAD^') ⇒ ConditionalDeploy

Returns a new instance of ConditionalDeploy.



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/capistrano-conditional/deploy.rb', line 39

def initialize(compare_to = 'HEAD^')
  @logger = Capistrano::Logger.new(:output => STDOUT)
  @logger.level = Capistrano::Logger::MAX_LEVEL
  
  @verbose = true
  @git = Git.open('.')
  @last_deployed = @git.object(compare_to)
  @diff = @git.diff('HEAD', compare_to)
  @changed = @diff.stats[:files].keys.sort
  @to_run = []
end

Class Method Details

.apply_conditions!(deployed) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/capistrano-conditional/deploy.rb', line 29

def self.apply_conditions!(deployed)
  conditional = self.new(deployed)
  conditional.ensure_local_up_to_date
  conditional.screen_conditionals
  conditional.report_plan
  conditional.run_conditionals
end

.monitor_migrations(context) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/capistrano-conditional/deploy.rb', line 15

def self.monitor_migrations(context)
  if ARGV.any?{|v| v['deploy:migrations']} # If running deploy:migrations
    # If there weren't any changes to migrations or the schema file, then abort the deploy
    ConditionalDeploy.register :unneeded_migrations, :none_match => ['db/schema.rb', 'db/migrate'] do
      context.send :abort, "You're running migrations, but it doesn't look like you need to!"
    end
  else # If NOT running deploy:migrations
    # If there were changes to migration files, run migrations as part of the deployment
    ConditionalDeploy.register :forgotten_migrations, :any_match => ['db/schema.rb', 'db/migrate'], :msg => "Forgot to run migrations? It's cool, we'll do it for you." do
      context.after "deploy:update_code", "deploy:migrate"
    end  
  end    
end

.register(name, opts, &block) ⇒ Object



10
11
12
13
# File 'lib/capistrano-conditional/deploy.rb', line 10

def self.register(name, opts, &block)
  raise("Already added a conditional with that name") if @@conditionals.any?{|c| c.name == name}
  @@conditionals << Capistrano::Conditional::Unit.new(name, opts, block)
end

Instance Method Details

#ensure_local_up_to_dateObject



51
52
53
54
55
56
57
58
59
# File 'lib/capistrano-conditional/deploy.rb', line 51

def ensure_local_up_to_date
  return true if ENV['ALLOW_UNCOMMITTED']
  s = @git.status
  no_changes = %w(changed added deleted).all? { |attrib| s.send(attrib).empty? }

  unless no_changes
    abort "\nYour working copy contains local changes not yet committed to git. \nPlease commit all changes before deploying.\n\n"
  end
end

#log(text = "\n", level = Capistrano::Logger::TRACE) ⇒ Object



62
63
64
# File 'lib/capistrano-conditional/deploy.rb', line 62

def log(text = "\n", level = Capistrano::Logger::TRACE)
  @logger.log(level, text, "Conditional")
end

#report_planObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/capistrano-conditional/deploy.rb', line 61

def report_plan
  def log(text = "\n", level = Capistrano::Logger::TRACE)
    @logger.log(level, text, "Conditional")
  end
  
  log
  log "Conditional Deployment Report:", Capistrano::Logger::IMPORTANT
  log
  log "\tLast deployed commit: #{@last_deployed.message}", Capistrano::Logger::DEBUG
  log
  log "\tFiles Modified:", Capistrano::Logger::DEBUG
  @changed.each {|f| log "\t\t- #{f}"}
  log
  log "\tConditional Runlist:", Capistrano::Logger::DEBUG
  if @to_run.empty?
    log "\t\t* No conditional tasks have been added"
  else
    @to_run.each do |job|
      out = job.message ? "#{job.name} (#{job.message})" : job.name
      log "\t\t* Running #{out}"
    end
  end
  log
end

#run_conditionalsObject



96
97
98
99
100
# File 'lib/capistrano-conditional/deploy.rb', line 96

def run_conditionals
  @to_run.each do |job|
    job.block.call
  end
end

#screen_conditionalsObject



86
87
88
89
90
91
92
93
94
# File 'lib/capistrano-conditional/deploy.rb', line 86

def screen_conditionals
  @@conditionals.each do |job|
    force = job.name && ENV["RUN_#{job.name.to_s.upcase}"]
    skip  = job.name && ENV["SKIP_#{job.name.to_s.upcase}"]
    next unless force || job.applies?(@changed)
    next if skip
    @to_run << job
  end
end