getProjectName method

Future<String> getProjectName({
  1. required String workingDirectory,
})

Returns the name field from the target project's pubspec.yaml.

Falls back to the directory name with a warning when parsing fails, so generation can continue rather than aborting the entire pipeline.

Implementation

Future<String> getProjectName({required String workingDirectory}) async {
  try {
    final content = await File('$workingDirectory/pubspec.yaml').readAsString();
    final doc = loadYaml(content) as YamlMap;
    final projectName = doc['name'] as String;

    return projectName;
  } catch (e) {
    final fallback = workingDirectory.split(Platform.pathSeparator).last;
    _commandHelper.warning(
      'Could not read project name from pubspec.yaml: $e\n'
      'Falling back to directory name: "$fallback".',
    );
    return fallback;
  }
}