Map _parsePubspecLockSync()

in tool/pub_get_offline.dart [61:91]


Map<String, String> _parsePubspecLockSync(File file) {
  final versions = <String, String>{};
  final lines = file.readAsLinesSync();
  String? package;
  String? source;
  for (final line in lines) {
    bool startsWithSpaces(int count) =>
        line.startsWith(' ' * count) && !line.startsWith(' ' * (count + 1));
    if (startsWithSpaces(2) && line.endsWith(':')) {
      package = line.trim().split(':').first;
      source = null;
    } else if (startsWithSpaces(4) && package != null) {
      final parts = line.trim().split(':');
      if (parts.first == 'source' && parts.length > 1) {
        source = parts[1].trim();
        continue;
      }
      if (parts.first == 'version' && parts.length > 1) {
        if (source != 'hosted') continue;
        final quoted = parts[1].trim().split('"');
        if (quoted.length == 3 && quoted.first.isEmpty && quoted.last.isEmpty) {
          final version = quoted[1];
          if (version.isNotEmpty) {
            versions[package] = version;
          }
        }
      }
    }
  }
  return versions;
}