Class: Pakyow::Support::CLI::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/pakyow/support/cli/runner.rb

Constant Summary collapse

SPINNER =
:dots
FAILURE_MARK =
""
SUCCESS_MARK =
""
FAILURE_MESSAGE =
"failed"
SUCCESS_MESSAGE =
""

Instance Method Summary collapse

Constructor Details

#initialize(message:) ⇒ Runner

Returns a new instance of Runner.



20
21
22
23
24
25
26
27
28
29
# File 'lib/pakyow/support/cli/runner.rb', line 20

def initialize(message:)
  @spinner = TTY::Spinner.new(
    Support::CLI.style.bold(":spinner #{message}"),
    format: SPINNER,
    success_mark: SUCCESS_MARK,
    error_mark: FAILURE_MARK
  )

  @succeeded = @failed = false
end

Instance Method Details

#completed?Boolean

Returns ‘true` if the command has completed.

Returns:

  • (Boolean)


77
78
79
# File 'lib/pakyow/support/cli/runner.rb', line 77

def completed?
  succeeded? || failed?
end

#failed(output = "") ⇒ Object

Called when the command fails.



67
68
69
70
71
72
73
# File 'lib/pakyow/support/cli/runner.rb', line 67

def failed(output = "")
  unless completed?
    @failed = true
    @spinner.error(Support::CLI.style.red(FAILURE_MESSAGE))
    puts indent_output(output) unless output.empty?
  end
end

#failed?Boolean

Returns ‘true` if the command has completed unsuccessfully.

Returns:

  • (Boolean)


89
90
91
# File 'lib/pakyow/support/cli/runner.rb', line 89

def failed?
  @failed == true
end

#run(*command) ⇒ Object

Runs a command or block of code. If a value for ‘command` is passed with the block, the result will be yielded to the block on success.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/pakyow/support/cli/runner.rb', line 34

def run(*command)
  @spinner.auto_spin

  if command.empty? && block_given?
    yield self
    succeeded
  else
    result = TTY::Command.new(printer: :null, pty: true).run!(*command)

    if result.failure?
      failed(result.err)
    else
      succeeded

      if block_given?
        yield result
      end
    end
  end
end

#succeeded(output = "") ⇒ Object

Called when the command succeeds.



57
58
59
60
61
62
63
# File 'lib/pakyow/support/cli/runner.rb', line 57

def succeeded(output = "")
  unless completed?
    @succeeded = true
    @spinner.success(Support::CLI.style.green(SUCCESS_MESSAGE))
    puts indent_output(output) unless output.empty?
  end
end

#succeeded?Boolean

Returns ‘true` if the command has completed successfully.

Returns:

  • (Boolean)


83
84
85
# File 'lib/pakyow/support/cli/runner.rb', line 83

def succeeded?
  @succeeded == true
end