Module: Greenhouse::Tasks::ProjectTask::InstanceMethods

Defined in:
lib/greenhouse/tasks/project_task.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



13
14
15
# File 'lib/greenhouse/tasks/project_task.rb', line 13

def self.included(base)
  base.send :alias_method, :klone, :clone
end

Instance Method Details

#bundle(cmd = 'install') ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/greenhouse/tasks/project_task.rb', line 17

def bundle(cmd='install')
  puts "Running Bundler for \e[36m#{@project.title}\e[0m..."
  @project.bundle(cmd)
  true
rescue Exception => e
  puts "\e[31mError running Bundler for #{@project.title}\e[0m"
  puts "#{e.class.name}: #{e.message}"
  puts e.backtrace
  # TODO? prompt to continue?
  false
end

#cloneObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/greenhouse/tasks/project_task.rb', line 29

def clone
  puts "Cloning \e[36m#{@project.title}\e[0m (#{@project.repository.remote}) into #{@project.path}..."
  @project.repository.clone
    
  # Ignore the project's ignored files
  Bundler.with_clean_env do
    @project.chdir do
      @project.ignored.each { |file| `git update-index --assume-unchanged #{file.to_s} 2>&1` if File.exists?(file.to_s) }
    end
  end
  true
rescue Exception => e
  puts "\e[31mCould not clone #{@project.title}\e[0m"
  puts "#{e.class.name}: #{e.message}"
  puts e.backtrace
  # TODO? prompt to continue?
  false
end

#commit_changesObject



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/greenhouse/tasks/project_task.rb', line 181

def commit_changes
  add_untracked = nil
  if @project.repository.untracked?
    while !%w(a add p prompt s skip).include?(add_untracked) do
      puts
      puts "You have untracked files in your project:"
      puts
      @project.repository.untracked.each do |name,file|
        puts "U   #{@project.path}/#{name}"
      end
      puts
      print "Would you like to add them all, be prompted for each or skip them? ([a]dd/[p]rompt/[s]kip): "
      add_untracked = STDIN.gets.chomp.downcase
    end

    if %w(a add).include?(add_untracked)
      @project.repository.untracked.each do |name,file|
        @project.repository.add(name)
      end
    elsif %w(p prompt).include?(add_untracked)
      @project.repository.untracked.each do |name,file|
        puts
        addfile = nil
        while !%w(a add s skip).include?(addfile) do
          print "Do you want to add #{name} to your commit? ([a]dd/[s]kip): "
          addfile = STDIN.gets.chomp.downcase
        end
        if %w(a add).include?(addfile)
          @project.repository.add(name)
          puts "Added #{name}."
        end
      end
    end
  end

  add_modified = nil
  if @project.repository.unstaged?
    while !%w(a add p prompt s skip).include?(add_modified) do
      puts
      puts "You have modified files in your project:"
      puts
      @project.repository.unstaged.each do |name,file|
        puts "#{file.type}   #{@project.path}/#{name}"
        puts file.sha_index
      end
      puts
      print "Would you like to add them all, be prompted for each or skip them? ([a]dd/[p]rompt/[s]kip): "
      add_modified = STDIN.gets.chomp.downcase
    end

    if %w(a add).include?(add_modified)
      @project.repository.unstaged.each do |name,file|
        @project.repository.add(name)
      end
    elsif %w(p prompt).include?(add_modified)
      @project.repository.unstaged.each do |name,file|
        puts
        addfile = nil
        while !%w(a add s skip).include?(addfile) do
          print "Do you want to add #{name} to your commit? ([a]dd/[s]kip): "
          addfile = STDIN.gets.chomp.downcase
        end
        if %w(a add).include?(addfile)
          @project.repository.add(name)
          puts "Added #{name}."
        end
      end
    end
  end
    
  if ![nil,'s','skip'].include?(add_untracked) || ![nil,'s','skip'].include?(add_modified)
    puts
    puts "Changes to be committed:"
    puts
    @project.repository.staged.each do |name,file|
      print file.untracked ? "U" : file.type
      puts "   #{@project.path}/#{name}"
      puts file.sha_index
    end
    puts
  end

  puts "Enter a commit message (leave blank to skip): "
  message = STDIN.gets.chomp
  return if message.empty?
  
  @project.repository.commit(message)
  return true
end

