Using the source_gen stack to make a code generator, how can I make a generator that generates code that would be the input of another generator (more specifically json_se
It's not possible just with annotation because there maybe two packages that both have the @JsonSerializable
annotation
There are two situtations :
You know what other generators should run after your generator.
class Example extends Generator {
@override
String generate(LibraryReader library, BuildStep buildStep) {
return JsonSerializable().generate('''
@JsonSerializable(nullable: false)
class Person {
final String firstName;
final String lastName;
final DateTime dateOfBirth;
Person({this.firstName, this.lastName, this.dateOfBirth});
factory Person.fromJson(Map json) => _PersonFromJson(json);
Map toJson() => _PersonToJson(this);
}
''');
}
}
Unfortunately currently there is no way to tell the source_gen that your generator may produce a code that needs code generation.
I created an issue here https://github.com/dart-lang/source_gen/issues/442 if you want to subscribe