readVersion method

ProjectVersion readVersion(
  1. File pubspecFile
)

Reads the current ProjectVersion from pubspecFile.

Throws ValidationException if pubspec.yaml is missing or does not contain a version: line.

Implementation

ProjectVersion readVersion(File pubspecFile) {
  if (!pubspecFile.existsSync()) {
    throw ValidationException(
        'pubspec.yaml not found at path "${pubspecFile.path}".');
  }

  final lines = pubspecFile.readAsLinesSync();
  for (final line in lines) {
    final trimmed = line.trim();
    if (trimmed.startsWith('version:') && !trimmed.startsWith('#')) {
      final rawVersion = trimmed.substring('version:'.length).trim();
      final unquoted =
          rawVersion.replaceAll("'", '').replaceAll('"', '').trim();
      if (unquoted.isEmpty) {
        throw const ValidationException(
            'Found empty "version:" line in pubspec.yaml.');
      }
      return ProjectVersion.parse(unquoted);
    }
  }

  throw const ValidationException(
      'No valid "version:" entry found in pubspec.yaml.');
}