Skip to content

refactor: add the Flutter client startup hook seam (WP-00 R3) - #76

Merged
AdamMusa merged 22 commits into
mainfrom
refactor/entrypoint-hooks
Nov 20, 2026
Merged

AdamMusa merged 22 commits into
mainfrom
refactor/entrypoint-hooks

Conversation

@AdamMusa

@AdamMusa AdamMusa commented Nov 8, 2026 •

Copy link
Copy Markdown
Owner

Head 36b98ef8 contains main a9b58473 (#73 H1 goldens, #74, #75, #70, #83, #88, #77, #86, #71), merged with merge commits (44577a93, then b0baf549, e624fec9, ad36f12c, 003bc899 and 36b98ef8). Against main it changes five files (git diff --stat a9b58473 36b98ef8): the new client_bootstrap.rb (+363), the new build/contributors/startup_hooks.rb (+27), build_command.rb (+4), the new client_bootstrap_test.rb (+1,245) and build_contributors_test.rb (+26 −2). Companion: AdamMusa/ruflet-template#3 (08c4a00).

What

WP-00 R3, CLI side: Ruflet::CLI::ClientBootstrap. Later build features (WP-02/06/08/09) use this seam to add startup hooks to the Flutter client. CLIENT_BOOTSTRAP_HOOK_PROVIDERS is empty and frozen, so no feature adds a hook yet.

In #75's layout, write_client_bootstrap runs as the startup_hooks build contributor. It is registered right after :extensions, so it runs once the extension selection is applied and verified, before assets and before flutter pub get. That is the point where prepare_flutter_client called it before the split. The contributor always continues the build.

  • No hooks (every build today): nothing is written, edited or deleted.
    • The seam only checks whether lib/ruflet_bootstrap.g.dart exists and starts with the generated header. Only a build with hooks writes such a file.
    • If such a file is there, it is kept in step (empty lists) while a directive under lib/ names it, and removed once none does.
  • With hooks (future features): the seam writes the file and imports it after the last import of lib/main.self.dart and lib/main.server.dart. It also replaces their marked block (ruflet-template#3) with a pointer comment.
    • An entrypoint that already imports the file gets no second import. Its block is still replaced, so its own empty lists cannot shadow the imported ones.
    • An entrypoint with no import line gets the import as its first line.
    • An entrypoint with neither the block nor that import is left alone, with a warning.
    • A missing entrypoint is skipped.
    • Every hook is validated before any file changes.
    • A CRLF entrypoint stays CRLF. The import goes after the last import, even when that import spans lines or ends in a // comment.
  • A file at that path without the generated header is never overwritten or deleted. That covers the developer's own file, another tool's file, a directory, a symlink (dangling or not) and an unreadable file. With hooks, the build leaves that file and the entrypoints alone and warns:
    warning: lib/ruflet_bootstrap.g.dart was not written by `ruflet build`, so it is left as it is and the N startup hooks this build adds will not run. Rename that file to let the build generate it.
    
  • Only this client's own file counts. The URI must resolve to lib/ruflet_bootstrap.g.dart relative to the importing file, or be package:<the client's pubspec name>/ruflet_bootstrap.g.dart, in either quote style. package:other/…, dart:, other schemes and absolute paths never count.
    • Keeping the file: an import, export or part directive counts (not part of), through any of its URIs, a conditional import's alternatives included. The directive may start a line, or follow another directive or a block comment on its line. A // comment does not count. Text that only looks like a directive after a semicolon keeps the file too, which is the safe direction. ruflet-template#3's conformance test uses the same two patterns.
    • Routing: only import directives that start a line decide whether an entrypoint still needs the import.
  • The directive scan cannot fail a build. It skips files and directories under lib/ that cannot be read. A file that is not valid UTF-8 is scanned with its invalid bytes replaced, so a directive in it still counts. With hooks, an entrypoint that is not valid UTF-8 is routed too, and every byte outside the lines that change is kept.

Behavior changes

None. No provider exists, so ruflet build writes, edits and deletes exactly what it did before. The H1 goldens below show this.

This round:

  • Merge with refactor: split build_command.rb into cli/build modules (WP-00 R1) #75. The seam's call is now the startup_hooks contributor at the same point of the build. build_contributors_test pins it in BUILT_IN after extensions, and a new test checks that it passes the client dir, config, platform and self_contained on and continues whatever write_client_bootstrap returns.
  • A mid-line directive (review probe r5). Keep-or-delete only saw a directive at the start of a line. After a build with hooks, a lib file with import 'a.dart'; export 'ruflet_bootstrap.g.dart'; or /* hooks */ export 'ruflet_bootstrap.g.dart'; lost the file in the next build without hooks. Only a build with hooks can reach this, and none does yet.

Evidence

All run 2026-11-19 and 20 on c3604c40, with Ruby 4.0.5 and Flutter 3.41.2 unless marked. b0baf549 to 36b98ef8 only merge main: caa4ca95 (#83: the ruflet_rails and ruflet_record Gemfile.lock files, two VM harness tests, a stray 0.0.8 file), a309af55 (#88: four new ruflet_core test files) c444cbd5 (#77: the CLI command registry and 56 CLI goldens), 9202eb0c (#86: control contract tests and a material_control_registry.rb wire-type fix) and a9b58473 (#71: ruflet_core client state readers and their tests). None of them touches a file this PR changes, and this PR's diff against main is byte-identical to git diff dd924eb3 c3604c40.

Release coupling

No release order is needed. No build writes anything new until a feature registers a provider. Merging ruflet-template#3 ships it to every user, so the owner merges it (D1).

packages/ruflet/test/client_bootstrap_test.rb fixes the
lib/ruflet_bootstrap.g.dart every build writes into the Flutter client:
with no hooks it is the exact default ruflet-template ships (empty
rufletPreRunAppHooks and rufletPostFirstFrameHooks); an unchanged file is
not rewritten; hooks from registered providers render in order with one
import per library; and a hook the entrypoints could not run (unknown
phase, malformed import or function) is rejected.
ruflet-template's entrypoints run two ordered hook lists, one just before
runApp and one after the first frame, read from lib/ruflet_bootstrap.g.dart.
Ruflet::CLI::ClientBootstrap writes that file on every build, once the
entrypoints and the extension selection are final. Later build features
add a hook by registering a provider in CLIENT_BOOTSTRAP_HOOK_PROVIDERS
instead of editing lib/main*.dart; none is registered yet, so every build
writes the empty default the template ships, byte for byte (LF on every
platform).

H1 goldens: the 60 goldens of builds that reach client preparation gain
exactly one file, lib/ruflet_bootstrap.g.dart (new, 949 bytes, empty hook
lists); nothing else in any golden changes. The vendored template (e32c873)
predates the hooks, so its entrypoints do not import the file and the
compiled app is unchanged. Rebuild goldens stay "(no files changed)": the
write is idempotent.
Every build used to write lib/ruflet_bootstrap.g.dart into the Flutter
client, which changed 60 H1 goldens and added an unused file to client
dirs the developer owns (RUFLET_CLIENT_DIR, ./ruflet_client,
templates/ruflet_flutter_template). The template entrypoints now declare
both hook lists empty themselves between
// --RUFLET_BOOTSTRAP_HOOKS_START-- and // --RUFLET_BOOTSTRAP_HOOKS_END--
(AdamMusa/ruflet-template#3), so they build with every gem, released ones
included.

write_client_bootstrap now:
- writes and deletes nothing while no provider adds a hook and nothing
  under lib/ imports the generated file: every build today;
- with hooks, writes them to lib/ruflet_bootstrap.g.dart, imports it from
  lib/main.self.dart and lib/main.server.dart and replaces the marked
  block with a pointer. An entrypoint without the block (every released
  template) gets a warning naming it, and is left alone;
- keeps an imported file in step (empty lists without hooks), and removes
  a generated file nothing imports any more. A file it did not write is
  never removed;
- validates every hook before touching any file, and reads lib/ as bytes,
  so a non-UTF-8 or unreadable file cannot fail a build.

The 60 goldens go back to #73's, byte for byte: compat.rb checks 160/160
against them.
AdamMusa added a commit to AdamMusa/ruflet-template that referenced this pull request Nov 8, 2026
Released gems (0.0.19 to 0.0.23) fetch template main at build time and,
for a client outside build/client (RUFLET_CLIENT_DIR, ./ruflet_client),
refresh lib/main*.dart file by file. They never write
lib/ruflet_bootstrap.g.dart, so entrypoints that import it stop
compiling there.

The entrypoints now declare both hook lists empty themselves, between
// --RUFLET_BOOTSTRAP_HOOKS_START-- and // --RUFLET_BOOTSTRAP_HOOKS_END--,
next to the RufletBootstrapHook typedef and the runner, and import
nothing new. The template no longer ships lib/ruflet_bootstrap.g.dart.
Only while a build feature adds hooks does `ruflet build` write that
file, import it and empty the marked block (AdamMusa/ruflet#76).

The import section is byte-identical to e32c873 again, so the CLI's
import edits are unchanged too.

The conformance test now checks that the template does not ship the
generated file, that the entrypoints import only files the template
ships, and that the marked block holds exactly the two empty lists. It
fails on e32c873 and on the previous head 8c7e36a.
@AdamMusa AdamMusa changed the title refactor: write lib/ruflet_bootstrap.g.dart during build (WP-00 R3) refactor: add the Flutter client startup hook seam (WP-00 R3) Nov 8, 2026
Keeps this branch stacked on ruflet#73 (WP-00 H1) as it stands now, so
the H1 goldens this PR must leave unchanged are #73's current 226.
No build feature registers a hook provider yet, so neither the unit tests
nor the H1 goldens could see whether prepare_flutter_client calls
write_client_bootstrap at all: deleting the call, moving it before the
extension check, or dropping its platform:/self_contained: arguments left
every test green. That matters while this branch has to be merged with
R1 (split build_command.rb) and R2 (command registry), whose conflict
resolutions could drop the call silently.

The new tests run the real prepare_flutter_client against the H1 fixture
template plus the marked block ruflet-template#3 ships, with only the
flutter/dart/pod commands faked (pub get writes the package config the
build then verifies). For a server-driven web build and a self-contained
iOS build they pin that:

* the provider runs once, with this build's config, platform and
  self_contained;
* the generated file and the two routed entrypoints are the only files
  that differ from the same build without hooks;
* the provider runs after the extension selection is applied and
  verified, sees the pruned entrypoints, and runs before flutter pub get;
* a build that stops at the extension check runs no hook and writes
  nothing;
* without a provider the build writes no generated file, warns about
  nothing and leaves the marked block in place.
…WP-00 R3)

client_bootstrap_imported? built its glob pattern from the client path,
so a RUFLET_CLIENT_DIR or ./ruflet_client under a directory named like
"my app [v2]", "proj{1}" or "a,b{c,d}" matched no Dart file. With hooks,
the entrypoints were routed to lib/ruflet_bootstrap.g.dart, the scan then
found no import, and the file was never written, so the client stopped
compiling. Without hooks, the same miss would have deleted a generated
file an entrypoint still imports.

The client's lib/ is now the glob's base, never part of its pattern. No
build today is affected: no build feature registers a hook provider yet,
and without one the entrypoints never import the file.
Keeps this branch stacked on ruflet#73 (WP-00 H1) as it stands now, so
the H1 goldens this PR must leave unchanged are #73's current ones.
With no hooks, write_client_bootstrap wrote lib/ruflet_bootstrap.g.dart
whenever a lib file imported a path ending in that name, and overwrote
whatever was there (review probes A and B). A build without hooks now
writes, edits and deletes nothing unless the file starts with the
generated header, which only a build with hooks writes; that file keeps
the refresh-while-imported and remove-when-unimported behavior.

- A lib/ruflet_bootstrap.g.dart without the header (the developer's own
  file, a directory, a symlink, an unreadable file) is never overwritten
  or deleted. A build with hooks warns and leaves it and the entrypoints
  alone.
- Only an import that resolves to this client's
  lib/ruflet_bootstrap.g.dart counts: relative to the importing file, or
  package:<the client's pubspec name>/ruflet_bootstrap.g.dart, in either
  quote style. package:other/... does not.
- With hooks, a CRLF entrypoint stays CRLF, and the import goes after a
  last import that spans lines or ends in a // comment.

Tests: probes A and B, the hooks-plus-foreign-file warning, symlinks,
import resolution and the pubspec name, unreadable files and dirs
(rescue SystemCallError), multi-line and commented last imports, and a
CRLF entrypoint.
…F-8 entrypoints (WP-00 R3)

The keep-or-delete decision now counts every import, export and part
directive and every URI in it (a conditional import's alternatives
included), with the same patterns as ruflet-template's conformance test.
Before, a lib file that only exported the generated file, or named it as
a conditional alternative, saw it deleted by the next build without
hooks and stopped compiling.

Routing an entrypoint reads and writes bytes and scans a scrubbed copy,
so a build with hooks no longer raises ArgumentError on an entrypoint
that is not valid UTF-8, and keeps its other bytes.

Tests: both review probe cases, the non-UTF-8 entrypoint, a dangling
symlinked bootstrap, '/ruflet_bootstrap.g.dart' and 'x:/../' URIs, an
entrypoint that imports the file without the block, a missing
entrypoint, and an entrypoint that only exports the file. No build adds
hooks yet, so no build today reaches any of this.
#75 split build_command.rb into cli/build/ modules and turned
prepare_flutter_client's steps into registered contributors. The seam is
carried into that layout:

- build_command.rb requires client_bootstrap and includes ClientBootstrap
  next to FlutterSdk, as before.
- The write_client_bootstrap call, which ran right after the extension
  selection was applied and verified, is now the startup_hooks contributor
  (cli/build/contributors/startup_hooks.rb), registered after :extensions,
  so it still runs at that point, before assets and before Flutter.
- build_contributors_test pins startup_hooks in that place in BUILT_IN and
  checks that the contributor passes the build on and always continues.
Without hooks, the generated file stays while a directive under lib/
names it. The directive pattern only saw a directive at the start of a
line, so `import 'a.dart'; export 'ruflet_bootstrap.g.dart';` or
`/* hooks */ export 'ruflet_bootstrap.g.dart';` let the build delete a
file that line still needed. A directive now also counts after another
directive or a block comment on its line. Text that only looks like one
after a semicolon keeps the file too, the safe direction.
CLIENT_BOOTSTRAP_DIRECTIVE stays character for character the DIRECTIVE
of ruflet-template's conformance test, which changes with it.

Tests: the directive tables gain those forms (and `part\tof`, a mid-line
`part of`, a block-commented directive that does not count), an
entrypoint with the block but no import line gets the import as its
first line (plant N19), and no package name never matches 'package:/'.
No build adds a hook yet, so no build changes.
@AdamMusa
AdamMusa merged commit 43f7582 into main Nov 20, 2026
19 of 20 checks passed
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