readResolvedHelperVersion function

String? readResolvedHelperVersion(
  1. String projectPath
)

Returns the fdb_helper version the Flutter project at projectPath resolves, from .dart_tool/package_config.json, or null when it cannot be determined (no pub get yet, no fdb_helper dependency, unreadable files).

Path and git dependencies are read from the package's pubspec.yaml; hosted ones fall back to the fdb_helper-X.Y.Z cache directory name.

Implementation

String? readResolvedHelperVersion(String projectPath) {
  try {
    final configFile = File('$projectPath/.dart_tool/package_config.json');
    if (!configFile.existsSync()) return null;
    final config = jsonDecode(configFile.readAsStringSync());
    if (config is! Map || config['packages'] is! List) return null;

    for (final entry in config['packages'] as List) {
      if (entry is! Map || entry['name'] != 'fdb_helper') continue;
      final rootUri = entry['rootUri'];
      if (rootUri is! String) return null;

      // rootUri is relative to the package_config.json file itself.
      final root = configFile.absolute.uri.resolve(rootUri.endsWith('/') ? rootUri : '$rootUri/');
      final rootPath = root.toFilePath();
      final pubspec = File('${rootPath}pubspec.yaml');
      if (pubspec.existsSync()) {
        final match = RegExp(r'^version:\s*([^\s#]+)', multiLine: true).firstMatch(pubspec.readAsStringSync());
        if (match != null) return match.group(1)!.replaceAll(RegExp('''['"]'''), '');
      }
      final segments = root.pathSegments.where((s) => s.isNotEmpty);
      final dirMatch = RegExp(r'^fdb_helper-(\d+\.\d+\.\d+\S*)$').firstMatch(segments.isEmpty ? '' : segments.last);
      return dirMatch?.group(1);
    }
    return null;
  } catch (_) {
    // Advisory only — never fail the calling command.
    return null;
  }
}