Class: CreateRailsApp::Runner

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

Overview

Executes commands via the shell.

Supports --dry-run mode, which prints commands instead of running them. Can run multiple commands in sequence (e.g. gem install + rails new).

Examples:

Runner.new.run!(['rails', '_8.1.2_', 'new', 'myapp', '--api'])

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



17
18
19
20
# File 'lib/create_rails_app/runner.rb', line 17

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



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/create_rails_app/runner.rb', line 28

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

  status = if defined?(Bundler)
             Bundler.with_unbundled_env { @system_runner.call(*command) }
           else
             @system_runner.call(*command)
           end
  return true if status.respond_to?(:success?) && status.success?

  code = status.respond_to?(:exitstatus) ? status.exitstatus : nil
  message = 'Command failed'
  message += " (exit #{code})" if code
  message += ": #{command.shelljoin}"
  raise Error, message
end