loadPubspecConfig function

Config loadPubspecConfig(
  1. File pubspecFile, {
  2. File? buildFile,
})

Implementation

Config loadPubspecConfig(File pubspecFile, {File? buildFile}) {
  final pubspecLocaleHint = normalize(
    join(basename(pubspecFile.parent.path), basename(pubspecFile.path)),
  );

  stdout.writeln(
    '$flutterGenVersion Loading ...',
  );

  final defaultMap = loadYaml(configDefaultYamlContent) as Map?;

  final pubspecContent = pubspecFile.readAsStringSync();
  final pubspecMap = loadYaml(pubspecContent) as Map?;

  var mergedMap = mergeMap([defaultMap, pubspecMap]);
  stdout.writeln(
    'Reading FlutterGen options from $pubspecLocaleHint',
  );

  if (buildFile != null && buildFile.existsSync()) {
    final buildContent = buildFile.readAsStringSync();
    final rawMap = loadYaml(buildContent) as Map?;
    final optionBuildMap = rawMap?['targets']?[r'$default']?['builders']
        ?['flutter_gen']?['options'];

    if (optionBuildMap != null) {
      final buildMap = {'flutter_gen': optionBuildMap};
      mergedMap = mergeMap([mergedMap, buildMap]);
      final buildLocaleHint = normalize(
        join(basename(buildFile.parent.path), basename(buildFile.path)),
      );
      stdout.writeln(
        'Reading FlutterGen options from $buildLocaleHint',
      );
    }
  }

  final pubspec = Pubspec.fromJson(mergedMap);
  return Config._(pubspec: pubspec, pubspecFile: pubspecFile);
}