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



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/cap_gun/presenter.rb', line 115

def body
<<-EOL
#{summary}
#{comment}
Deployment details
==================
Current Branch: #{capistrano[:current_revision]}

Release Revision: #{capistrano[:latest_revision]}
Previous Release Revision: #{capistrano[:previous_revision]}

Repository: #{capistrano[:repository]}
Deploy path: #{capistrano[:deploy_to]}
Domain: #{capistrano[:domain]}
#{git_details}
EOL
end

#branchObject



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

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

#commentObject



111
112
113
# File 'lib/cap_gun/presenter.rb', line 111

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.



84
85
86
87
88
89
# File 'lib/cap_gun/presenter.rb', line 84

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



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

def current_user
  Etc.getlogin
end

#deployed_toObject



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

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

#email_prefixObject



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

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

#exit_codeObject



63
64
65
# File 'lib/cap_gun/presenter.rb', line 63

def exit_code
  $?
end

#fromObject



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

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

#git_detailsObject



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

def git_details
  return unless capistrano[:scm].to_sym == :git
  <<-EOL
#{branch}
#{git_log}
  EOL
  rescue
    nil
end

#git_logObject



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

def git_log
  "\nCommits since last release\n====================\n#{git_log_messages}"
end

#git_log_messagesObject



58
59
60
61
# File 'lib/cap_gun/presenter.rb', line 58

def git_log_messages
  messages = `git log #{capistrano[:previous_revision]}..#{capistrano[:latest_revision]} --pretty=format:%h:%s`
  exit_code.success? ? messages : "N/A"
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.



70
71
72
73
74
75
76
# File 'lib/cap_gun/presenter.rb', line 70

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



91
92
93
# File 'lib/cap_gun/presenter.rb', line 91

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

#local_timezoneObject



95
96
97
# File 'lib/cap_gun/presenter.rb', line 95

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

#present_time(time) ⇒ Object



78
79
80
# File 'lib/cap_gun/presenter.rb', line 78

def present_time(time)
  time.strftime("%B #{time.day.ordinalize}, %Y %l:%M %p #{local_timezone}").gsub(/\s+/, ' ').strip
end

#previous_release_timeObject



103
104
105
# File 'lib/cap_gun/presenter.rb', line 103

def previous_release_time 
  humanize_release_time(capistrano[:previous_release])
end

#recipientsObject



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

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

#release_timeObject



99
100
101
# File 'lib/cap_gun/presenter.rb', line 99

def release_time
  present_time(Time.now)
end

#subjectObject



107
108
109
# File 'lib/cap_gun/presenter.rb', line 107

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

#summaryObject



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

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