#indent_spaces(indent = 0) ⇒ Object

TODO move this to a logger class



141
142
143
# File 'lib/greenhouse/tasks/project_task.rb', line 141

def indent_spaces(indent=0)
  indent.times.map {" "}.join
end


145
146
147
148
149
150
151
152
153
154
# File 'lib/greenhouse/tasks/project_task.rb', line 145

def print_local_changes(indent=0)
  puts "#{indent_spaces indent}\e[33mYou have uncommitted changes in #{@project.title}!\e[0m"
  puts "#{indent_spaces indent}The following files have uncommitted local modifications:"
  puts
  @project.repository.changes.each do |name,file|
    print "#{indent_spaces indent}#{file.untracked ? "U" : file.type}"
    puts "   #{@project.path}/#{name}"
  end
  puts
end


168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/greenhouse/tasks/project_task.rb', line 168

def print_out_of_sync_branches(indent=0)
  puts "#{indent_spaces indent}\e[33mYou have out of sync branches in #{@project.title}\e[0m"
  puts
  @project.repository.behind.each do |branch|
    puts "#{indent_spaces indent}    \e[37mbranch\e[0m #{branch[0].name} \e[37mis behind\e[0m #{branch[1].name}/#{branch[0].name}"
  end

  @project.repository.diverged.each do |branch|
    puts "#{indent_spaces indent}    \e[37mbranch\e[0m #{branch[0].name} \e[37mand\e[0m #{branch[1].name}/#{branch[0].name} \e[37mhave diverged\e[0m"
  end
  puts
end


156
157
158
159
160
161
162
163
164
165
166
# File 'lib/greenhouse/tasks/project_task.rb', line 156

def print_unpushed_branches(indent=0)
  puts "#{indent_spaces indent}\e[33mYou have branches in #{@project.title} that haven't been pushed!\e[0m"
  puts
  @project.repository.ahead.each do |branch|
    puts "#{indent_spaces indent}    branch #{branch[0].name} is ahead of #{branch[1].name}/#{branch[0].name}"
  end
  @project.repository.diverged.each do |branch|
    puts "#{indent_spaces indent}    branch #{branch[0].name} and #{branch[1].name}/#{branch[0].name} have diverged"
  end
  puts
end

#pullObject



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
# File 'lib/greenhouse/tasks/project_task.rb', line 48

def pull
  print "Checking \e[36m#{@project.title}\e[0m git remotes for upstream commits... "
  
  @project.repository.fetch # fetch the latest from remotes
  if @project.repository.out_of_sync?
    puts
    print_out_of_sync_branches
    
    # Un-ignore the project's ignored files before attempting any pulls/merges
    Bundler.with_clean_env do
      @project.chdir do
        @project.ignored.each { |file| `git update-index --no-assume-unchanged #{file.to_s} 2>&1` if File.exists?(file.to_s) }
      end
    end

    merge = nil
    stashed = false
    if @project.repository.changes?(false)
      puts "\e[33mYou have uncommitted local changes in #{@project.path} on branch #{@project.repository.git.branch.name}\e[0m"
      while !['y','yes','n','no'].include?(merge) do
        print "Would you like to stash your changes and merge the latest commits from upstream? ([y]es/[n]o): "
        merge = STDIN.gets.chomp.downcase
      end

      if ['y','yes'].include?(merge)
        puts "Stashing local changes..."
        stashed = @project.repository.git.branch.name
        @project.repository.stash
      end
    else
      while !['y','yes','n','no'].include?(merge) do
        print "Would you like to attempt to merge the latest commits from upstream? ([y]es/[n]o): "
        merge = STDIN.gets.chomp.downcase
      end
    end

    if ['y','yes'].include?(merge)
      @project.repository.out_of_sync.each do |branch|
        print "Attempting to merge #{branch[1].name}/#{branch[0].name} into #{branch[0].name}..."
        @project.repository.git.checkout(branch[0].name)
        @project.repository.git.merge("#{branch[1].name}/#{branch[0].name}")
        puts "Success."
      end

      if stashed != false
        puts "Popping local stash..."
        @project.repository.git.checkout(stashed)
        @project.repository.pop_stash
      end
      
    end
    
    # Ignore the project's ignored files
    Bundler.with_clean_env do
      @project.chdir do
        @project.ignored.each { |file| `git update-index --assume-unchanged #{file.to_s} 2>&1` if File.exists?(file.to_s) }
      end
    end
    
    return true if ['y','yes'].include?(merge)
  else
    puts "Already up-to-date."
  end
