Ejemplo n.º 1
0
 public static void AddLibraryDependency(LibraryDependency dependency, ISet <LibraryDependency> list)
 {
     if (list.Any(r => r.Name == dependency.Name))
     {
         var          matchingDependency = list.Single(r => r.Name == dependency.Name);
         VersionRange newVersionRange    = VersionRange.CommonSubSet(new VersionRange[]
         {
             matchingDependency.LibraryRange.VersionRange, dependency.LibraryRange.VersionRange
         });
         if (!newVersionRange.Equals(VersionRange.None))
         {
             list.Remove(matchingDependency);
             list.Add(new LibraryDependency()
             {
                 LibraryRange   = new LibraryRange(matchingDependency.Name, newVersionRange, LibraryDependencyTarget.All),
                 IncludeType    = dependency.IncludeType & matchingDependency.IncludeType,
                 SuppressParent = dependency.SuppressParent & matchingDependency.SuppressParent
             });
         }
         else
         {
             throw new InvalidOperationException($"Package version constraints for {dependency.Name} return a version range that is empty.");
         }
     }
     else
     {
         list.Add(dependency);
     }
 }
Ejemplo n.º 2
0
        public static void AddPackageDependency(PackageDependency dependency, ISet <PackageDependency> set)
        {
            var matchingDependency = set.SingleOrDefault(r => r.Id == dependency.Id);

            if (matchingDependency != null)
            {
                VersionRange newVersionRange = VersionRange.CommonSubSet(new VersionRange[]
                {
                    matchingDependency.VersionRange, dependency.VersionRange
                });
                if (!newVersionRange.Equals(VersionRange.None))
                {
                    set.Remove(matchingDependency);
                    set.Add(new PackageDependency(dependency.Id, newVersionRange, dependency.Include, dependency.Exclude));
                }
                else
                {
                    throw new InvalidOperationException($"Package version constraints for {dependency.Id} return a version range that is empty.");
                }
            }
            else
            {
                set.Add(dependency);
            }
        }
Ejemplo n.º 3
0
        public void VersionRangeSet_CommonSubSet(string expected, string rangeA, string rangeB)
        {
            // Arrange
            var a = VersionRange.Parse(rangeA);
            var b = VersionRange.Parse(rangeB);

            // Act
            var ranges = new List <VersionRange>()
            {
                a, b
            };
            var combined = VersionRange.CommonSubSet(ranges);

            var rangesRev = new List <VersionRange>()
            {
                b, a
            };
            var combinedRev = VersionRange.CommonSubSet(rangesRev);

            // Assert
            Assert.Equal(expected, combined.ToNormalizedString());

            // Verify the order has no effect
            Assert.Equal(expected, combinedRev.ToNormalizedString());
        }
        private VersionRange GetAllowedVersions()
        {
            // allowed version ranges for selected list of projects
            var allowedVersionsRange = GetConstraintsForSelectedProjects()
                                       .Select(e => e.VersionRange)
                                       .Where(v => v != null);

            // if version constraints exist then merge all the ranges and return common range which satisfies all
            return(allowedVersionsRange.Any() ? VersionRange.CommonSubSet(allowedVersionsRange) : VersionRange.All);
        }
Ejemplo n.º 5
0
        public void VersionRangeSet_CommonSubSet_EmptyRangeList()
        {
            // Arrange
            var ranges = new List <VersionRange>()
            {
            };

            // Act
            var combined = VersionRange.CommonSubSet(ranges);

            // Assert
            Assert.Equal(VersionRange.None.ToNormalizedString(), combined.ToNormalizedString());
        }
Ejemplo n.º 6
0
        private VersionRange GetAllowedVersions()
        {
            // selected list of projects
            var selectedProjectsName = Projects.Where(p => p.IsSelected).Select(p => p.NuGetProject.GetMetadata <string>(NuGetProjectMetadataKeys.Name));

            // allowed version ranges for selected list of projects
            var allowedVersionsRange = _projectVersionRangeDict.Where(kvp => selectedProjectsName.Contains(kvp.Key, StringComparer.OrdinalIgnoreCase))
                                       .Select(kvp => kvp.Value);

            allowedVersionsRange = allowedVersionsRange.Where(v => v != null);

            // if version constraints exist then merge all the ranges and return common range which satisfies all
            return(allowedVersionsRange.Any() ? VersionRange.CommonSubSet(allowedVersionsRange) : VersionRange.All);
        }
Ejemplo n.º 7
0
        public void VersionRangeSet_CommonSubSet_SingleRangeList()
        {
            // Arrange
            var a      = VersionRange.Parse("[1.0.0, )");
            var ranges = new List <VersionRange>()
            {
                a
            };

            // Act
            var combined = VersionRange.CommonSubSet(ranges);

            // Assert
            Assert.Equal(a.ToNormalizedString(), combined.ToNormalizedString());
        }
