plan method
Computes the changes without touching the file system.
Implementation
SyncResult plan() {
final changes = <FileChange>[];
final expected = <String>{};
void put(
String relative,
String content, {
bool Function(String)? isCurrent,
}) {
expected.add(relative);
final file = File(_abs(relative));
if (!file.existsSync()) {
changes.add(
FileChange(ChangeKind.create, _pkgPath(relative), content: content),
);
return;
}
final existing = file.readAsStringSync();
if (existing == content) return;
if (!regenerate && isCurrent != null && isCurrent(existing)) return;
changes.add(
FileChange(ChangeKind.update, _pkgPath(relative), content: content),
);
}
for (final type in schema.enums.values) {
if (config.isSkipped(type.name)) continue;
put(
layout.enumFile(type),
singles.enumFile(type),
isCurrent: (existing) => _sameEnumValues(existing, type),
);
}
for (final type in schema.types.values) {
if (schema.rootTypes.contains(type.name) || config.isSkipped(type.name)) {
continue;
}
if (type.fields.isEmpty) {
_warnings.add('${type.name} has no fields; skipped.');
continue;
}
put(
layout.modelFile(type),
models.render(type),
isCurrent: (existing) => _sameModelFields(existing, type),
);
}
final singleFiles = <GeneratedFile, String?>{
GeneratedFile.enumConverters: schema.enums.isEmpty
? null
: singles.enumConverters(),
GeneratedFile.queryOperations: singles.operations(
schema.query,
GeneratedFile.queryOperations,
'Query',
),
GeneratedFile.mutationOperations: singles.operations(
schema.mutation,
GeneratedFile.mutationOperations,
'Mutation',
),
GeneratedFile.subscriptionOperations: singles.operations(
schema.subscription,
GeneratedFile.subscriptionOperations,
'Subscription',
),
GeneratedFile.queryArguments: singles.arguments(
schema.query,
GeneratedFile.queryArguments,
'Query',
),
GeneratedFile.mutationArguments: singles.arguments(
schema.mutation,
GeneratedFile.mutationArguments,
'Mutation',
),
GeneratedFile.subscriptionArguments: singles.arguments(
schema.subscription,
GeneratedFile.subscriptionArguments,
'Subscription',
),
};
for (final MapEntry(key: file, value: content) in singleFiles.entries) {
if (content != null) put(layout.singleFile(file), content);
}
if (config.barrel case final barrel?) {
put(barrel, singles.barrel(expected));
}
changes.addAll(_staleFiles(expected));
if (types.unmappedScalars.isNotEmpty) {
_warnings.add(
'No Dart type for scalar(s) ${types.unmappedScalars.join(', ')}; '
'generated as Object. Map them under `scalars:`.',
);
}
return SyncResult(changes, _warnings);
}