Class: Starlined::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/starlined/runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRunner

Returns a new instance of Runner.



10
11
12
13
14
# File 'lib/starlined/runner.rb', line 10

def initialize
  @running_instances = 0
  @run_semaphore = Mutex.new
  @animation = nil
end

Instance Attribute Details

#animationObject (readonly)

Returns the value of attribute animation.



8
9
10
# File 'lib/starlined/runner.rb', line 8

def animation
  @animation
end

Instance Method Details

#execute(callback, print_err: true, aka: nil, no_count: false, sudo: false) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/starlined/runner.rb', line 51

def execute(callback, print_err: true, aka: nil, no_count: false, sudo: false)
  handle_sudo if sudo

  start_step(aka: aka)
  result = callback.call
  end_step(aka: aka, no_count: no_count)

  handle_error(result, print_err, aka) unless result.last.success?

  result
end

#pass_stepObject



47
48
49
# File 'lib/starlined/runner.rb', line 47

def pass_step
  @animation&.increment_step
end

#progress(aka: nil, no_count: false) ⇒ Object



40
41
42
43
44
45
# File 'lib/starlined/runner.rb', line 40

def progress(aka: nil, no_count: false)
  start_step(aka: aka)
  yield
ensure
  end_step(aka: aka, no_count: no_count)
end

#run(command, print_err: true, aka: nil, no_count: false) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/starlined/runner.rb', line 28

def run(command, print_err: true, aka: nil, no_count: false)
  needs_sudo = !!(command =~ /^sudo/)

  execute(
    -> { Open3.capture3(command) },
    print_err: print_err,
    aka: aka,
    no_count: no_count,
    sudo: needs_sudo
  )
end

#start(message, steps: 0) ⇒ Object



16
17
18
19
# File 'lib/starlined/runner.rb', line 16

def start(message, steps: 0)
  stop_animation
  @animation = Animation.new(message, steps)
end

#stopObject



21
22
23
24
25
26
# File 'lib/starlined/runner.rb', line 21

def stop
  return unless @animation

  Messages.success(@animation.message, @animation.elapsed_time)
  stop_animation
end