summaryrefslogtreecommitdiff
path: root/lib/jive/batch_runner.rb
blob: 48be03389d01870bc13679d506457efd9294a5b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
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
# frozen_string_literal: true

module Jive
  class BatchRunner
    attr_reader :runner, :stdout

    def initialize(runner: Runner.new, stdout: $stdout)
      @runner = runner
      @stdout = stdout
    end

    def run(tasks)
      stream_output_for(runner, tasks)
      stdout.puts "==================================================="
      print_result_for(runner)
    end

    private

    def stream_output_for(runner, tasks)
      runner.run(tasks) do |command, &run|
        stdout.puts
        stdout.puts "$ #{command.join(" ")}"
        result = run.call
        stdout.print result.stdout
        stdout.print result.stderr
        stdout.puts "==> Finished in #{result.duration} seconds"
        stdout.puts
      end
    end

    def print_result_for(runner)
      if runner.all_success_and_clean?
        stdout.puts "Passed successfully."
        0
      elsif runner.all_success?
        stdout.puts "Passed successfully, but we have warnings:"
        stdout.puts
        emit_warnings_for(runner)
        2
      else
        stdout.puts "Something failed:"
        emit_warnings_for(runner)
        emit_errors_for(runner)
        1
      end
    end

    def emit_warnings_for(runner)
      runner.warned_results.each do |result|
        stdout.puts
        stdout.puts "**** #{result.command.join(" ")} had the following warning(s):"
        stdout.puts
        stdout.puts result.stderr
        stdout.puts
      end
    end

    def emit_errors_for(runner)
      runner.failed_results.each do |result|
        stdout.puts
        stdout.puts "**** #{result.command.join(" ")} failed with the following error(s):"
        stdout.puts
        stdout.puts result.stdout
        stdout.puts result.stderr
        stdout.puts
      end
    end
  end
end