findPrimaryVersion method

Version? findPrimaryVersion(
  1. String packageName
)

Finds and returns the latest (non-pre-release) version installed into pub cache for the given package.

If there are no stable versions then a pre-release version may be returned if one exists.

If no versions are installed then null is returned.

Implementation

Version? findPrimaryVersion(String packageName) {
  if (!exists(pathToDartLang)) {
    return null;
  }
  final packages = find(
    '$packageName-*.*',
    types: [Find.directory],
    workingDirectory: pathToDartLang,
  ).toList();

  if (packages.isEmpty) {
    verbose(() => 'No installed packages for $packageName found');
    return null;
  }

  final primary = Version.primary(
    packages.map((package) {
      final filename = basename(package);
      final firstHyphen = filename.indexOf('-');
      assert(firstHyphen >= 0, 'should always be a hypen after the filename');
      final version = filename.substring(firstHyphen + 1);

      return Version.parse(version);
    }).toList(),
  );
  verbose(() => 'Found primary version $primary for $packageName');
  return primary;
}