Class: Capistrano::Capflow

Inherits:
Object
  • Object
show all
Defined in:
lib/capistrano/capflow.rb

Class Method Summary collapse

Class Method Details

.load_into(capistrano_configuration) ⇒ Object

include Capistrano::Helpers::CapflowHelper



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/capistrano/capflow.rb', line 12

def self.load_into(capistrano_configuration)

  capistrano_configuration.load do
    before "deploy:update_code", "capflow:calculate_tag"
    before "capflow:calculate_tag", "capflow:verify_up_to_date"

    namespace :capflow do
          
    def who
      identity = (`git config user.name` || `whoami`)
      identity.chomp.to_url
    end

    def tags
      `git tag`.split("\n").compact
    end

    def non_release_tags
      tags - releases 
    end

    def current_branch
      branches.select{|b| b =~ /^\*\s/}.first.gsub(/^\*\s/,"")
    end

    def branches
     `git branch --no-color`.split("\n")
    end

    def version_tag_prefix
      `git config gitflow.prefix.versiontag`.split("\n").first
    end

    def releases
      tags.select{|t| t =~ /^#{version_tag_prefix}(\d+)/}.collect{|version| Versionomy.parse(version) }.sort
    end

    def latest_release
      releases.reverse.first.to_s
    end

    def available_tags
      Capistrano::CLI.ui.say "Available Tags:"
      Capistrano::CLI.ui.say "#{non_release_tags.join("\n")}"
    end

    def available_releases
      Capistrano::CLI.ui.say "\nAvailable Releases:".color :green
      Capistrano::CLI.ui.say "#{releases.sort.reverse.join("\n")}"
    end

    def banner

      "\\nCapflow for Gitflow\n      BANNER\n    end\n\n\n\n      def deploy_from\n        puts banner\n        if stage == :production\n          available_releases\n          from_destination = Capistrano::CLI.ui.ask(\"\\nRelease to deploy:\".color(:yellow).bright) do |q|\n            q.default = latest_release\n          end\n        else\n          create_tag = Capistrano::CLI.ui.agree(\"Do you want to tag deployment? [y/N]\".color(:yellow)) do |q|\n            q.default = 'N'\n          end\n          return next_tag if create_tag\n          available_tags\n          from_destination = Capistrano::CLI.ui.ask \"\\nBranch, tag or release to deploy:\".color(:yellow).bright do |q|\n            q.default = current_branch\n          end\n        end\n        return from_destination\n      end\n\n      def next_tag\n        hwhen   = Date.today.to_s\n        what = Capistrano::CLI.ui.ask(\"What does this release introduce? (this will be normalized and used in the tag for this release)\".color(:yellow)).to_url\n        new_staging_tag = \"\#{hwhen}-\#{who}-\#{what}\"\n        puts \"Tagging current branch for deployment to staging as '\#{new_staging_tag}'\".color(:green)\n        system \"git tag -a -m 'tagging current code for deployment to staging' \#{new_staging_tag}\"\n        return new_staging_tag\n      end\n\n      def using_git?\n        fetch(:scm, :git).to_sym == :git\n      end\n\n      task :verify_up_to_date do\n        if using_git?\n          set :local_branch, `git branch --no-color 2> /dev/null | sed -e '/^[^*]/d'`.gsub(/\\* /, '').chomp\n          set :local_sha, `git log --pretty=format:%H HEAD -1`.chomp\n          set :origin_sha, `git log --pretty=format:%H \#{local_branch} -1`\n          unless local_sha == origin_sha\n            abort \"\"\"\nYour \#{local_branch} branch is not up to date with origin/\#{local_branch}.\nPlease make sure you have pulled and pushed all code before deploying:\n\ngit pull origin \#{local_branch}\n# run tests, etc\ngit push origin \#{local_branch}\n\n\"\"\"\n          end\n        end\n      end\n\n      desc \"Calculate the tag to deploy\"\n      task :calculate_tag do\n        if using_git?\n          # make sure we have any other deployment tags that have been pushed by others so our auto-increment code doesn't create conflicting tags\n          `git fetch`\n          if stage == :production\n              tag_production\n          else\n              tag_staging\n          end\n\n\n            system \"git push --tags origin \#{local_branch}\"\n            if $? != 0\n              abort \"git push failed\"\n            end\n\n        end\n      end\n\n      desc \"Mark the current code as a staging/qa release\"\n      task :tag_staging do\n        #current_sha = `git log --pretty=format:%H HEAD -1`\n        #last_staging_tag_sha = if last_staging_tag\n         #                        `git log --pretty=format:%H \#{last_staging_tag} -1`\n          #                     end\n\n        #if last_staging_tag_sha == current_sha\n        #  puts \"Not re-tagging staging because latest tag (\#{last_staging_tag}) already points to HEAD\"\n        #  new_staging_tag = last_staging_tag\n        #else\n        #\n        staging_destination = deploy_from\n\n        #end\n\n        set :branch, staging_destination\n      end\n\n      desc \"Push the approved tag to production. Pass in tag to deploy with '-s tag=staging-YYYY-MM-DD-X-feature'.\"\n      task :tag_production do\n\n        production_destination = deploy_from\n\n         really_deploy = Capistrano::CLI.ui.ask(\"Do you really want to deploy \#{production_destination}? [y/N]\").to_url\n\n         exit(1) unless really_deploy =~ /^[Yy]$/\n\n        set :branch, production_destination\n      end\n    end\n\n    namespace :deploy do\n      namespace :pending do\n        task :compare do\n          #gitflow.commit_log\n        end\n      end\n    end\n\n  end\n\nend\n"