Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add local package patterns to source-mappings for online feeds #44076

Merged
merged 4 commits into from
Oct 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ public class UpdateNuGetConfigPackageSourcesMappings : Task
private Dictionary<string, List<string>> allSourcesPackages = [];
private Dictionary<string, List<string>> oldSourceMappingPatterns = [];

// allOldSourceMappingPatterns is a union of all patterns from oldSourceMappingPatterns
List<string> allOldSourceMappingPatterns = [];

// All other dictionaries are: 'package id', 'list of package versions'
private Dictionary<string, List<string>> currentPackages = [];
private Dictionary<string, List<string>> referencePackages = [];
Expand Down Expand Up @@ -136,14 +139,17 @@ public override bool Execute()
}
}

// Union all package sources to get the distinct list. These will get added to
// Union all package sources to get the distinct list. Remove all original patterns
// from online feeds that were unique to those feeds.
//
// These will get added to
// all custom sources and all online sources based on the following logic:
// If there were existing mappings for online feeds, add cummulative mappings
// from all feeds to these two.
// If there were no existing mappings, add default mappings for all online feeds.
List<string> packagePatterns = pkgSrcMappingElement.Descendants()
.Where(e => e.Name == "packageSource")
.SelectMany(e => e.Descendants().Where(e => e.Name == "package"))
.SelectMany(e => e.Descendants().Where(e => e.Name == "package" && !allOldSourceMappingPatterns.Contains(e.Attribute("pattern").Value)))
.Select(e => e.Attribute("pattern").Value)
.Distinct()
.ToList();
Expand All @@ -154,11 +160,7 @@ public override bool Execute()
}

AddMappingsForCustomSources(pkgSrcMappingElement, pkgSourcesElement, packagePatterns);

if (oldSourceMappingPatterns.Count == 0)
{
AddMappingsForOnlineSources(pkgSrcMappingElement, pkgSourcesElement, packagePatterns);
}
AddMappingsForOnlineSources(pkgSrcMappingElement, pkgSourcesElement, packagePatterns);
}

using (var writer = XmlWriter.Create(NuGetConfigFile, new XmlWriterSettings { NewLineChars = newLineChars, Indent = true }))
Expand All @@ -180,28 +182,41 @@ private void AddMappingsForCustomSources(XElement pkgSrcMappingElement, XElement
{
if (null != GetElement(pkgSourcesElement, "add", sourceName))
{
ReplaceSourceMappings(pkgSrcMappingElement, sourceName, packagePatterns);
AddSourceMappings(pkgSrcMappingElement, sourceName, packagePatterns);

// Add all old source mapping patterns for custom sources.
// Unlike local sources, custom sources cannot be enumerated.
XElement pkgSrcElement = GetElement(pkgSrcMappingElement, "packageSource", sourceName);
if (pkgSrcElement != null)
{
foreach (string pattern in allOldSourceMappingPatterns)
{
pkgSrcElement.Add(new XElement("package", new XAttribute("pattern", pattern)));
}
}
}
}
}

private void ReplaceSourceMappings(XElement pkgSrcMappingElement, string sourceName, List<string> packagePatterns)
private void AddSourceMappings(XElement pkgSrcMappingElement, string sourceName, List<string> packagePatterns)
{
XElement pkgSrc = new XElement("packageSource", new XAttribute("key", sourceName));
foreach (string packagePattern in packagePatterns)
{
pkgSrc.Add(new XElement("package", new XAttribute("pattern", packagePattern)));
}
XElement pkgSrc;

XElement existingPkgSrcElement = GetElement(pkgSrcMappingElement, "packageSource", sourceName);
if (existingPkgSrcElement != null)
{
existingPkgSrcElement.ReplaceWith(pkgSrc);
pkgSrc = existingPkgSrcElement;
}
else
{
pkgSrc = new XElement("packageSource", new XAttribute("key", sourceName));
pkgSrcMappingElement.Add(pkgSrc);
}

foreach (string packagePattern in packagePatterns)
{
pkgSrc.Add(new XElement("package", new XAttribute("pattern", packagePattern)));
}
}

private void AddMappingsForOnlineSources(XElement pkgSrcMappingElement, XElement pkgSourcesElement, List<string> packagePatterns)
Expand All @@ -215,7 +230,7 @@ private void AddMappingsForOnlineSources(XElement pkgSrcMappingElement, XElement
.Select(e => e.Attribute("key").Value)
.Distinct())
{
ReplaceSourceMappings(pkgSrcMappingElement, sourceName, packagePatterns);
AddSourceMappings(pkgSrcMappingElement, sourceName, packagePatterns);
}
}

Expand Down Expand Up @@ -377,6 +392,10 @@ private void GetExistingFilteredSourceMappings(XElement pkgSrcMappingElement)
!prebuiltPackages.ContainsKey(pattern))
{
filteredPatterns.Add(pattern);
if (!allOldSourceMappingPatterns.Contains(pattern))
{
allOldSourceMappingPatterns.Add(pattern);
}
}
}

Expand Down