Class: CreateRubyGem::Runner

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

Overview

Executes the assembled bundle gem command via the shell.

Supports --dry-run mode, which prints the command instead of running it.

Examples:

Runner.new.run!(['bundle', 'gem', 'my_gem', '--exe'])

Instance Method Summary collapse

Constructor Details

#initialize(out: $stdout, system_runner: nil) ⇒ Runner

Returns a new instance of Runner.

Parameters:

  • out (IO) (defaults to: $stdout)

    output stream for dry-run printing

  • system_runner (#call, nil) (defaults to: nil)

    callable that executes a shell command



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

def initialize(out: $stdout, system_runner: nil)
  @out = out
  @system_runner = system_runner || ->(*command) { ::CLI::Kit::System.system(*command) }
end

Instance Method Details

#run!(command, dry_run: false) ⇒ true

Executes the command or prints it in dry-run mode.

Parameters:

  • command (Array<String>)

    the command to execute

  • dry_run (Boolean) (defaults to: false)

    when true, prints instead of executing

Returns:

  • (true)

    on success

Raises:

  • (Error)

    if the command exits with a non-zero status



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

def run!(command, dry_run: false)
  if dry_run
    @out.puts(command.join(' '))
    return true
  end

  status = @system_runner.call(*command)
  return true if status.respond_to?(:success?) && status.success?

  raise Error, "Command failed: #{command.shelljoin}"
end