Ejemplo n.º 1
0
        /// <summary>
        /// Parses a dependency from the feed in the format:
        /// id or id:versionSpec, or id:versionSpec:targetFramework
        /// </summary>
        private static Tuple <string, DependencyVersion, string> ParseDependency(string value)
        {
            if (String.IsNullOrWhiteSpace(value))
            {
                return(null);
            }

            // IMPORTANT: Do not pass StringSplitOptions.RemoveEmptyEntries to this method, because it will break
            // if the version spec is null, for in that case, the Dependencies string sent down is "<id>::<target framework>".
            // We do want to preserve the second empty element after the split.
            string[] tokens = value.Trim().Split(new[] { ':' });

            if (tokens.Length == 0)
            {
                return(null);
            }

            // Trim the id
            string id = tokens[0].Trim();

            // Parse the dependency version
            DependencyVersion depVer = new DependencyVersion();

            if (tokens.Length > 1)
            {
                // Parse the version
                depVer = DependencyVersion.ParseDependencyVersion(tokens[1]);
            }

            // Get the target framework, returns empty string if none exists.
            var targetFramework = (tokens.Length > 2 && !String.IsNullOrEmpty(tokens[2])) ? tokens[2] : String.Empty;

            return(Tuple.Create(id, depVer, targetFramework));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Read the dependencies from xelement. Returns a list of dependencies
        /// </summary>
        /// <param name="containerElement"></param>
        /// <returns></returns>
        private static List <PackageDependency> ReadDependencies(XElement containerElement)
        {
            // list of dependency
            var dependencies = containerElement.ElementsNoNamespace("dependency");

            return((from element in containerElement.ElementsNoNamespace("dependency")
                    let idElement = element.Attribute("id")
                                    // Check that id is not null or empty
                                    where idElement != null && !String.IsNullOrEmpty(idElement.Value)
                                    // Project a PackageDependency based on that
                                    select new PackageDependency
            {
                Id = idElement.Value.SafeTrim(),
                DependencyVersion = DependencyVersion.ParseDependencyVersion(element.GetOptionalAttributeValue("version").SafeTrim())
            }).ToList());
        }
        public PackageDependency Make(dynamic jsonObject)
        {
            PackageDependency pd = new PackageDependency();

            if (jsonObject.HasProperty("id"))
            {
                pd.Id = jsonObject.id;
            }

            if (jsonObject.HasProperty("range"))
            {
                pd.DependencyVersion = DependencyVersion.ParseDependencyVersion(jsonObject.range);
            }

            return(pd);
        }
        /// <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);
            }
        }
        /// <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);

            // First call to SearchPackages will just look for the packages with current credentials
            if (SearchPackages(name, requiredVersion, minimumVersion, maximumVersion, minInclusive, maxInclusive, id, request))
            {
                return;
            }

            if (request.CredentialUsername.IsNullOrEmpty())
            {
                // If no packages were found, try again using credentials retrieved from credential provider
                // First call to the credential provider is to get credentials, but if those credentials fail,
                // we call the cred provider again to ask the user for new credentials, and then search pkgs again using new creds
                var query       = new Uri(request.FindRegisteredSource(request.Sources.First()).Location.IsNullOrEmpty() ? request.Sources.First() : request.FindRegisteredSource(request.Sources.First()).Location);
                var credentials = request.GetCredsFromCredProvider(query.AbsoluteUri, request, false);
                var newclient   = PathUtility.GetHttpClientHelper(credentials.UserName, credentials.SecurePassword, request.WebProxy);
                request.SetHttpClient(newclient);

                if (SearchPackages(name, requiredVersion, minimumVersion, maximumVersion, minInclusive, maxInclusive, id, request))
                {
                    return;
                }

                // Calling the credential provider for a second time, using -IsRetry
                credentials = request.GetCredsFromCredProvider(query.AbsoluteUri, request, true);
                newclient   = PathUtility.GetHttpClientHelper(credentials.UserName, credentials.SecurePassword, request.WebProxy);
                request.SetHttpClient(newclient);

                if (SearchPackages(name, requiredVersion, minimumVersion, maximumVersion, minInclusive, maxInclusive, id, request))
                {
                    return;
                }
            }
        }