zorphy_migrator 2.5.0
zorphy_migrator: ^2.5.0 copied to clipboard
Codemod that migrates freezed model classes to zorphy using resolved AST analysis. Converts simple classes, sealed unions, @Default values, and @JsonKey annotations; reports anything that needs human [...]
zorphy_migrator #
A codemod that migrates model classes to zorphy
using resolved AST analysis — not regex — so union variants, generics,
defaults, and @JsonKey renames are converted correctly.
Two input dialects are supported:
- freezed model classes (the original target), and
@ExchangeableObject/@ExchangeableEnumcustom codegen models — the annotations used by flutter_inappwebview forks (e.g. zikzak_inappwebview) that do not use Freezed at all. Without this dialect the tool silently no-ops on such packages (see zorphy #86); with it, the same seamless migration path applies.
Part of the zorphy 2.0 release. See the comparison for why teams switch.
Usage #
dart pub global activate zorphy_migrator
# or: dart run zorphy_migrator ... (as a dev dependency)
zorphy_migrator migrate lib/ --dry-run # preview unified diff (default)
zorphy_migrator migrate lib/ --apply --report MIGRATION.md
zorphy_migrator migrate lib/ --apply --fail-on-manual # CI: exit 1 if a human is needed
Exit codes: 0 = clean migration, 1 = items need manual attention (or
nothing to migrate was found), 2 = analysis error.
Mapping #
| freezed construct | zorphy output |
|---|---|
@freezed class Foo with _$Foo { const factory Foo({required String a, int? b}) = _Foo; } |
@Zorphy(preset: ZorphyPreset.lean) abstract class $Foo { String get a; int? get b; } |
Union: factory Foo.ok(T v) = Ok; |
@Zorphy(explicitSubTypes: [$Ok]) abstract class $$Foo {} + abstract class $Ok implements $$Foo { T get v; } |
factory Foo.fromJson(...) => _$FooFromJson(json); |
generateJson: true on the annotation |
@Default(expr) |
@JsonKey(defaultValue: expr) on the getter (never silently dropped) |
@JsonKey(...) |
preserved verbatim on the getter |
| Field types referencing other migrated classes | rewritten to $-prefixed form (List<CartItem> → List<$CartItem>) |
@ExchangeableObject construct |
zorphy output |
|---|---|
@ExchangeableObject() class Foo_ { T x; Foo_({required this.x}); } |
@Zorphy(kind: ZorphyKind.valueObject, generateJson: true, generateCompareTo: true) abstract class $Foo { T get x; } |
this.x = expr constructor default |
@JsonKey(defaultValue: expr) on the getter |
@ExchangeableEnum() class Foo_ { static const A = Foo_._internal(0); } |
plain enum Foo { A } (member names + declaration order preserved) |
Field type Bar_ where Bar is converted in the same run |
Bar (the codegen _ suffix is dropped; concrete sibling form, recovered by zorphy #351) |
Lean-preset inference: classes that provably use only lean features (no
union, no defaults) are emitted with preset: ZorphyPreset.lean and the
choice is noted in the report; everything else uses the standard preset
(byte-compatible with zorphy 1.x defaults).
Not converted (always reported, never dropped) #
@unfreezedmutable classes — zorphy is immutable-first- Custom getters/methods in the class body
- Non-factory constructors and asserts
@Defaultexpressions that reference their own field- For
@ExchangeableObject/@ExchangeableEnum: custom@ExchangeableObjectMethod/@ExchangeableObjectProperty/@ExchangeableObjectConstructormembers, instance fields that are not constructor parameters, and enum wire values that do not map onto a plain enum (non-sequential ints, string values differing from the member name)
Every skipped construct appears in the report with file:line and a
reason. The tool never deletes files — removing *.freezed.dart and
the freezed dependencies is a documented manual step.
After --apply #
freezed sources #
- Remove
freezed/freezed_annotationfrom pubspec; addzorphy(dev) +zorphy_annotation. - Delete
*.freezed.dart; keep.g.dartonly wheregenerateJson: truewas emitted. - Update
partdirectives: drop.freezed.dart, add.zorphy.dart. - Replace
when/mapcalls with Dart 3switchexpressions on the sealed base. dart run build_runner build --delete-conflicting-outputs, thendart analyze.
@ExchangeableObject / @ExchangeableEnum sources #
- Swap the internal-annotations import for
package:zorphy_annotation/zorphy_annotation.dartand addpart '<file>.zorphy.dart';next to the existing.g.dartpart. - Delete the stale codegen
.g.dart(regenerated by json_serializable). If the package'sbuild.yamlscopes entity generation to a specific directory (e.g.lib/src/domain/entities/**), move the converted files there first. - Update barrels to re-export the entity under the
_-stripped public name, and glue platform controllers (fromMap→fromJson,toMap→toJson, enum wire_value→.index/name, plus any platform-native enum translation that used_nativeValue).
Requirements #
- The target project must resolve (
dart pub getdone) — the migrator uses the analyzer's resolved AST. - Targets zorphy 2.0 (
zorphy_annotation ^2.0.0). @ExchangeableObject/@ExchangeableEnumclasses are matched by annotation name (any library), so no dependency on the fork's internal annotations package is needed.