findVersion method

String? findVersion(
  1. String packageName,
  2. String requestedVersion
)

Searches for requestedVersion of packageName in pub-cache and returns the fully qualified path to that version. If the version is not found then null is returned;

Implementation

String? findVersion(String packageName, String requestedVersion) {
  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;
  }

  for (final pathToPackage in packages) {
    final fullPackageName = basename(pathToPackage);
    final firstHyphen = fullPackageName.indexOf('-');
    assert(firstHyphen >= 0, 'should always be a hypen after the filename');

    final packageVersion = fullPackageName.substring(firstHyphen + 1);

    if (packageVersion.compareTo(requestedVersion) == 0) {
      verbose(() => 'Found  version $packageVersion for $packageName '
          'at $pathToPackage');
      return pathToPackage;
    }
  }
  return null;
}