Microsoft.PackageManagement.NuGetProvider.NuGetRequest.GetInstalledPackages C# (CSharp) Method

GetInstalledPackages() private method

Search in the destination of the request to checks whether the package name is installed. Returns true if at least 1 package is installed
private GetInstalledPackages ( string name, string requiredVersion, string minimumVersion, string maximumVersion, bool minInclusive = true, bool maxInclusive = true, bool terminateFirstFound = false ) : bool
name string
requiredVersion string
minimumVersion string
maximumVersion string
minInclusive bool
maxInclusive bool
terminateFirstFound bool
return bool
        internal bool GetInstalledPackages(string name, string requiredVersion, string minimumVersion, string maximumVersion, bool minInclusive = true, bool maxInclusive = true, bool terminateFirstFound = false)
        {
            try
            {
                bool found = false;
                var paths = InstalledPath.Where(Directory.Exists).ToArray();

                // if directory does not exist then just return false
                if (!paths.Any())
                {
                    return false;
                }

                // look in the destination directory for directories that contain *.nupkg & .nuspec files.
                var subdirs = paths.SelectMany(Directory.EnumerateDirectories);

                foreach (var subdir in subdirs)
                {
                    //reset the flag when we begin a folder
                    var isDup = false;

                    var nupkgs = Directory.EnumerateFiles(subdir, "*.nupkg", SearchOption.TopDirectoryOnly).Union(
                        Directory.EnumerateFiles(subdir, "*.nuspec", SearchOption.TopDirectoryOnly));

                    foreach (var pkgFile in nupkgs)
                    {

                        if (isDup)
                        {
                            continue;
                        }

                        //As the package name has to be in the installed file path, check if it is true
                        if (!String.IsNullOrWhiteSpace(name) && !NuGetProvider.IsNameMatch(name, pkgFile))
                        {
                            //not the right package
                            continue;
                        }

                        //unpack the package
                        var existFileName = Path.GetFileNameWithoutExtension(pkgFile);

                        var pkgItem = GetPackageByFilePath(pkgFile, existFileName);

                        if (pkgItem != null && pkgItem.IsInstalled)
                        {
                            //A user does not provide any package name in the commandeline, return them all
                            if (string.IsNullOrWhiteSpace(name))
                            {
                                isDup = true;
                                if (!YieldPackage(pkgItem, existFileName))
                                {
                                    return found;
                                }
                            }
                            else
                            {

                                //check if the version matches
                                if (!string.IsNullOrWhiteSpace(requiredVersion))
                                {
                                    if (pkgItem.Package.Version != new SemanticVersion(requiredVersion))
                                    {
                                        continue;
                                    }
                                }
                                else
                                {
                                    // if min and max version not matched
                                    if (!MinAndMaxVersionMatched(pkgItem.Package.Version, minimumVersion, maximumVersion, minInclusive, maxInclusive))
                                    {
                                        continue;
                                    }

                                    if (terminateFirstFound)
                                    {
                                        // if we are searching for a name and terminatefirstfound is set to true, then returns here
                                        return true;
                                    }
                                }

                                //found the match
                                isDup = true;
                                YieldPackage(pkgItem, existFileName);
                                found = true;

                            } //end of else

                        } //end of if (pkgItem...)
                    } //end of foreach
                }//end of foreach subfolder

                return found;
            }
            catch (Exception ex)
            {
                ex.Dump(this);
                return false;
            }
        }

Usage Example

        /// <summary>
        /// Returns the package dependencies of packageItem. We only return the dependencies that are not installed in the destination folder of request
        /// </summary>
        /// <param name="packageItem"></param>
        /// <param name="request"></param>
        private static IEnumerable<PackageItem> GetPackageDependenciesHelper(PackageItem packageItem, NuGetRequest request)
        {
            if (packageItem.Package.DependencySetList == null)
            {
                yield break;
            }

            foreach (var depSet in packageItem.Package.DependencySetList)
            {
                if (depSet.Dependencies == null)
                {
                    continue;
                }

                foreach (var dep in depSet.Dependencies)
                {
                    // Get the min dependencies version
                    string minVersion = dep.DependencyVersion.MinVersion.ToStringSafe();

                    // Get the max dependencies version
                    string maxVersion = dep.DependencyVersion.MaxVersion.ToStringSafe();

                    // check whether it is already installed at the destination
                    if (request.GetInstalledPackages(dep.Id, null, minVersion, maxVersion, minInclusive: dep.DependencyVersion.IsMinInclusive, maxInclusive: dep.DependencyVersion.IsMaxInclusive, terminateFirstFound: true))
                    {
                        request.Verbose(String.Format(CultureInfo.CurrentCulture, Messages.AlreadyInstalled, dep.Id));
                        // already have a dependency so move on
                        continue;
                    }

                    // get all the packages that match this dependency
                    var dependentPackageItem = request.GetPackageById(dep.Id, request, minimumVersion: minVersion, maximumVersion: maxVersion, minInclusive: dep.DependencyVersion.IsMinInclusive, maxInclusive: dep.DependencyVersion.IsMaxInclusive).ToArray();

                    if (dependentPackageItem.Length == 0)
                    {
                        request.WriteError(ErrorCategory.ObjectNotFound, dep.Id, Constants.Messages.UnableToFindDependencyPackage, dep.Id);

                        break;
                    }

                    // Get the package that is the latest version
                   yield return dependentPackageItem.OrderByDescending(each => each.Version).FirstOrDefault();
                }
            }
        }
All Usage Examples Of Microsoft.PackageManagement.NuGetProvider.NuGetRequest::GetInstalledPackages