Skip to content

Commit

Permalink
Add local package patterns to source-mappings for online feeds (#44076)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Simons <msimons@microsoft.com>
  • Loading branch information
NikolaMilosavljevic and MichaelSimons authored Oct 11, 2024
1 parent 1d61946 commit c9b7a34
Showing 1 changed file with 35 additions and 16 deletions.
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

0 comments on commit c9b7a34

Please sign in to comment.