readProjectInfo method
Reads package name / version / description from pubspec.yaml.
Implementation
ProjectInfo readProjectInfo(String projectRoot) {
final pubspecFile = File(
'${Directory(projectRoot).path}${Platform.pathSeparator}pubspec.yaml',
);
if (!pubspecFile.existsSync()) {
throw StateError(
'No pubspec.yaml found at ${pubspecFile.path}. '
'Pass the root of a Dart/Flutter project.',
);
}
final yaml = loadYaml(pubspecFile.readAsStringSync());
if (yaml is! YamlMap) {
throw StateError('pubspec.yaml is not a valid YAML map.');
}
final name = yaml['name']?.toString() ?? 'unknown';
final version = yaml['version']?.toString() ?? '0.0.0';
final description = yaml['description']?.toString();
return ProjectInfo(
name: name,
version: version,
description: description,
projectPath: Directory(projectRoot).absolute.path,
);
}