Class: MilkTea::CLI

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

Defined Under Namespace

Classes: TestResult

Constant Summary collapse

GENERATED_FILE_HEADER_PREFIX =
"# generated by mtc".freeze
COMMANDS =

Single source of truth for the top-level command set: used by the help overview and shell-completion generation so they cannot drift.

[
  ["check",       "Type-check and lint source files"],
  ["build",       "Compile a source file or package"],
  ["run",         "Build and execute a program"],
  ["run-module",  "Resolve, build, and run a module by name"],
  ["test",        "Discover and run @[test] functions"],
  ["new",         "Scaffold a new package"],
  ["format",      "Format source files in place or check formatting"],
  ["lint",        "Lint source files and report warnings"],
  ["lex",         "Print the lexer token stream"],
  ["parse",       "Parse source and print the AST"],
  ["lower",       "Lower source to IR and print it"],
  ["emit-c",      "Compile source to C and print it"],
  ["debug",       "Print tokens, AST, facts, and diagnostics"],
  ["deps",        "Manage package dependencies"],
  ["toolchain",   "Manage the native toolchain"],
  ["bindgen",     "Generate a binding module from a C header"],
  ["cache",       "Inspect and manage the build cache"],
  ["docs",        "Serve the local documentation site"],
  ["snapshot",    "Render a highlighted HTML snapshot"],
  ["lsp",         "Start the Language Server Protocol server"],
  ["dap",         "Start the Debug Adapter Protocol server"],
  ["completions", "Print a shell completion script"],
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv, out:, err:, source_overrides: nil, build_frontend: nil) ⇒ CLI

Returns a new instance of CLI.



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/milk_tea/tooling/cli.rb', line 46

def initialize(argv, out:, err:, source_overrides: nil, build_frontend: nil)
  @argv = argv.dup
  @out = out
  @err = err
  @include_module_roots = []
  @include_path_flags = []
  @ambient_module_roots = MilkTea::ModuleRoots.roots_for_path(Dir.pwd)
  @source_overrides = normalize_source_overrides(source_overrides)
  @build_frontend = build_frontend
  @color = :auto
  @verbose = false
  @quiet = false
end

Class Method Details

.start(argv = ARGV, out: $stdout, err: $stderr, source_overrides: nil, build_frontend: nil) ⇒ Object



42
43
44
# File 'lib/milk_tea/tooling/cli.rb', line 42

def self.start(argv = ARGV, out: $stdout, err: $stderr, source_overrides: nil, build_frontend: nil)
  new(argv, out:, err:, source_overrides:, build_frontend:).start
end

Instance Method Details

#startObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
# File 'lib/milk_tea/tooling/cli.rb', line 60

def start
  return 1 unless extract_global_options!

  extract_include_paths!
  command = @argv.shift

  if version_request?(command)
    @out.puts("mtc #{MilkTea::VERSION}")
    return 0
  end

  if help_request?(command)
    topic = command == "help" ? @argv.shift : nil
    if topic
      print_command_help(topic, @out)
    else
      print_help(@out)
    end
    return 0
  end

  if command && @include_module_roots.any? && !command_supports_include_paths?(command)
    @err.puts("unknown option #{@include_path_flags.first} for #{command}")
    print_usage(@err)
    return 1
  end

  if @argv.first == "--help" || @argv.first == "-h"
    @argv.shift
    print_command_help(command, @out)
    return 0
  end

  case command
  when "lex"
    lex_command
  when "parse"
    parse_command
  when "format"
    format_command
  when "lint"
    lint_command
  when "check"
    check_command
  when "lower"
    lower_command
  when "emit-c"
    emit_c_command
  when "build"
    build_command
  when "test"
    test_command
  when "run"
    run_command
  when "run-module"
    app_command
  when "new"
    new_command
  when "debug"
    debug_command
  when "toolchain"
    toolchain_command
  when "deps"
    deps_command
  when "bindgen"
    bindgen_command
  when "cache"
    cache_command
  when "docs"
    docs_command
  when "snapshot"
    snapshot_command
  when "lsp"
    lsp_command
  when "dap"
    dap_command
  when "completions"
    completions_command
  else
    print_usage(@err)
    1
  end
rescue StandardError => e
  raise unless handled_cli_error?(e)

  @err.puts(ErrorFormatter.format(e, color: error_color?(@err)))
  1
end