getPubspecVersion method

Future<String> getPubspecVersion()

Implementation

Future<String> getPubspecVersion() async {
  final file = File(p.join(projectDir, 'pubspec.yaml'));
  if (!file.existsSync()) {
    throw BuildException(
      'pubspec.yaml not found at "${file.path}"',
      fix: 'Verify you are in the root directory of a Flutter project.',
    );
  }

  try {
    final yaml = loadYaml(await file.readAsString());
    return yaml['version']?.toString() ?? '1.0.0';
  } catch (e, s) {
    throw BuildException(
      'Failed to parse version from pubspec.yaml',
      fix:
          'Ensure pubspec.yaml has a valid "version:" entry (e.g., 1.0.0+1).',
      originalStackTrace: s,
    );
  }
}