end

#purge(force = false) ⇒ Object



293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/greenhouse/tasks/project_task.rb', line 293

def purge(force=false)
  return unless @project.exists?

  @project.repository.fetch # fetch the latest from remotes
  if @project.repository.changes? || @project.repository.ahead? || @project.repository.diverged?
    
    # Prompt to take action if there are local changes
    if @project.repository.changes?
      print_local_changes
      
      puts "You can skip this project, commit your changes now or remove the project (and lose your changes)."
      commit = nil
      while !%w(c commit s skip r remove).include?(commit) do
        print "What would you like to do? ([c]ommit/[s]kip/[r]emove): "
        commit = STDIN.gets.chomp.downcase
      end

      if %w(s skip).include?(commit)
        puts "\e[33mSkipping #{@project.title}...\e[0m"
        return
      elsif %w(c commit).include?(commit)
        begin
          raise "Could not commit local changes" unless commit_changes
        rescue
          puts "\e[33mThere was a problem committing local changes to #{@project.title}\e[0m"
          puts "\e[33mSkipping #{@project.title}...\e[0m"
          return
        end
      end
    end

    # Prompt to take action if there are unpushed branches
    if @project.repository.ahead? || @project.repository.diverged?
      print_unpushed_branches

      puts "You can skip this project, push your branches now or remove the project (and lose your changes)."
      push = nil
      while !%w(p push s skip r remove).include?(push) do
        print "What would you like to do? ([p]ush/[s]kip/[r]emove): "
        push = STDIN.gets.chomp.downcase
      end

      if %w(s skip).include?(push)
        puts "\e[33mSkipping #{@project.title}...\e[0m"
        return
      elsif %w(p push).include?(push)
        begin
          raise "Cound not push local branches" unless push_branches
        rescue
          puts "\e[33mThere was a problem pushing local branches for #{@project.title}\e[0m"
          puts "You may manually resolve conflicts in #{@project.path} and try again."
          puts "\e[33mSkipping #{@project.title}...\e[0m"
          return
        end
      end
    end
  
  end
  
  puts "\e[33mRemoving #{@project.title} project directory...\e[0m"

  Projects::procfile.processes.delete_if do |key,process|
    # this is sort of generic, just checks for the project name in the key/cmd
    key.match(/\A.*#{@project.name}.*\Z/) || process.command.match(/\A.*#{@project.name}.*\Z/)
  end
  Projects::procfile.write

  @project.destroy
end

#pushObject



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
# File 'lib/greenhouse/tasks/project_task.rb', line 113

def push
  if @project.repository.ahead? || @project.repository.diverged?
    print_unpushed_branches

    print "Would you like to push these branches now? ([P]ush/[s]kip): "
    push = STDIN.gets.chomp.downcase

    if %w(s skip).include?(push)
      puts "Skipped #{@project.title}"
      return
    else
      begin
        raise "Cound not push local branches" unless push_branches
      rescue
        puts "\e[33mThere was a problem pushing local branches for #{@project.title}\e[0m"
        puts "You may manually resolve conflicts in #{@project.path} and try again."
        puts "\e[33mSkipping #{@project.title}...\e[0m"
        return
      end
    end
    
    return true
  else
    puts "Nothing to push for \e[36m#{@project.title}\e[0m."
  end
end

#push_branchesObject



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/greenhouse/tasks/project_task.rb', line 271

def push_branches
  @project.repository.out_of_sync.each do |branch|
    begin
      print "Attempting to merge #{branch[1].name}/#{branch[0].name} into #{branch[0].name} before pushing..."
      @project.repository.git.checkout(branch[0].name)
      @project.repository.git.merge("#{branch[1].name}/#{branch[0].name}")
      puts "\e[32mSuccess.\e[0m"
    rescue
      # TODO detect unmerged files, allow to resolve inline?
      puts "\e[31mFailed! Unresolved conflicts.\e[0m"
      return false
    end
  end

  print "Pushing local branches..."
  @project.repository.branches.local.each do |branch|
    @project.repository.push('origin', branch.name)
  end
  puts "\e[32mSuccess.\e[0m"
  return true
end