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

GetPackageById() private method

private GetPackageById ( PackageSource source, string name, NuGetRequest request, string requiredVersion = null, string minimumVersion = null, string maximumVersion = null, bool minInclusive = true, bool maxInclusive = true ) : IEnumerable
source PackageSource
name string
request NuGetRequest
requiredVersion string
minimumVersion string
maximumVersion string
minInclusive bool
maxInclusive bool
return IEnumerable
        private IEnumerable<PackageItem> GetPackageById(PackageSource source, 
            string name,
            NuGetRequest request, 
            string requiredVersion = null, 
            string minimumVersion = null, 
            string maximumVersion = null,
            bool minInclusive = true,
            bool maxInclusive = true)
        {
            try {
                Debug(Resources.Messages.DebugInfoCallMethod3, "NuGetRequest", "GetPackageById", name);

                // source should be attached to a repository
                if (source.Repository == null)
                {
                    return Enumerable.Empty<PackageItem>();
                }

                // otherwise fall back to traditional behavior
                var pkgs = source.Repository.FindPackagesById(name, request);

                // exact version is required if required version is not null or empty OR maxversion == minversion and min and max inclusive are true
                bool exactVersionRequired = (!string.IsNullOrWhiteSpace(requiredVersion))
                    || (!string.IsNullOrWhiteSpace(maximumVersion)
                        && !string.IsNullOrWhiteSpace(minimumVersion)
                        && (new SemanticVersion(minimumVersion) == new SemanticVersion(maximumVersion))
                        && minInclusive && maxInclusive);

                // if required version not specified then don't use unlisted version
                // unlisted version is the one that has published year as 1900
                if (!exactVersionRequired)
                {
                    pkgs = pkgs.Where(pkg => pkg.Published.HasValue && pkg.Published.Value.Year > 1900);
                }

                if (AllVersions.Value){
                    //Display versions from lastest to oldest
                    pkgs = (from p in pkgs select p).OrderByDescending(x => x.Version);
                }
                else if (String.IsNullOrWhiteSpace(requiredVersion) && String.IsNullOrWhiteSpace(minimumVersion) && String.IsNullOrWhiteSpace(maximumVersion))
                {

                    if (AllowPrereleaseVersions.Value || source.Repository.IsFile) {
                        //Handling something like Json.NET 7.0.1-beta3 as well as local repository
                        pkgs = from p in pkgs
                            group p by p.Id
                            into newGroup
                                   select newGroup.Aggregate((current, next) => (next.Version > current.Version) ? next : current);

                    } else {
                        pkgs = from p in pkgs where p.IsLatestVersion select p;
                    }
                }
                else if (!exactVersionRequired && !AllowPrereleaseVersions.Value)
                {
                    // if exact version is not required and allow prerelease is false, we will have to filter out prerelease version
                    pkgs = from p in pkgs where string.IsNullOrWhiteSpace(p.Version.SpecialVersion) select p;
                }

                pkgs = FilterOnContains(pkgs);
                pkgs = FilterOnTags(pkgs);

                var results = FilterOnVersion(pkgs, requiredVersion, minimumVersion, maximumVersion, minInclusive, maxInclusive)
                    .Select(pkg => new PackageItem {
                        Package = pkg,
                        PackageSource = source,
                        FastPath = MakeFastPath(source, pkg.Id, pkg.Version.ToString())
                    });
                return results;
            } catch (Exception e) {
                e.Dump(this);
                return Enumerable.Empty<PackageItem>();
            }
        }

Same methods

NuGetRequest::GetPackageById ( string name, NuGetRequest request, string requiredVersion = null, string minimumVersion = null, string maximumVersion = null, bool minInclusive = true, bool maxInclusive = true ) : IEnumerable

Usage Example

