NuGet.LocalPackageRepository.GetPackageLookupPaths C# (CSharp) Method

GetPackageLookupPaths() public method

public GetPackageLookupPaths ( string packageId, SemanticVersion version ) : IEnumerable
packageId string
version SemanticVersion
return IEnumerable
        public IEnumerable<string> GetPackageLookupPaths(string packageId, SemanticVersion version)
        {
            // Files created by the path resolver. This would take into account the non-side-by-side scenario 
            // and we do not need to match this for id and version.
            var packageFileName = PathResolver.GetPackageFileName(packageId, version);
            var filesMatchingFullName = GetPackageFiles(packageFileName);

            if (version.Version.Revision < 1)
            {
                // If the build or revision number is not set, we need to look for combinations of the format
                // * Foo.1.2.nupkg
                // * Foo.1.2.3.nupkg
                // * Foo.1.2.0.nupkg
                // * Foo.1.2.0.0.nupkg
                // To achieve this, we would look for files named 1.2*.nupkg if both build and revision are 0 and
                // 1.2.3*.nupkg if only the revision is set to 0.
                string partialName = version.Version.Build < 1 ?
                                        String.Join(".", packageId, version.Version.Major, version.Version.Minor) :
                                        String.Join(".", packageId, version.Version.Major, version.Version.Minor, version.Version.Build);
                partialName += "*" + Constants.PackageExtension;

                // Partial names would result is gathering package with matching major and minor but different build and revision. 
                // Attempt to match the version in the path to the version we're interested in.
                var partialNameMatches = GetPackageFiles(partialName).Where(path => FileNameMatchesPattern(packageId, version, path));
                return Enumerable.Concat(filesMatchingFullName, partialNameMatches);
            }
            return filesMatchingFullName;
        }

Usage Example

		public static bool IsPackageInstalled (this PackageReference packageReference, PhysicalFileSystem fileSystem)
		{
			if (packageReference.Version == null) {
				return false;
			}

			var repository = new LocalPackageRepository (new DefaultPackagePathResolver (fileSystem), fileSystem);
			return repository
				.GetPackageLookupPaths (packageReference.Id, packageReference.Version)
				.Any ();
		}