Пример #1
0
        private static MyNugetPackage find_dependency_pacakge_using_http_request(MyNugetPackageDependency dependencyPkg, ChocolateyConfiguration configuration)
        {
            bool allowPreRelease = configuration.Prerelease;
            List <MyNugetPackage> totalFoundBestPkgs = new List <MyNugetPackage>();

            foreach (string sourceUri in configuration.Sources.Split(';'))
            {
                bool isUriOk = Uri.IsWellFormedUriString(sourceUri.Trim(), UriKind.Absolute);
                if (!isUriOk)
                {
                    "chocolatey".Log().Debug("Skipping search in unsupported source: '{0}' - Only WEB sources are supported for this command".format_with(sourceUri)); continue;
                }
                ChocolateyConfiguration tempConf = configuration.deep_copy();
                tempConf.Sources = sourceUri.Trim();
                string responseStr = query_for_package_using_http_request(dependencyPkg, tempConf);
                if (responseStr == null)
                {
                    continue;
                }
                List <MyNugetPackage> foundPackagesList = parseHttpQueryPackageDetailsRequestResponse(dependencyPkg, responseStr);
                if (foundPackagesList.Count() == 0)
                {
                    "chocolatey".Log().Debug("Failed to find any 'entry' child nodes of pkg: '{0}' in source: '{1}'".format_with(dependencyPkg.Id, sourceUri)); continue;
                }

                // Sort results based on their version so that higher is first
                foundPackagesList.Sort(delegate(MyNugetPackage x, MyNugetPackage y) {
                    return(y.Version.CompareTo(x.Version));
                });

                MyNugetPackage bestMatchingPkg = foundPackagesList[0]; //Latest version found
                if (dependencyPkg.VersionSpec != null)                 // If want a specific version
                // Attempt to get the best matching package
                {
                    bestMatchingPkg = getBestMatchingDependencyPkgFromList(foundPackagesList, dependencyPkg);
                }

                if (bestMatchingPkg == null)
                {
                    "chocolatey".Log().Debug("Failed to determine best package from found 'entry' child nodes of pkg: '{0}' in source: '{1}'".format_with(dependencyPkg.Id, sourceUri)); continue;
                }
                totalFoundBestPkgs.Add(bestMatchingPkg);
            }

            if (totalFoundBestPkgs.Count() == 0)
            {
                "chocolatey".Log().Warn("Failed to find a dependency package '{0}' that satisfies version spec: {1}".format_with(dependencyPkg.Id, dependencyPkg.VersionSpec.ToString()));
                return(null);
            }

            // Sort results based on their version so that higher is first
            totalFoundBestPkgs.Sort(delegate(MyNugetPackage x, MyNugetPackage y) {
                return(y.Version.CompareTo(x.Version));
            });
            return(totalFoundBestPkgs[0]);
        }
Пример #2
0
        public static MyNugetPackage ConvertFromIPackage(IPackage package)
        {
            List <MyNugetPackageDependency> packageDependencies = new List <MyNugetPackageDependency>();

            foreach (PackageDependency packageDependency in getCompatiblePackageDependencies(package))
            {
                packageDependencies.Add(MyNugetPackageDependency.ConvertFromPackageDependency(packageDependency));
            }
            return(new MyNugetPackage(package.Id, package.Version, packageDependencies));
        }
Пример #3
0
 public override bool Equals(object obj)
 {
     //Check for null and compare run-time types.
     if ((obj == null) || !this.GetType().Equals(obj.GetType()))
     {
         return(false);
     }
     else
     {
         MyNugetPackageDependency pkgDependency = (MyNugetPackageDependency)obj;
         return(this.Id.Equals(pkgDependency.Id) && (this.VersionSpec.Equals(pkgDependency.VersionSpec)));
     }
 }
Пример #4
0
        private static string query_for_package_using_http_request(MyNugetPackageDependency dependencyPkg, ChocolateyConfiguration configuration)
        {
            HttpClient.DefaultCredentialProvider  = new ChocolateyNugetCredentialProvider(configuration);
            HttpClient.DefaultCertificateProvider = new ChocolateyClientCertificateProvider(configuration);

            string sourceUri = configuration.Sources;
            //bool includePrerelease = configuration.Prerelease;
            //int top = 200;
            //int skip = 0;
            //string targetFramework = "";
            //string uriPostfix = "Search()?&$top={0}&searchTerm='{1}'&includePrerelease={2}&targetFramework='{3}'".format_with(top, dependencyPkg.Id.ToLower(), includePrerelease.ToString().ToLower(), targetFramework);
            //if (findLatestVersion) { uriPostfix += "&$filter=IsLatestVersion"; }

            string uriPostfix = "FindPackagesById()?id='{0}'".format_with(dependencyPkg.Id.ToLower());

            if (dependencyPkg.IsExactVersion)
            {
                uriPostfix = "Packages(Id='{0}',Version='{1}')".format_with(dependencyPkg.Id.ToLower(), dependencyPkg.VersionSpec.MaxVersion.ToString());
            }

            sourceUri  = sourceUri.TrimEnd('/');
            uriPostfix = uriPostfix.TrimStart('/');
            string fullRequestUri = string.Format("{0}/{1}", sourceUri, uriPostfix);

            HttpClient client = new HttpClient(new Uri(fullRequestUri));

            client.Method = "GET";
            string responseStr = null;

            try
            {
                "chocolatey".Log().Debug("Querying uri: {0}  for package:  '{1}'".format_with(fullRequestUri, dependencyPkg.Id.ToLower()));
                HttpWebResponse response           = (HttpWebResponse)client.GetResponse();
                int             responseStatusCode = (int)response.StatusCode;
                "chocolatey".Log().Debug("Reading response");
                using (var reader = new StreamReader(response.GetResponseStream())) { responseStr = reader.ReadToEnd(); }
                if ((responseStatusCode < 200) && (responseStatusCode > 299))
                {
                    "chocolatey".Log().Debug(responseStr);
                    throw new Exception("Received bad status code from query: {0} == '{1}'".format_with(responseStatusCode, response.StatusCode));
                }
            }
            catch (Exception e)
            {
                "chocolatey".Log().Debug(e.ToString());
                "chocolatey".Log().Debug("Failed to query: {0}  for package:  '{1}'".format_with(fullRequestUri, dependencyPkg.Id.ToLower()));
                responseStr = null;
            }
            return(responseStr);
        }
