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('[DSFlutterGen] v$packageVersion Loading ...');

  final defaultMap = loadYaml(configDefaultYamlContent) as Map?;

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

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

  if (buildFile != null) {
    if (buildFile.existsSync()) {
      final buildContent = buildFile.readAsStringSync();
      final rawMap = loadYaml(buildContent) as Map?;
      final builders = rawMap?['targets']?[r'$default']?['builders'];
      final optionBuildMap = (builders?['ds_flutter_gen_runner'] ??
          builders?['ds_flutter_gen'])?['options'];
      if (optionBuildMap is YamlMap && optionBuildMap.isNotEmpty) {
        final buildMap = {'ds_flutter_gen': optionBuildMap};
        mergedMap = mergeMap([mergedMap, buildMap]);
        final buildLocaleHint = normalize(
          join(basename(buildFile.parent.path), basename(buildFile.path)),
        );
        stdout.writeln(
          '[DSFlutterGen] Reading options from $buildLocaleHint',
        );
      } else {
        stderr.writeln(
          '[DSFlutterGen] Specified ${buildFile.path} as input but the file '
          'does not contain valid options, ignoring...',
        );
      }
    } else {
      stderr.writeln(
        '[DSFlutterGen] Specified ${buildFile.path} as input but the file '
        'does not exists.',
      );
    }
  }

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