getProjectName static method

String getProjectName()

Reads the project's pubspec.yaml from the current working directory and returns the top-level name value as a String.

Throws an Exception if:

  • pubspec.yaml is not found in the project root, or
  • the name entry is missing from the YAML.

This function performs synchronous file I/O and uses loadYaml to parse the file, so call it only where blocking operations are acceptable.

Implementation

static String getProjectName() {
  final file = File('pubspec.yaml');
  if (!file.existsSync()) {
    throw Exception('pubspec.yaml not found in project root!');
  }
  final content = file.readAsStringSync();
  final doc = loadYaml(content);
  if (doc['name'] == null) {
    throw Exception('Project name not found in pubspec.yaml');
  }
  return doc['name'];
}