Skip to content

refactor: dispatch ruflet commands through a command registry (WP-00 R2) - #77

Merged
AdamMusa merged 9 commits into
mainfrom
refactor/cli-command-registry
Nov 20, 2026
Merged

AdamMusa merged 9 commits into
mainfrom
refactor/cli-command-registry

Conversation

@AdamMusa

@AdamMusa AdamMusa commented Nov 8, 2026 •

Copy link
Copy Markdown
Owner

WP-00 R2. Based on main (dd924eb3, which has #73, #74, #75 and #70), merged in with a merge commit.

What

Ruflet::CLI.run picked a command with a 30-line case/when, and print_help printed a hand-written heredoc. Both now come from one table, Ruflet::CLI::Commands in cli/commands.rb. Each command in it declares:

Field Meaning Example (build)
name, aliases the words that run it build (new has bootstrap, init)
handler the Ruflet::CLI method that runs it, given the arguments after the word :command_build
usage its line under "Commands:" in ruflet help (nil means not listed) ruflet build <apk|...> [--lite|--full|--self] [--verbose]
options the switches it defines (OptionParser#on strings), or the tokens a hand-parsing command looks for --self, --lite, --full, -v/--verbose
forwards the tool that receives the arguments it does not read flutter build <target>
help the sections ruflet help prints after the command list "Build profiles"
first_run_assets whether the Flutter template is fetched before it runs true

cli.rb keeps only the dispatch:

  • run downcases the first word and looks it up, as before. An unknown word still warns, prints the help and returns 1.
  • print_help renders the table.
  • ensure_first_run_assets asks the table instead of a word list.
  • version and help become two private handlers, command_version and command_help.
  • The extend order (NewCommand, RunCommand, UpdateCommand, BuildCommand, ExtraCommand) is unchanged. test: CLI and page compatibility goldens (WP-00 H1) #73's notes say R2 must keep it.

Registration. cli/commands.rb loads before the command files, so a later command can register itself beside its own code. It passes before: or after: when the end of the list is not the right place, as R1's build contributors do:

Commands.register(
  "logs",
  handler: :command_logs,
  usage: "ruflet logs [-d DEVICE_ID]",
  options: [Commands.option("-d", "--device DEVICE_ID", description: "Device to read")],
  after: "emulators"
)
  • Registering the same command again changes nothing.
  • These raise ArgumentError: a name or alias that is already taken, a word repeated within one command, a word that is not lowercase or has a space, passing both before: and after:, and an unknown position.

Options are declared, not used yet. Each command still parses its own arguments. The declarations do not cover every form the commands accept. commands.rb and the tests record the forms they leave out, which CLI-12 must keep accepting:

  • run, update, debug and emulators parse with OptionParser. It also accepts any unique prefix of a long option, and a single letter that starts exactly one long option. For example, run -d is --desktop (D6 keeps that meaning), debug -d ID is --device-id ID, run -p 9000 is --port 9000, update -c is --check and emulators -s -e ID is --start --emulator ID. The commands read these forms, but the table does not declare them. Built from the declared switches, OptionParser accepts the same forms unless require_exact is set.
  • build, install and doctor look only for the exact tokens they declare. They accept no prefixes and no --flag=value: install --device=ID is not read as a device.

Why

WP-06 and WP-07 add commands and flags (logs, clean, flutter, screenshot, --device, ...), and CLI-12 changes how every command reports a bad flag. With a case statement and a heredoc, each of those edits the same lines in cli.rb. After this PR, a new command is one registration, and its help line, usage and flag list come from the same place.

Proof of zero change

The only files outside packages/ruflet/test/ that differ from main are lib/ruflet/cli.rb and lib/ruflet/cli/commands.rb.

  • H1 goldens, written on main's code first. This branch adds 56 invocations. The first 39 pin the command words and help: mixed-case words, the empty word, version extra, help bogus, create/bootstrap/init with no name, and --help/-h after all 12 commands. The other 17 pin OptionParser's short and prefix forms, run -v and update -v among them.
    • To prove they describe today's behavior, I made a tree with main's library code and this branch's compat harness, corpus and goldens. git diff origin/main shows no change under any packages/*/lib there. A full compat.rb --regenerate in that tree changed none of the existing goldens. It added the new ones; update -v was added later with --only. compat.rb then checked all 290 goldens there: exit 0, 0 changed.
    • On this head, compat.rb checks all 290 goldens: exit 0, 0 changed.
  • The dispatch pin passes on the old code. test/cli_dispatch_test.rb is its own first commit (52b96c99). Run against main's library code, it passes all 9 tests. It checks:
    • every word and alias reaches the same method, with the caller's argv minus the word;
    • words match in any case;
    • the command's result is the exit code;
    • version and help ignore further arguments;
    • an unknown word is reported downcased, with the help and exit 1;
    • which words fetch first-run assets;
    • the help text is byte-for-byte the old heredoc.

Tests

All runs are on this head, 055b27ce.

  • test/cli_commands_test.rb (20 tests, plus the 9 dispatch tests it loads) checks the following.
    • The built-in table: help order, every word and its handler, exact lookup, first_run_assets, the rendered help, forwards, that handlers are Ruflet::CLI methods taking argv, and that the table is frozen.
    • Declarations against the code:
      • For run, update, debug and emulators, the test captures the OptionParser the command builds and compares its summary with one built from the declarations.
      • For build, install and doctor, it records the tokens the command deletes from or extracts out of argv.
      • devices forwards every argument.
    • Completed forms (new): for each of the four OptionParser commands, the test pins every letter -a to -z the command takes and the option that letter selects, plus a few long prefixes. It also checks that none of their parsers sets require_exact.
    • Mutation checks: setting require_exact on run's parser fails the test. So does adding a declared --device switch to run, which makes -d ambiguous.
    • Registration API: positions, idempotence, conflicts, aliases, and help rendering with sections.
  • packages/ruflet, with each test/*_test.rb except compat_goldens_test.rb run in its own process: 19 files, 308 runs, 0 failures, 0 errors and 0 skips. The result is the same on Ruby 4.0.5 and on 3.4.5 (CI's .ruby-version, minitest 5.25.5).
  • ruby -Ilib -Itest test/compat_goldens_test.rb: 298 runs, 0 failures, 0 errors, 0 skips.
  • ruby test/compat/compat.rb --jobs 4: checked 290 goldens, exit 0, with no CHANGED, ERROR, MISSING or STALE lines.
  • Sensitivity checks with --only, each made on a scratch copy and then reverted:
    • Changing one ruflet help row (the devices usage): --only empty/help changes all 5 of its goldens.
    • Setting require_exact on run's parser: --only run-main.rb-- changes 6 of its 16 goldens, exactly -d, -p 9000, -n, -w, --desk and -v.
    • Setting require_exact on debug's parser: --only debug-- changes 4 of 8, namely -h, -d chrome, --dev chrome and -p macos.
    • Dropping the init alias changes init.txt and init-my_app.txt.

Behavior changes

None.

Today's behavior, now pinned (left for CLI-12)

The goldens record these as they are. Changing any of them is CLI-12's job, and it must list the change as a behavior change:

  • ruflet new --help and new -h create a project named --help / -h.
  • run, update, debug and emulators with --help/-h print OptionParser's bare switch list (Usage: ruflet [options]) and exit 0.
  • run -v and update -v reach OptionParser's own --version: they print "ruflet: version unknown" and exit 1. update -v fetches the template first.
  • The OptionParser commands accept the short and prefix forms above. Only run -d is protected by D6, but CLI-12 must not drop any of the others silently.
  • build --help exits 1 with "Unsupported build target: --help".
  • devices --help runs flutter devices --help.
  • install and doctor ignore --help.
  • help build prints the general help, although the gem README says "Run ruflet help <command> for all options".
  • The debug usage line says [scriptname|path], but its first argument is the platform.
  • build with no target prints its own usage line. Its target order (apk|android|aab|ios|ipa|...) differs from the help line's (apk|android|ios|ipa|aab|...).

Not in this PR

  • Nothing parses with the declarations yet. The per-command OptionParser blocks are untouched.
  • extra_command.rb is not split into per-command files. That is for the lead to decide before WP-06 and WP-07.
  • No new command or flag, and no docs change.
  • Size: +10.4 KB of uncompressed gem source. cli/commands.rb adds 11.6 KB, and cli.rb shrinks by 1.1 KB.

Before the case/when in Ruflet::CLI.run becomes a command table, pin
what it does: every command word and alias reaches the same command
method with the caller's remaining argv, words match in any case, the
command's result is the exit code, version and help ignore further
arguments, an unknown word is reported downcased with the help and
exit 1, only create/new/bootstrap/init/update/debug/build/install/doctor
fetch the first-run assets (and a failed fetch never stops them), and
the help text is byte-for-byte the current one.

The commands are recorders on a private copy of the CLI, so the test
runs nothing and does not touch Ruflet::CLI. It passes on the current
code.
39 new H1 invocations pin the command words and help surface the
command registry (R2) and CLI-12 touch:

- mixed-case words (VERSION, -V, --Version, HELP, -H, Bogus-Command,
  CREATE, New my_app), the empty word, and version/help with extra
  arguments;
- create, bootstrap and init without an app name (usage, exit 1, and
  the first-run asset fetch the aliases trigger);
- --help and -h after every command, recording today's behavior as is:
  OptionParser's own summary for run/update/debug/emulators, a project
  named --help for new/create, "Unsupported build target" for build,
  flutter devices --help, and doctor/install ignoring the flag.

Mixed-case invocations get explicit ids so their goldens cannot collide
with the lowercase ones on a case-insensitive filesystem. The child now
sets $0 to the installed executable, as running `ruflet` does, so
OptionParser's usage line says "ruflet" instead of the harness script;
no existing golden changes.
Ruflet::CLI::Commands is to be the table `ruflet` dispatches through and
`ruflet help` renders. The test pins the built-in table against the
dispatch pins (help order, every word and the method it runs, which
commands fetch first-run assets, the rendered help, that only build and
devices forward their remaining arguments), and the registration API
later commands use: before:/after: positions, idempotent re-registration,
and errors for taken names or aliases, repeated or non-lowercase words
and bad positions.

Each command's declared options must be exactly what it reads today:
for run, update, debug and emulators the OptionParser the command builds
is captured and its summary compared with one built from the
declarations; for build, install and doctor the flags they delete or
extract from argv are recorded. devices must forward every argument.
The case/when in Ruflet::CLI.run and the hand-written help heredoc are
replaced by Ruflet::CLI::Commands (cli/commands.rb), an ordered table in
which each command declares its name and aliases, the CLI method that
runs it, its usage line, the options it reads, the tool its remaining
arguments are forwarded to, its help sections and whether it fetches
the first-run assets.

- run downcases the first word and looks it up, as before; an unknown
  word still warns, prints help and returns 1.
- print_help renders the table; the "Build profiles" section is the
  build command's help.
- ensure_first_run_assets asks the table instead of a word list.
- version and help become the private command_version/command_help.

cli/commands.rb loads before the command files, so a later command
(WP-07's logs, clean, ...) registers itself beside its code with
before:/after: instead of editing a shared case statement. The declared
options are not used for parsing yet; they are what CLI-12's
per-command usage and unknown-flag errors are to be built from.

No output changes: the dispatch pins, the rendered help and all 199 H1
goldens are identical.
run, update, debug and emulators parse with OptionParser, which also
accepts a single letter that starts exactly one long option and any
unique prefix of one. None of these forms is declared in the command
table, and only `run -d` had a golden. 17 new H1 invocations record
them as they behave today: run -p 9000, -n, -w, --desk and -v; update
-c, all -c -p linux, all -f and -v; debug -d chrome, --dev chrome,
-p macos, web -r and web -w canvaskit; emulators -c, -d and -s -e ID.
`run -v` and `update -v` reach OptionParser's own --version and exit 1
with "ruflet: version unknown".

The goldens were generated on main's code (dd924eb lib, this
branch's corpus). Regenerating all 290 goldens there added these 17
and changed no existing golden.
The command table's options are the switches a command defines (or,
for build, install and doctor, the tokens they look for), not every
form it reads. run, update, debug and emulators parse with
OptionParser, so they also take a single letter that starts exactly
one long option and any unique prefix of one: `run -d` is --desktop
(D6 keeps that meaning) and `debug -d ID` is --device-id ID.

commands.rb now says so, and that a parser built from the declarations
must keep accepting those forms. A new test pins every letter a-z each
of the four commands takes and the option it selects, a few long
prefixes, and that none of their parsers sets require_exact. Adding a
--device switch to run (which makes -d ambiguous) or setting
require_exact on run's parser fails it.

Comment and test only; no code path changes.
@AdamMusa
AdamMusa merged commit c444cbd into main Nov 20, 2026
12 checks passed
AdamMusa added a commit that referenced this pull request Nov 20, 2026
Conflict in cli/commands.rb's header comment only: main (#77) documents
the forms OptionParser completes beyond the declarations; this branch
documents that help and usage errors are rendered from them. Both kept.
AdamMusa added a commit that referenced this pull request Nov 20, 2026
…review)

Review findings on #78, written against the old behavior first:

- main's pin (#77) says `emulators -c` is --create and `debug -d` and
  `--dev` select --device-id. On this branch -c is ambiguous with --cold
  and the debug option's first long name is --device. The pin helper now
  builds its parser with the hidden switches too, as the switch-list
  check above it does.
- `help --` and `--help --` listed the commands (exit 0); `build -- apk`
  said "Unsupported build target: --". Unit tests and H1 entries for them.
- H1 entries for `emulators --c`, and for `--version -v`, `-v --verbose`
  and `version --anything`, whose change (exit 0 to 64) the next commit
  lists as a behavior change.

The 7 new goldens were generated on main c444cbd (where a full
--regenerate changed no other golden).
AdamMusa added a commit that referenced this pull request Nov 20, 2026
- `ruflet help --` and `--help --` list the commands again (exit 0):
  `--` ends the options, it is not a bad flag.
- `ruflet build -- apk` is again "Unsupported build target: --"; nothing
  after `--` is taken as the build target, so `build --release -- apk`
  does not build either.
- `emulators -c` and `--c` reach --create again: they are hidden
  switches of --create, as run's -d, --d and --de are of --desktop.
- debug declares --device-id first, so -d and --dev select it as main's
  pin says; its help reads "--device-id ID, --device ID".

Goldens regenerated for the listed behavior changes only: debug's help
label (3), `install --device=ID` now installing on ID (main's #77 golden
records it ignored), and the version words rejecting a flag (3).
AdamMusa added a commit that referenced this pull request Nov 21, 2026
Port the build fixes onto the cli/build/* split (#75) and keep #77/#78's
command registry: usage errors stay main's (exit 64), and the dispatcher,
help heredoc and new-command checks this branch had are dropped in favour
of main's. appbundle now gets aab's identity check, package rename and
stale-output clearing without being renamed to aab.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant