build method

  1. @override
Future<void> build(
  1. BuildStep buildStep
)

Generates the outputs for a given BuildStep.

Implementation

@override
Future<void> build(BuildStep buildStep) async {
  if (options.config['json_path'] == null) {
    return;
  }

  if (!buildStep.inputId.path.startsWith(options.config['json_path'])) {
    return;
  }

  // Read the JSON file
  print('json_serializer_helper build started for ${buildStep.inputId.path}');

  final jsonString = await buildStep.readAsString(buildStep.inputId);

  String className =
      convertFileNameToClassName(buildStep.inputId.pathSegments.last);
  Map<String, String> generatedFiles =
      generateDartClasses(className, jsonString, true);

  // Save the generated Dart file to the destination path
  generatedFiles.forEach((fileName, generatedDartFile) async {
    await buildStep.writeAsString(
        buildStep.inputId.changeExtension('.dart'), generatedDartFile);
  });
}