Example #1
0
        /// <summary>
        /// Searches package sources given name and version information
        ///
        /// Package information must be returned using <c>request.YieldPackage(...)</c> function.
        /// </summary>
        /// <param name="name">a name or partial name of the package(s) requested</param>
        /// <param name="requiredVersion">A specific version of the package. Null or empty if the user did not specify</param>
        /// <param name="minimumVersion">A minimum version of the package. Null or empty if the user did not specify</param>
        /// <param name="maximumVersion">A maximum version of the package. Null or empty if the user did not specify</param>
        /// <param name="id">if this is greater than zero (and the number should have been generated using <c>StartFind(...)</c>, the core is calling this multiple times to do a batch search request. The operation can be delayed until <c>CompleteFind(...)</c> is called</param>
        /// <param name="request">An object passed in from the PackageManagement that contains functions that can be used to interact with its Provider</param> 
        public void FindPackage(string name, string requiredVersion, string minimumVersion, string maximumVersion, int id, NuGetRequest request)
        {
            if (request == null){
                throw new ArgumentNullException("request");
            }

            // true if we want to include the max and min version
            bool minInclusive = true;
            bool maxInclusive = true;

            // If finding by canonical id, then the version follows dependency version requirement
            if (request.GetOptionValue("FindByCanonicalId").IsTrue())
            {
                // Use the dependency version if no min and max is supplied
                if (String.IsNullOrWhiteSpace(maximumVersion) && String.IsNullOrWhiteSpace(minimumVersion))
                {
                    DependencyVersion depVers = DependencyVersion.ParseDependencyVersion(requiredVersion);
                    maximumVersion = depVers.MaxVersion.ToStringSafe();
                    minimumVersion = depVers.MinVersion.ToStringSafe();
                    minInclusive = depVers.IsMinInclusive;
                    maxInclusive = depVers.IsMaxInclusive;

                    // set required version if we have both min max as the same value.
                    if (depVers.MaxVersion != null && depVers.MinVersion != null
                        && depVers.MaxVersion == depVers.MinVersion && minInclusive && maxInclusive)
                    {
                        requiredVersion = maximumVersion;
                    }
                    else
                    {
                        requiredVersion = null;
                    }
                }                
            }

            request.Debug(Resources.Messages.DebugInfoCallMethod, PackageProviderName, string.Format(CultureInfo.InvariantCulture, "FindPackage' - name='{0}', requiredVersion='{1}',minimumVersion='{2}', maximumVersion='{3}'", name, requiredVersion, minimumVersion, maximumVersion));

            NormalizeVersion(request, ref requiredVersion, ref minimumVersion, ref maximumVersion);

            try {

            
            // If there are any packages, yield and return
            if (request.YieldPackages(request.GetPackageById(name, request, requiredVersion, minimumVersion, maximumVersion, minInclusive, maxInclusive), name))
            {
                return;
            }

            // Check if the name contains wildcards. If not, return. This matches the behavior as "Get-module xje" 
            if (!String.IsNullOrWhiteSpace(name) && !WildcardPattern.ContainsWildcardCharacters(name))
            {              
                return;
            }

            // In the case of the package name is null or contains wildcards, error out if a user puts version info
            if (!String.IsNullOrWhiteSpace(requiredVersion) || !String.IsNullOrWhiteSpace(minimumVersion) || !String.IsNullOrWhiteSpace(maximumVersion))
            {
                request.Warning( Constants.Messages.MissingRequiredParameter, "name");
                return;
            }
            
            
       
            // Have we been cancelled?
            if (request.IsCanceled) {
                request.Debug(Resources.Messages.RequestCanceled, PackageProviderName, "FindPackage");

                return;
            }

            // A user does not provide the package full Name at all Or used wildcard in the name. Let's try searching the entire repository for matches.
            request.YieldPackages(request.SearchForPackages(name), name);
            }
            catch (Exception ex)
            {
                ex.Dump(request);
            }
        }
All Usage Examples Of Microsoft.PackageManagement.NuGetProvider.NuGetRequest::GetPackageById