Code generators have a bad reputation, and it's earned. You generate an app, you customize it, then the data model changes and you have to regenerate. Now you choose between losing your changes and never regenerating again.
I built Underpeaks so that regeneration is safe. This post explains what actually happens when you run it.
The problem
Say you generate a Flutter app from a products model. You tweak the product details screen. A week later you add a field called sku to the model and regenerate.
A naive generator rewrites every file. Your tweaks are gone.
Step 1: every generated file is tracked
When Underpeaks generates a project, it writes a manifest next to the code. For every file it creates, the manifest records a SHA-256 checksum of the whole file.
Files also fall into one of three categories:
- Generated: files you're not meant to edit.
- Customizable: files you're expected to change.
- Root: project-level files.
On regeneration, Underpeaks compares each file's current checksum with the one in the manifest. If the checksums match, you haven't touched the file, so it's safe to update. If they don't match, you've changed it, and it is left alone.
Step 2: new fields are inserted, not regenerated
The part I'm happiest with: adding a field doesn't regenerate your screen. It inserts only the new field.
Generated screens contain a marker comment:
// NXF:INSERT-FIELDS-HERE - DO NOT DELETE
On regeneration, Underpeaks diffs the model's fields against the ones already in the file and inserts only the new ones at the marker. Fields that already exist are skipped, so you never get duplicate rows. Screens with several sections get one marker per section.
Text(product.name),
Text(product.price),
Text(product.sku), // new field, inserted by regeneration
// NXF:INSERT-FIELDS-HERE - DO NOT DELETE
Step 3: removed fields are reported, not deleted
If you delete a field from the model, the generator doesn't delete the matching code. It can't know whether you built something on top of it. Instead it prints a warning listing the fields that no longer exist in the model, and you decide what to remove.
What this means in practice
- Customize freely. Your edits survive.
- Add fields to a model and regenerate in seconds.
- Nothing is deleted without you seeing a warning first.
The one rule is the marker comment: leave it where it is.
Try it
Underpeaks Core is free and self-hosted: https://github.com/underpeaks/underpeaks-core. I'd like to hear about the edge cases where this breaks. I'm one person building it and I read every comment.
Top comments (0)