Configuration.fromYaml constructor

Configuration.fromYaml(
  1. File yamlFile, {
  2. String? projectDirPath,
})

Create a Configuration from yamlFile.

Implementation

factory Configuration.fromYaml(
  final File yamlFile, {
  String? projectDirPath,
}) {
  final String yamlPath = yamlFile.path;

  if (!yamlFile.existsSync()) {
    throw ArgumentError('${path.basename(yamlPath)} does not exist');
  }

  final String yamlContent = yamlFile.readAsStringSync();

  if (yamlContent.trim().isEmpty) {
    throw ArgumentError('${path.basename(yamlPath)} is empty');
  }

  try {
    final Object? parseYamlContentRaw = loadYaml(
      yamlContent,
      sourceUrl: Uri.tryParse(yamlPath),
    );

    if (parseYamlContentRaw is! Map<Object?, Object?>) {
      throw ArgumentError('Config file content is invalid');
    }

    final Configuration configuration =
        Configuration.fromJson(parseYamlContentRaw);

    return Configuration(
      projectType: configuration.projectType,
      features: configuration.features,
      projectDirPath: projectDirPath ?? configuration.projectDirPath,
    );
  } on CheckedFromJsonException catch (e, stackTrace) {
    final List<String> errorMessage = <String>[
      if (e.className != null) 'Could not create `${e.className}`.',
      if (e.key != null) 'There is a problem with "${e.key}".',
      if (e.message != null) e.message!
    ];

    throw ArgumentError(
      "${errorMessage.join('\n')}\n${stackTrace.toString()}",
    );
  } on YamlException catch (e) {
    throw ArgumentError(e.message);
  }
}