Skip to content

Commit c9b7a34

Browse files
Add local package patterns to source-mappings for online feeds (#44076)
Co-authored-by: Michael Simons <msimons@microsoft.com>
1 parent 1d61946 commit c9b7a34

File tree

1 file changed

+35
-16
lines changed

1 file changed

+35
-16
lines changed

src/SourceBuild/content/eng/tools/tasks/Microsoft.DotNet.UnifiedBuild.Tasks/UpdateNuGetConfigPackageSourcesMappings.cs

+35-16
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ public class UpdateNuGetConfigPackageSourcesMappings : Task
6262
private Dictionary<string, List<string>> allSourcesPackages = [];
6363
private Dictionary<string, List<string>> oldSourceMappingPatterns = [];
6464

65+
// allOldSourceMappingPatterns is a union of all patterns from oldSourceMappingPatterns
66+
List<string> allOldSourceMappingPatterns = [];
67+
6568
// All other dictionaries are: 'package id', 'list of package versions'
6669
private Dictionary<string, List<string>> currentPackages = [];
6770
private Dictionary<string, List<string>> referencePackages = [];
@@ -136,14 +139,17 @@ public override bool Execute()
136139
}
137140
}
138141

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

156162
AddMappingsForCustomSources(pkgSrcMappingElement, pkgSourcesElement, packagePatterns);
157-
158-
if (oldSourceMappingPatterns.Count == 0)
159-
{
160-
AddMappingsForOnlineSources(pkgSrcMappingElement, pkgSourcesElement, packagePatterns);
161-
}
163+
AddMappingsForOnlineSources(pkgSrcMappingElement, pkgSourcesElement, packagePatterns);
162164
}
163165

164166
using (var writer = XmlWriter.Create(NuGetConfigFile, new XmlWriterSettings { NewLineChars = newLineChars, Indent = true }))
@@ -180,28 +182,41 @@ private void AddMappingsForCustomSources(XElement pkgSrcMappingElement, XElement
180182
{
181183
if (null != GetElement(pkgSourcesElement, "add", sourceName))
182184
{
183-
ReplaceSourceMappings(pkgSrcMappingElement, sourceName, packagePatterns);
185+
AddSourceMappings(pkgSrcMappingElement, sourceName, packagePatterns);
186+
187+
// Add all old source mapping patterns for custom sources.
188+
// Unlike local sources, custom sources cannot be enumerated.
189+
XElement pkgSrcElement = GetElement(pkgSrcMappingElement, "packageSource", sourceName);
190+
if (pkgSrcElement != null)
191+
{
192+
foreach (string pattern in allOldSourceMappingPatterns)
193+
{
194+
pkgSrcElement.Add(new XElement("package", new XAttribute("pattern", pattern)));
195+
}
196+
}
184197
}
185198
}
186199
}
187200

188-
private void ReplaceSourceMappings(XElement pkgSrcMappingElement, string sourceName, List<string> packagePatterns)
201+
private void AddSourceMappings(XElement pkgSrcMappingElement, string sourceName, List<string> packagePatterns)
189202
{
190-
XElement pkgSrc = new XElement("packageSource", new XAttribute("key", sourceName));
191-
foreach (string packagePattern in packagePatterns)
192-
{
193-
pkgSrc.Add(new XElement("package", new XAttribute("pattern", packagePattern)));
194-
}
203+
XElement pkgSrc;
195204

196205
XElement existingPkgSrcElement = GetElement(pkgSrcMappingElement, "packageSource", sourceName);
197206
if (existingPkgSrcElement != null)
198207
{
199-
existingPkgSrcElement.ReplaceWith(pkgSrc);
208+
pkgSrc = existingPkgSrcElement;
200209
}
201210
else
202211
{
212+
pkgSrc = new XElement("packageSource", new XAttribute("key", sourceName));
203213
pkgSrcMappingElement.Add(pkgSrc);
204214
}
215+
216+
foreach (string packagePattern in packagePatterns)
217+
{
218+
pkgSrc.Add(new XElement("package", new XAttribute("pattern", packagePattern)));
219+
}
205220
}
206221

207222
private void AddMappingsForOnlineSources(XElement pkgSrcMappingElement, XElement pkgSourcesElement, List<string> packagePatterns)
@@ -215,7 +230,7 @@ private void AddMappingsForOnlineSources(XElement pkgSrcMappingElement, XElement
215230
.Select(e => e.Attribute("key").Value)
216231
.Distinct())
217232
{
218-
ReplaceSourceMappings(pkgSrcMappingElement, sourceName, packagePatterns);
233+
AddSourceMappings(pkgSrcMappingElement, sourceName, packagePatterns);
219234
}
220235
}
221236

@@ -377,6 +392,10 @@ private void GetExistingFilteredSourceMappings(XElement pkgSrcMappingElement)
377392
!prebuiltPackages.ContainsKey(pattern))
378393
{
379394
filteredPatterns.Add(pattern);
395+
if (!allOldSourceMappingPatterns.Contains(pattern))
396+
{
397+
allOldSourceMappingPatterns.Add(pattern);
398+
}
380399
}
381400
}
382401

0 commit comments

Comments
 (0)