Class: Rbs::Src::CommandRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/rbs/src/command_runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stdout:) ⇒ CommandRunner

Returns a new instance of CommandRunner.



6
7
8
9
# File 'lib/rbs/src/command_runner.rb', line 6

def initialize(stdout:)
  @stdout = stdout
  @level = 0
end

Instance Attribute Details

#stdoutObject (readonly)

Returns the value of attribute stdout.



4
5
6
# File 'lib/rbs/src/command_runner.rb', line 4

def stdout
  @stdout
end

Instance Method Details

#execute!(*command, chdir: Pathname.pwd) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rbs/src/command_runner.rb', line 26

def execute!(*command, chdir: Pathname.pwd)
  # @type var out: String
  # @type var status: Process::Status

  out, status = Open3.capture2e(*command, chdir: chdir)

  unless status.success?
    puts "🚨 Command failed: #{command.inspect} in #{chdir}..."
    out.each_line do |line|
      puts "| #{line}"
    end
    raise status.inspect
  end
end

#push(message) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/rbs/src/command_runner.rb', line 16

def push(message)
  puts "âŠī¸ #{message}"
  begin
    @level += 1
    yield
  ensure
    @level -= 1
  end
end

#puts(message) ⇒ Object



11
12
13
14
# File 'lib/rbs/src/command_runner.rb', line 11

def puts(message)
  prefix = "  " * @level
  stdout.puts "#{prefix}#{message}"
end

#query!(*command, chdir: Pathname.pwd) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rbs/src/command_runner.rb', line 52

def query!(*command, chdir: Pathname.pwd)
  # @type var out: String
  # @type var status: Process::Status

  out, status = Open3.capture2e(*command, chdir: chdir)

  unless status.success?
    puts "🚨 Command failed: #{command.inspect} in #{chdir}..."
    out.each_line do |line|
      puts "| #{line}"
    end
    raise status.inspect
  end

  out
end

#query?(*command, chdir: Pathname.pwd) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
49
50
# File 'lib/rbs/src/command_runner.rb', line 41

def query?(*command, chdir: Pathname.pwd)
  # @type var out: String
  # @type var status: Process::Status

  out, status = Open3.capture2e(*command, chdir: chdir)

  if status.success?
    out
  end
end