Ejemplo n.º 8
0
        public void VersionRangeSet_CommonSubSetInMultipleRanges()
        {
            // Arrange
            var ranges = new List <VersionRange>()
            {
                VersionRange.Parse("[1.0.0, 5.0.0)"),
                VersionRange.Parse("[2.0.0, 6.0.0]"),
                VersionRange.Parse("[4.0.0, 5.0.0]"),
            };

            // Act
            var combined = VersionRange.CommonSubSet(ranges);

            // Assert
            Assert.Equal("[4.0.0, 5.0.0)", combined.ToNormalizedString());
        }
Ejemplo n.º 9
0
        public bool TryFindBestVersion(IEnumerable <string> versions, [MaybeNullWhen(false)] out string bestMatch)
        {
            if (versions is null)
            {
                throw new System.ArgumentNullException(nameof(versions));
            }

            var nugetVersions = new List <NuGetVersion>();
            var ranges        = new List <VersionRange>();

            foreach (var v in versions)
            {
                if (NuGetVersion.TryParse(v, out var parsedVersion))
                {
                    nugetVersions.Add(parsedVersion);
                }

                if (VersionRange.TryParse(v, out var range))
                {
                    ranges.Add(range);
                }
            }

            if (nugetVersions.Count == 0 && ranges.Count == 0)
            {
                bestMatch = null;
                return(false);
            }

            var finalRange = VersionRange.CommonSubSet(ranges);

            if (nugetVersions.Count > 0)
            {
                bestMatch = finalRange.FindBestMatch(nugetVersions).ToNormalizedString();
                return(true);
            }
            else
            {
                bestMatch = finalRange.MinVersion.ToNormalizedString();
                return(true);
            }
        }
Ejemplo n.º 10
0
 public void UpdateAllowedRange(VersionRange range)
 {
     Allowed = VersionRange.CommonSubSet(new[] { Allowed, range });
 }
Ejemplo n.º 11
0
        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);
                }
            }
        }
Ejemplo n.º 12
0
        private List <PackageDependencyGroup> GetDependencySets()
        {
            var dependencies = from d in Dependencies.NullAsEmpty()
                               select new
            {
                Id              = d.ItemSpec,
                Version         = d.GetVersion(),
                TargetFramework = d.GetTargetFramework()
            };

            return((from dependency in dependencies
                    group dependency by dependency.TargetFramework into dependenciesByFramework
                    let targetFramework = dependenciesByFramework.Key
                                          let packages = (from dependency in dependenciesByFramework
                                                          where dependency.Id != "_._"
                                                          group dependency by dependency.Id into dependenciesById
                                                          select new PackageDependency(dependenciesById.Key, VersionRange.CommonSubSet(dependenciesById.Select(x => x.Version))))
                                                         select new PackageDependencyGroup(targetFramework, packages)
                    ).ToList());
        }
Ejemplo n.º 13
0
        public void PartialRangesDoNotOverlap()
        {
            var errors = new List <string>();

            var partialGroups = Directory.GetFiles(TestSetup.DocsRootPath, "*.partial.md", SearchOption.AllDirectories)
                                .Select(path =>
            {
                var filename = Path.GetFileNameWithoutExtension(Path.GetFileNameWithoutExtension(path));
                var parts    = filename.Split('_');

                if (parts.Length != 4)
                {
                    errors.Add($"Partial {path} is invalid. Its name does not have 4 segments.");
                    return(null);
                }

                if (!TryParseVersionRange(parts[3], out var versionRange))
                {
                    errors.Add($"Partial {path} is invalid. Cannot parse the version into a valid VersionRange.");
                    return(null);
                }

                return(new
                {
                    Path = path,
                    DirectoryPath = Path.GetDirectoryName(path),
                    ParentFile = parts[0],
                    Name = parts[1],
                    Component = parts[2],
                    VersionsText = parts[3],
                    Versions = versionRange
                });
            })
                                .Where(partial => partial != null)
                                .GroupBy(partial => new { partial.DirectoryPath, partial.ParentFile, partial.Name, partial.Component })
                                // If only one partial, there can't be an overlap
                                .Where(group => group.Take(1).Any());

            foreach (var group in partialGroups)
            {
                var partials = group.ToArray();

                for (int i = 0; i < partials.Length; i++)
                {
                    for (int j = i + 1; j < partials.Length; j++)
                    {
                        var left   = partials[i];
                        var right  = partials[j];
                        var common = VersionRange.CommonSubSet(new[] { left.Versions, right.Versions });
                        if (common != VersionRange.None)
                        {
                            var k = group.Key;
                            errors.Add($"In {k.DirectoryPath}, there is a version overlap in partials for {k.ParentFile}_{k.Name}_{k.Component}:\r\n    - Partial Versions: '{left.VersionsText}' and '{right.VersionsText}'\r\n    - Parsed Versions: '{left.Versions}' and '{right.Versions}'");
                        }
                    }
                }
            }

            if (errors.Count > 0)
            {
                Assert.Fail($"Problems detected with partial version ranges which can cause the wrong partial to be rendered. Remember that upper ranges should use ) for an exclusive range, not ] which indicates an inclusive range.\r\n  > {string.Join("\r\n  > ", errors)}");
            }
        }