Class: GitBundle::Commands::Generic

Inherits:
Object
  • Object
show all
Includes:
GitBundle::Console
Defined in:
lib/git_bundle/commands/generic.rb

Constant Summary

Constants included from GitBundle::Console

GitBundle::Console::COLORS

Instance Method Summary collapse

Methods included from GitBundle::Console

#clear_line, #puts_attention, #puts_error, #puts_heading, #puts_prompt, #puts_repo_heading

Constructor Details

#initialize(repositories, args) ⇒ Generic

Returns a new instance of Generic.



6
7
8
9
# File 'lib/git_bundle/commands/generic.rb', line 6

def initialize(repositories, args)
  @repositories = repositories
  @args = args
end

Instance Method Details

#invokeObject



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
# File 'lib/git_bundle/commands/generic.rb', line 16

def invoke
  repository_results = @repositories.map {|repo| {repository: repo, output: '', error: false, complete: false, thread: nil}}

  @repositories.each_with_index do |repo, index|
    results = repository_results[index]
    results[:thread] = Thread.new do
      results[:output] = repo.execute_git(@args, color: true)
      results[:error] = true unless $?.exitstatus == 0
    end
  end

  waiting_for_repositories = @repositories.map(&:name)
  print_wait_line(waiting_for_repositories)

  until waiting_for_repositories.empty?
    repository_results.each do |result|
      next if result[:complete]

      if result[:thread].join(0.1)
        result[:complete] = true
        clear_line

        puts_repo_heading(result[:repository])
        puts result[:output]

        waiting_for_repositories.delete(result[:repository].name)
        print_wait_line(waiting_for_repositories) unless waiting_for_repositories.empty?
      end
    end
  end

  puts ''
  errors = repository_results.select {|r| r[:error]}.map {|r| r[:repository].name}
  puts_error "Command failed in #{errors.join(', ')}." unless errors.empty?
end


11
12
13
14
# File 'lib/git_bundle/commands/generic.rb', line 11

def print_wait_line(names)
  STDOUT.print "Waiting for #{names.map {|name| colorize(name, COLORS[:highlight], true)}.join(', ')}"
  STDOUT.flush
end