Class: CapGun::Presenter

Inherits:
Object
  • Object
show all
Defined in:
lib/cap_gun/presenter.rb

Constant Summary collapse

DEFAULT_SENDER =
%("CapGun" <[email protected]>)
DEFAULT_EMAIL_PREFIX =
"[DEPLOY]"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(capistrano) ⇒ Presenter

Returns a new instance of Presenter.



10
11
12
# File 'lib/cap_gun/presenter.rb', line 10

def initialize(capistrano)
  self.capistrano = capistrano
end

Instance Attribute Details

#capistranoObject

Returns the value of attribute capistrano.



8
9
10
# File 'lib/cap_gun/presenter.rb', line 8

def capistrano
  @capistrano
end

Instance Method Details

#bodyObject



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/cap_gun/presenter.rb', line 125

def body
  body = "#{summary}\n"
  body << "#{comment}\n"
  body << "Deployment details\n"
  body << "====================\n"
  body << "Release: #{capistrano[:current_release]}\n"
  body << "Release Time: #{release_time}\n"
  body << "Release Revision: #{capistrano[:current_revision]}\n"
  body << "\n"
  body << "Previous Release: #{capistrano[:previous_release]}\n"
  body << "Previous Release Time: #{previous_release_time}\n"
  body << "Previous Release Revision: #{previous_revision}\n"
  body << "\n"
  body << "Repository: #{repository}\n"
  body << "Deploy path: #{capistrano[:deploy_to]}\n"
  body << "Domain: #{capistrano[:domain]}\n" if capistrano[:domain]
  body << "#{scm_details}\n"
  body
end

#branchObject



39
40
41
# File 'lib/cap_gun/presenter.rb', line 39

def branch
  "Branch: #{capistrano[:branch]}" unless capistrano[:branch].nil? || capistrano[:branch].empty?
end

#commentObject



117
118
119
# File 'lib/cap_gun/presenter.rb', line 117

def comment
  "Comment: #{capistrano[:comment]}.\n" if capistrano[:comment]
end

#convert_from_utc(timestamp) ⇒ Object

Use some DateTime magicrey to convert UTC to the current time zone When the whole world is on Rails 2.1 (and therefore new ActiveSupport) we can use the magic timezone support there.



86
87
88
89
90
91
# File 'lib/cap_gun/presenter.rb', line 86

def convert_from_utc(timestamp)
  # we know Capistrano release timestamps are UTC, but Ruby doesn't, so make it explicit
  utc_time = timestamp << "UTC"
  datetime = DateTime.parse(utc_time)
  datetime.new_offset(local_datetime_zone_offset)
end

#current_userObject



26
27
28
# File 'lib/cap_gun/presenter.rb', line 26

def current_user
  Etc.getlogin
end

#deployed_toObject



34
35
36
37
# File 'lib/cap_gun/presenter.rb', line 34

def deployed_to
  return "deployed to #{capistrano[:rails_env]}" if capistrano[:rails_env]
  "deployed"
end

#email_prefixObject



18
19
20
# File 'lib/cap_gun/presenter.rb', line 18

def email_prefix
  capistrano[:cap_gun_email_envelope][:email_prefix] || DEFAULT_EMAIL_PREFIX
end

#exit_codeObject



69
70
71
# File 'lib/cap_gun/presenter.rb', line 69

def exit_code
  $?
end

#fromObject



22
23
24
# File 'lib/cap_gun/presenter.rb', line 22

def from
  capistrano[:cap_gun_email_envelope][:from] || DEFAULT_SENDER
end

#humanize_release_time(path) ⇒ Object

Gives you a prettier date/time for output from the standard Capistrano timestamped release directory. This assumes Capistrano uses UTC for its date/timestamped directories, and converts to the local machine timezone.



76
77
78
79
80
81
82
# File 'lib/cap_gun/presenter.rb', line 76

def humanize_release_time(path)
  return unless path
  match = path.match(/(\d+)$/)
  return unless match
  local = convert_from_utc(match[1])
  local.strftime("%B #{local.day.ordinalize}, %Y %l:%M %p #{local_timezone}").gsub(/\s+/, ' ').strip
end

#local_datetime_zone_offsetObject



93
94
95
# File 'lib/cap_gun/presenter.rb', line 93

def local_datetime_zone_offset
  @local_datetime_zone_offset ||= DateTime.now.offset
end

#local_timezoneObject



97
98
99
# File 'lib/cap_gun/presenter.rb', line 97

def local_timezone
  @current_timezone ||= Time.now.zone
end

#previous_release_timeObject



109
110
111
# File 'lib/cap_gun/presenter.rb', line 109

def previous_release_time
  humanize_release_time(capistrano[:previous_release])
end

#previous_revisionObject



105
106
107
# File 'lib/cap_gun/presenter.rb', line 105

def previous_revision
  capistrano.fetch(:previous_revision, "n/a")
end

#recipientsObject



14
15
16
# File 'lib/cap_gun/presenter.rb', line 14

def recipients
  capistrano[:cap_gun_email_envelope][:recipients]
end

#release_timeObject



101
102
103
# File 'lib/cap_gun/presenter.rb', line 101

def release_time
  humanize_release_time(capistrano[:current_release])
end

#repositoryObject



121
122
123
# File 'lib/cap_gun/presenter.rb', line 121

def repository
  capistrano[:repository_url] || capistrano[:repository]
end

#scm_detailsObject



43
44
45
46
47
48
49
50
51
# File 'lib/cap_gun/presenter.rb', line 43

def scm_details
  return unless [:git,:subversion].include? capistrano[:scm].to_sym
  <<-EOL
#{branch}
#{scm_log}
  EOL
  rescue
    nil
end

#scm_logObject



53
54
55
# File 'lib/cap_gun/presenter.rb', line 53

def scm_log
  "\nCommits since last release\n====================\n#{scm_log_messages}"
end

#scm_log_messagesObject



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/cap_gun/presenter.rb', line 57

def scm_log_messages
  messages = case capistrano[:scm].to_sym
    when :git
      `git log #{previous_revision}..#{capistrano[:current_revision]} --pretty=format:%h:%s`
    when :subversion
      `svn log -r #{previous_revision.to_i+1}:#{capistrano[:current_revision]}`
    else
      "N/A"
  end
  exit_code.success? ? messages : "N/A"
end

#subjectObject



113
114
115
# File 'lib/cap_gun/presenter.rb', line 113

def subject
  "#{email_prefix} #{capistrano[:application]} #{deployed_to}"
end

#summaryObject



30
31
32
# File 'lib/cap_gun/presenter.rb', line 30

def summary
  %[#{capistrano[:application]} was #{deployed_to} by #{current_user} at #{release_time}.]
end