Пример #5
0
        private static MyNugetPackage parseXmlNodeForPackageDetails(MyNugetPackageDependency dependencyPkg, XmlNode entryNode)
        {
            string findPackageNameAndVersionRgxPattern = @"'(.*?)'";
            string repoPackageUrl         = null;
            string dependencyPkgIdLowered = dependencyPkg.Id.ToLower();
            List <MyNugetPackageDependency> packageDependenciesList = null;

            foreach (XmlNode entryChildNode in entryNode.ChildNodes)
            {
                string nodeNameLowered = entryChildNode.Name.ToLower();
                if (nodeNameLowered == "id")
                {
                    repoPackageUrl = entryChildNode.InnerText;
                }
                else if (nodeNameLowered == "m:properties")
                {
                    packageDependenciesList = parseXmlNodeForPackageDependenciesList(entryChildNode);
                }
            }
            if (repoPackageUrl == null)
            {
                throw new Exception("Failed to find any 'id' child of entry node of http response");
            }
            Regex           rg           = new Regex(findPackageNameAndVersionRgxPattern);
            MatchCollection regexMatches = rg.Matches(repoPackageUrl);

            if (regexMatches.Count != 2)
            {
                throw new Exception("Failed to regex: {0} for package id and version".format_with(repoPackageUrl));
            }
            string packageId = regexMatches[0].Value.Trim().Replace("'", "");

            if (packageId.ToLower() != dependencyPkgIdLowered)
            {
                return(null);
            }                                                                    // Not the package we were looking for
            string packageVersion = regexMatches[1].Value.Trim().Replace("'", "");

            return(new MyNugetPackage(packageId, packageVersion, packageDependenciesList));
        }
Пример #6
0
        private static List <MyNugetPackage> parseHttpQueryPackageDetailsRequestResponse(MyNugetPackageDependency dependencyPkg, string httpResponseStr)
        {
            "chocolatey".Log().Debug("Attempting to parse http response into an XML obj");
            XmlDocument xmlDoc = new XmlDocument();

            try
            {
                xmlDoc.LoadXml(httpResponseStr);
            }
            catch (Exception e) {
                "chocolatey".Log().Debug(e.ToString());
                "chocolatey".Log().Debug("Failed to parse http response into an XML obj");
                return(null);
            }

            XmlElement  rootNode         = xmlDoc.DocumentElement;
            XmlNodeList rootNodeChildren = rootNode.ChildNodes;

            if (xmlDoc.DocumentElement.Name.ToLower() == "entry")
            {
                //Create a new node.
                XmlElement node = xmlDoc.CreateElement("feed");
                //Add the node to the document.
                rootNode.PrependChild(node);
                rootNodeChildren = rootNode.ParentNode.ChildNodes;
            }


            "chocolatey".Log().Debug("Looping on XML obj to get all 'entry' child nodes");
            List <MyNugetPackage> foundPackagesList = new List <MyNugetPackage>();

            foreach (XmlNode docChildNode in rootNodeChildren)
            {
                // Looking for 'entry' child nodes only
                if (docChildNode.Name.ToLower() != "entry")
                {
                    continue;
                }
                MyNugetPackage package = parseXmlNodeForPackageDetails(dependencyPkg, docChildNode);
                if (package == null)
                {
                    continue;
                }
                foundPackagesList.Add(package);  // Add found dependecy package details

                // Check if looking for only 1 depenedncy
                if (dependencyPkg.IsExactVersion && dependencyPkg.VersionSpec != null && package.Version.CompareTo(dependencyPkg.VersionSpec.MaxVersion) == 0)
                {
                    break;
                }                                                                                                                                                          // Found what we were looking for - no need to search further more
            }

            return(foundPackagesList);
        }
Пример #7
0
        private static MyNugetPackage getBestMatchingDependencyPkgFromList(List <MyNugetPackage> foundPackagesList, MyNugetPackageDependency dependencyPkg)
        {
            "chocolatey".Log().Debug("Found Packages List:");
            foreach (var pkg in foundPackagesList)
            {
                "chocolatey".Log().Debug(" - " + pkg.ToString());
            }
            "chocolatey".Log().Debug("Determining which is best that satisfies dependency pkg version requirement: {0}".format_with(dependencyPkg.VersionSpec == null ? "null" : dependencyPkg.VersionSpec.ToString()));

            // If found a good candidate - get it
            foreach (MyNugetPackage candidatePkg in foundPackagesList)
            {
                if (candidatePkg.SatisfiesVersionSpec(dependencyPkg.VersionSpec))
                {
                    return(candidatePkg);
                }
            }
            return(null);
        }