Class: CreateRailsApp::CLI

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

Overview

Main entry point that orchestrates the entire create-rails-app flow.

Parses CLI flags, detects installed Rails versions, runs the interactive wizard (or loads a preset), builds the rails new command, and executes it. All collaborators are injected via constructor for testability.

Examples:

Run from the command line

CreateRailsApp::CLI.start(ARGV)

Defined Under Namespace

Classes: VersionChoice

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv:, out:, err:, store:, detector:, rails_detector:, runner:, prompter:, palette:) ⇒ CLI

Returns a new instance of CLI.

Parameters:

  • command-line arguments

  • standard output stream

  • standard error stream

  • configuration persistence

  • runtime version detector

  • Rails version detector

  • command runner

  • user interaction handler

  • color palette for terminal output



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/create_rails_app/cli.rb', line 64

def initialize(argv:, out:, err:, store:, detector:, rails_detector:, runner:, prompter:, palette:)
  @argv = argv.dup
  @out = out
  @err = err
  @store = store
  @detector = detector
  @rails_detector = rails_detector
  @runner = runner
  @prompter = prompter
  @palette = palette || UI::Palette.new
  @options = {}
end

Class Method Details

.start(argv = ARGV, out: $stdout, err: $stderr, store: Config::Store.new, detector: Detection::Runtime.new, rails_detector: Detection::RailsVersions.new, runner: nil, prompter: nil, palette: nil) ⇒ Integer

Constructs and runs a CLI instance with the given arguments.

Parameters:

  • (defaults to: ARGV)

    command-line arguments

  • (defaults to: $stdout)

    standard output stream

  • (defaults to: $stderr)

    standard error stream

  • (defaults to: Config::Store.new)

    configuration persistence

  • (defaults to: Detection::Runtime.new)

    runtime version detector

  • (defaults to: Detection::RailsVersions.new)

    Rails version detector

  • (defaults to: nil)

    command runner (built from out if nil)

  • (defaults to: nil)

    user interaction handler

  • (defaults to: nil)

    color palette for terminal output

Returns:

  • exit code (0 on success, non-zero on failure)



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/create_rails_app/cli.rb', line 30

def self.start(
  argv = ARGV,
  out: $stdout,
  err: $stderr,
  store: Config::Store.new,
  detector: Detection::Runtime.new,
  rails_detector: Detection::RailsVersions.new,
  runner: nil,
  prompter: nil,
  palette: nil
)
  instance = new(
    argv: argv,
    out: out,
    err: err,
    store: store,
    detector: detector,
    rails_detector: rails_detector,
    runner: runner || Runner.new(out: out),
    prompter: prompter,
    palette: palette
  )
  instance.run
end

Instance Method Details

#runInteger

Runs the CLI: parses flags, dispatches to the appropriate action, and returns an exit code.

Returns:

  • exit code (0 success, 1 error, 130 interrupt)



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/create_rails_app/cli.rb', line 81

def run
  parse_options!

  validate_top_level_flags!

  return print_version if @options[:version]
  return print_help if @options[:help]
  return doctor if @options[:doctor]
  return list_presets if @options[:list_presets]
  return show_preset if @options[:show_preset]
  return delete_preset if @options[:delete_preset]

  validate_preset_name!(@options[:save_preset]) if @options[:save_preset]

  runtime_versions = @detector.detect
  installed_rails = @rails_detector.detect
  version_choice = resolve_rails_version!(installed_rails)
  compatibility_entry = Compatibility::Matrix.for(version_choice.version || "#{version_choice.series}.0")
  builder = CommandBuilder.new

  app_name = resolve_app_name!

  if @options[:minimal]
    Options::Validator.new(compatibility_entry).validate!(app_name: app_name, options: {})

    version_choice = install_and_update_version!(version_choice) if version_choice.needs_install

    command = builder.build(app_name: app_name, rails_version: version_choice.version, options: {}, minimal: true)
    @runner.run!(command, dry_run: @options[:dry_run])
    return 0
  end

  selected_options =
    if @options[:preset]
      load_preset_options!
    else
      run_interactive_wizard!(
        app_name: app_name,
        builder: builder,
        compatibility_entry: compatibility_entry,
        last_used: @store.last_used,
        runtime_versions: runtime_versions,
        version_choice: version_choice
      )
    end

  Options::Validator.new(compatibility_entry).validate!(app_name: app_name, options: selected_options)

  version_choice = install_and_update_version!(version_choice) if version_choice.needs_install

  command = builder.build(
    app_name: app_name,
    rails_version: version_choice.version,
    options: selected_options,
    minimal: false
  )

  @runner.run!(command, dry_run: @options[:dry_run])
  unless @options[:dry_run]
    begin
      @store.save_last_used(selected_options)
      save_preset_if_requested(selected_options)
    rescue ValidationError, ConfigError => e
      @err.puts("Warning: #{e.message}")
    rescue Interrupt
      @err.puts('Skipped preset save.')
    end
  end
  0
rescue OptionParser::ParseError, ArgumentError, Error => e
  @err.puts(e.message)
  1
rescue Interrupt
  @err.puts(palette.color(:exit_message, 'See ya!'))
  130
end