build method
Generates the outputs for a given BuildStep
.
Implementation
@override
FutureOr<void> build(BuildStep buildStep) async {
SourceNode? schema;
late AssetId _schemaId;
final doc = await readDocument(buildStep, config.sourceExtension);
final docPackage = buildStep.inputId.package;
final docDirPath = p.dirname(buildStep.inputId.path);
if (config.schemaIds != null) {
for (final schemaId in config.schemaIds!) {
if (docPackage == schemaId.package &&
docDirPath.contains(p.dirname(schemaId.path))) {
schema =
await readDocument(buildStep, config.sourceExtension, schemaId);
_schemaId = schemaId;
break;
}
}
}
if (schema == null && config.schemaId != null) {
schema = await readDocument(
buildStep, config.sourceExtension, config.schemaId);
_schemaId = config.schemaId!;
}
if (schema == null) {
throw StateError('No schema found for ${buildStep.inputId}');
}
if ((config.whenExtensionConfig.generateMaybeWhenExtensionMethod ||
config.whenExtensionConfig.generateWhenExtensionMethod) &&
!config.shouldAddTypenames) {
throw StateError(
'When extensions require add_typenames to be true. Consider setting add_typenames to true in your build.yaml or disabling when_extensions in your build.yaml.');
}
final triStateValueConfig = config.triStateOptionalsConfig;
final schemaOutputAsset =
outputAssetId(_schemaId, schemaExtension, config.outputDir);
final schemaUrl = schemaOutputAsset.uri.toString();
final serializerOutputAsset =
AssetId(buildStep.inputId.package, schemaOutputAsset.path);
final serializerUrl = serializerOutputAsset.uri.toString();
final schemaAllocator = GqlAllocator(
buildStep.inputId.uri.toString(),
config.sourceExtension,
outputAssetId(buildStep.inputId, schemaExtension, config.outputDir)
.uri
.toString(),
schemaUrl,
serializerUrl,
config.outputDir,
);
final varAllocator = schemaAllocator;
final dataToVarsMode = config.dataToJsonMode;
final libs = <String, Library>{
astExtension: buildAstLibrary(doc),
dataExtension: buildDataLibrary(
config.shouldAddTypenames ? addTypenames(doc) : doc,
addTypenames(schema),
p.basename(generatedFilePath(buildStep.inputId, dataExtension)),
config.typeOverrides,
config.whenExtensionConfig,
config.dataClassConfig,
),
varExtension: buildVarLibrary(
doc,
addTypenames(schema),
p.basename(generatedFilePath(buildStep.inputId, varExtension)),
config.typeOverrides,
varAllocator,
triStateValueConfig,
config.shouldGenerateVarsCreateFactories),
reqExtension: buildReqLibrary(
doc,
p.basename(generatedFilePath(buildStep.inputId, reqExtension)),
dataToVarsMode,
),
schemaExtension: buildSchemaLibrary(
doc,
p.basename(generatedFilePath(buildStep.inputId, schemaExtension)),
config.typeOverrides,
config.enumFallbackConfig,
generatePossibleTypesMap: config.shouldGeneratePossibleTypes,
allocator: schemaAllocator,
triStateValueConfig: triStateValueConfig,
generateVarsCreateFactories: config.shouldGenerateVarsCreateFactories,
),
};
for (var entry in libs.entries) {
final generatedAsset =
outputAssetId(buildStep.inputId, entry.key, config.outputDir);
final schemaOutputAsset =
outputAssetId(_schemaId, schemaExtension, config.outputDir);
final serialzerOutputAsset =
AssetId(buildStep.inputId.package, schemaOutputAsset.path);
final allocator = GqlAllocator(
buildStep.inputId.uri.toString(),
config.sourceExtension,
generatedAsset.uri.toString(),
schemaOutputAsset.uri.toString(),
serialzerOutputAsset.uri.toString(),
config.outputDir,
);
await writeDocument(generatedAsset, entry.value, allocator, buildStep);
}
}