/// <summary> /// Find the installed package <paramref name="packageId"/> using the version <paramref name="versionRange"/> if not null, otherwise the <paramref name="constraintProvider"/> if specified. /// If no constraints are specified, the first found entry, whatever it means for NuGet, is used. /// </summary> /// <param name="packageId">Name of the package.</param> /// <param name="versionRange">The version range.</param> /// <param name="constraintProvider">The package constraint provider.</param> /// <param name="allowPrereleaseVersions">if set to <c>true</c> [allow prelease version].</param> /// <param name="allowUnlisted">if set to <c>true</c> [allow unlisted].</param> /// <returns>A Package matching the search criterion or null if not found.</returns> /// <exception cref="System.ArgumentNullException">packageIdentity</exception> /// <returns></returns> public NugetLocalPackage FindLocalPackage(string packageId, PackageVersionRange versionRange = null, ConstraintProvider constraintProvider = null, bool allowPrereleaseVersions = true, bool allowUnlisted = false) { // if an explicit version is specified, disregard the 'allowUnlisted' argument // and always allow unlisted packages. if (versionRange != null) { allowUnlisted = true; } else if (!allowUnlisted && ((constraintProvider == null) || !constraintProvider.HasConstraints)) { // Simple case, we just get the most recent version based on `allowPrereleaseVersions`. return(GetPackagesInstalled(new[] { packageId }).FirstOrDefault(p => allowPrereleaseVersions || string.IsNullOrEmpty(p.Version.SpecialVersion))); } var packages = GetLocalPackages(packageId); if (!allowUnlisted) { packages = packages.Where(p => p.Listed); } if (constraintProvider != null) { versionRange = constraintProvider.GetConstraint(packageId) ?? versionRange; } if (versionRange != null) { packages = packages.Where(p => versionRange.Contains(p.Version)); } return(packages?.FirstOrDefault(p => allowPrereleaseVersions || string.IsNullOrEmpty(p.Version.SpecialVersion))); }
/// <summary> /// Find the installed package <paramref name="packageId"/> using the version <paramref name="versionRange"/> if not null, otherwise the <paramref name="constraintProvider"/> if specified. /// If no constraints are specified, the first found entry, whatever it means for NuGet, is used. /// </summary> /// <param name="packageId">Name of the package.</param> /// <param name="versionRange">The version range.</param> /// <param name="constraintProvider">The package constraint provider.</param> /// <param name="allowPrereleaseVersions">if set to <c>true</c> [allow prelease version].</param> /// <param name="allowUnlisted">if set to <c>true</c> [allow unlisted].</param> /// <returns>A Package matching the search criterion or null if not found.</returns> /// <exception cref="System.ArgumentNullException">packageIdentity</exception> /// <returns></returns> public NugetPackage FindLocalPackage(string packageId, PackageVersion version = null, ConstraintProvider constraintProvider = null, bool allowPrereleaseVersions = true, bool allowUnlisted = false) { var versionRange = new PackageVersionRange(version); return(FindLocalPackage(packageId, versionRange, constraintProvider, allowPrereleaseVersions, allowUnlisted)); }