Class: FastlaneCore::CommandExecutor

Inherits:
Object
  • Object
show all
Defined in:
lib/fastlane_core/command_executor.rb

Overview

Executes commands and takes care of error handling and more

Class Method Summary collapse

Class Method Details

.execute(command: nil, print_all: false, print_command: true, error: nil, prefix: nil, loading: nil) ⇒ String

Returns All the output as string.

Parameters:

  • command (String) (defaults to: nil)

    The command to be executed

  • print_all (Boolean) (defaults to: false)

    Do we want to print out the command output while running?

  • print_command (Boolean) (defaults to: true)

    Should we print the command that’s being executed

  • error (Block) (defaults to: nil)

    A block that’s called if an error occurs

  • prefix (Array) (defaults to: nil)

    An array containg a prefix + block which might get applied to the output

  • loading (String) (defaults to: nil)

    A loading string that is shown before the first output

Returns:

  • (String)

    All the output as string



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
# File 'lib/fastlane_core/command_executor.rb', line 12

def execute(command: nil, print_all: false, print_command: true, error: nil, prefix: nil, loading: nil)
  print_all = true if $verbose
  prefix ||= {}

  output = []
  command = command.join(" ") if command.kind_of?(Array)
  Helper.log.info command.yellow.strip if print_command

  if print_all and loading # this is only used to show the "Loading text"...
    system("clear")
    puts loading.cyan
  end

  begin
    PTY.spawn(command) do |stdin, stdout, pid|
      stdin.each do |l|
        line = l.strip # strip so that \n gets removed
        output << line

        next unless print_all

        line = line.cyan

        # Prefix the current line with a string
        prefix.each do |element|
          line = element[:prefix] + line if element[:block] && element[:block].call(line)
        end

        # The actual output here, first clear and then print out 3 lines
        system("clear")
        puts line
      end
      Process.wait(pid)
      system("clear")
    end
  rescue => ex
    # This could happen when the environment is wrong:
    # > invalid byte sequence in US-ASCII (ArgumentError)
    output << ex.to_s
    o = output.join("\n")
    puts o
    error.call(o, nil)
  end

  # Exit status for build command, should be 0 if build succeeded
  status = $?.exitstatus
  if status != 0
    o = output.join("\n")
    puts o # the user has the right to see the raw output
    Helper.log.info "Exit status: #{status}"
    error.call(o, status)
  end

  return output.join("\n")
end