Esempio n. 1
0
        public void Add(NugetDependency packageDependency)
        {
            var package = nuget.FindBestPackage(packageDependency.Name, packageDependency.VersionRange);

            if (package == null)
            {
                var version = packageDependency.VersionRange.MinVersion.ToNormalizedString();
                Console.WriteLine($"Nuget was unable to find the package '{packageDependency.Name}' with version range '{packageDependency.VersionRange}', assuming it is using version '{version}'");
                builder.AddOrUpdatePackage(new Model.PackageId(packageDependency.Name, version));
                return;
            }

            var packageId = new Model.PackageId(packageDependency.Name, package.Identity.Version.ToNormalizedString());
            HashSet <Model.PackageId> dependencies = new HashSet <Model.PackageId>();

            var packages = nuget.DependenciesForPackage(package.Identity, packageDependency.Framework);

            foreach (PackageDependency dependency in packages)
            {
                var bestExisting = builder.GetBestVersion(dependency.Id, dependency.VersionRange);
                if (bestExisting != null)
                {
                    var id = new Model.PackageId(dependency.Id, bestExisting);
                    dependencies.Add(id);
                }
                else
                {
                    var depPackage = nuget.FindBestPackage(dependency.Id, dependency.VersionRange);
                    if (depPackage == null)
                    {
                        Console.WriteLine($"Unable to find package for '{dependency.Id}' version '{dependency.VersionRange}'");
                        continue;
                    }

                    var id = new Model.PackageId(depPackage.Identity.Id, depPackage.Identity.Version.ToNormalizedString());
                    dependencies.Add(id);

                    if (!builder.DoesPackageExist(id))
                    {
                        Add(new NugetDependency(dependency.Id, dependency.VersionRange, packageDependency.Framework));
                    }
                }
            }


            builder.AddOrUpdatePackage(packageId, dependencies);
        }
        private void Resolve(string id, string name, NuGet.Frameworks.NuGetFramework framework = null, VersionRange overrideRange = null)
        {
            id = id.ToLower();
            ResolutionData data;

            if (resolutionData.ContainsKey(id))
            {
                data = resolutionData[id];
                if (overrideRange != null)
                {
                    if (data.ExternalVersionRange == null)
                    {
                        data.ExternalVersionRange = overrideRange;
                    }
                    else
                    {
                        throw new Exception("Can't set more than one external version range.");
                    }
                }
            }
            else
            {
                data = new ResolutionData();
                data.ExternalVersionRange = overrideRange;
                data.Name          = name;
                resolutionData[id] = data;
            }

            var allVersions = FindAllVersionRangesFor(id);

            if (data.ExternalVersionRange != null)
            {
                allVersions.Add(data.ExternalVersionRange);
            }
            var combo = VersionRange.CommonSubSet(allVersions);
            var best  = nuget.FindBestPackage(id, combo);

            if (best == null)
            {
                Console.WriteLine($"Unable to find package for '{id}' with range '{combo.ToString()}'. Likely a conflict exists in packages.config or the nuget metadata service configured incorrectly.");
                if (data.CurrentVersion == null)
                {
                    data.CurrentVersion = combo.MinVersion;
                }
                return;
            }

            if (data.CurrentVersion == best.Identity.Version)
            {
                return;
            }

            data.CurrentVersion = best.Identity.Version;
            data.Dependencies.Clear();

            var packages = nuget.DependenciesForPackage(best.Identity, framework);

            foreach (PackageDependency dependency in packages)
            {
                if (!data.Dependencies.ContainsKey(dependency.Id.ToLower()))
                {
                    data.Dependencies.Add(dependency.Id.ToLower(), dependency.VersionRange);
                    Resolve(dependency.Id.ToLower(), dependency.Id, framework);
                }
            }
        }