Class: AllInRuby::CLI

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

Constant Summary collapse

COMMANDS =
%w[install uninstall status].freeze
STATUS_LABELS =
{
  created: "created",
  updated: "instructions added",
  already_installed: "already installed, left untouched",
  removed: "instructions removed",
  not_installed: "not installed",
  installed: "installed",
  manual: "no global rules file; use Cursor Settings > Rules"
}.freeze
DRY_RUN_LABELS =
STATUS_LABELS.merge(
  created: "would be created",
  updated: "instructions would be added",
  already_installed: "would already be covered, no change",
  removed: "instructions would be removed",
  not_installed: "not installed, nothing to remove"
).freeze

Instance Method Summary collapse

Constructor Details

#initialize(stdout: $stdout, stderr: $stderr, installer: Installer.new) ⇒ CLI

Returns a new instance of CLI.



27
28
29
30
31
# File 'lib/all_in_ruby/cli.rb', line 27

def initialize(stdout: $stdout, stderr: $stderr, installer: Installer.new)
  @stdout = stdout
  @stderr = stderr
  @installer = installer
end

Instance Method Details

#run(argv) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/all_in_ruby/cli.rb', line 33

def run(argv)
  options = { agents: [], scopes: [], dry_run: false, backup: true }
  parser = build_parser(options)
  parser.parse!(argv)

  command = argv.shift
  unless COMMANDS.include?(command)
    @stderr.puts parser
    return 1
  end

  agents = options[:agents].empty? ? Installer::AGENTS.dup : options[:agents]
  scopes = scopes_for(command, options[:scopes])
  dry_run = options[:dry_run] && command != "status"

  scope_results = scopes.map do |scope|
    results =
      if command == "status"
        @installer.status(agents: agents, scope: scope)
      else
        @installer.public_send(command, agents: agents, scope: scope, dry_run: dry_run, backup: options[:backup])
      end
    [scope, results]
  end

  print_statuses(scope_results, dry_run ? DRY_RUN_LABELS : STATUS_LABELS)

  results = scope_results.flat_map { |_scope, scope_result| scope_result }
  results.each do |result|
    next unless result.diff

    @stdout.puts
    @stdout.puts result.diff
  end

  print_backups(results, dry_run)

  if command == "install" && results.any? { |result| result.status == :manual }
    @stdout.puts
    @stdout.puts "Cursor has no global rules file. To apply the instruction globally,"
    @stdout.puts "paste this into Cursor Settings > Rules:"
    @stdout.puts
    @stdout.puts Instruction.body
  end
  0
rescue OptionParser::ParseError => e
  @stderr.puts e.message
  1
end