Class: AppUp::ShellRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/app_up/shell_runner.rb

Constant Summary collapse

MAX_PROCESS_COUNT =
8
CommandFailureError =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(log_path:, working_directory:, verbose: false) ⇒ ShellRunner

Returns a new instance of ShellRunner.



11
12
13
14
15
16
17
18
19
# File 'lib/app_up/shell_runner.rb', line 11

def initialize(log_path:, working_directory:, verbose: false)
  @working_directory = working_directory
  @log_path = log_path
  @queue = WorkQueue.new(MAX_PROCESS_COUNT, nil)
  @verbose = verbose
  Dir.chdir(%x[ git rev-parse --show-toplevel ].chomp)

  reset_log
end

Instance Attribute Details

#log_path=(value) ⇒ Object

Sets the attribute log_path

Parameters:

  • value

    the value to set the attribute log_path to.



8
9
10
# File 'lib/app_up/shell_runner.rb', line 8

def log_path=(value)
  @log_path = value
end

#queue=(value) ⇒ Object

Sets the attribute queue

Parameters:

  • value

    the value to set the attribute queue to.



8
9
10
# File 'lib/app_up/shell_runner.rb', line 8

def queue=(value)
  @queue = value
end

#working_directory=(value) ⇒ Object

Sets the attribute working_directory

Parameters:

  • value

    the value to set the attribute working_directory to.



8
9
10
# File 'lib/app_up/shell_runner.rb', line 8

def working_directory=(value)
  @working_directory = value
end

Instance Method Details

#enqueue(method, *args, &block) ⇒ Object



42
43
44
45
46
# File 'lib/app_up/shell_runner.rb', line 42

def enqueue(method, *args, &block)
  queue.enqueue_b do
    send(method, *args, &block)
  end
end

#flushObject



48
49
50
# File 'lib/app_up/shell_runner.rb', line 48

def flush
  queue.join
end

#notify(msg) ⇒ Object



37
38
39
40
# File 'lib/app_up/shell_runner.rb', line 37

def notify(msg)
  log msg.to_s
  print "#{msg.to_s.yellow}\n"
end

#run(cmd, dir: working_directory, &block) ⇒ Object

The block passed to run is a callback. It is used to add a dependent command to the queue.



23
24
25
26
27
28
29
30
# File 'lib/app_up/shell_runner.rb', line 23

def run(cmd, dir: working_directory, &block)
  command = "cd #{dir} && #{cmd}"
  handle_output_for(command)

  shell_out(command).split("\n").tap do
    block.call if block_given?
  end
end

#warn(msg) ⇒ Object



32
33
34
35
# File 'lib/app_up/shell_runner.rb', line 32

def warn(msg)
  log msg.to_s
  print "#{msg.to_s.red}\n"
end