@@ -62,6 +62,9 @@ public class UpdateNuGetConfigPackageSourcesMappings : Task
62
62
private Dictionary < string , List < string > > allSourcesPackages = [ ] ;
63
63
private Dictionary < string , List < string > > oldSourceMappingPatterns = [ ] ;
64
64
65
+ // allOldSourceMappingPatterns is a union of all patterns from oldSourceMappingPatterns
66
+ List < string > allOldSourceMappingPatterns = [ ] ;
67
+
65
68
// All other dictionaries are: 'package id', 'list of package versions'
66
69
private Dictionary < string , List < string > > currentPackages = [ ] ;
67
70
private Dictionary < string , List < string > > referencePackages = [ ] ;
@@ -136,14 +139,17 @@ public override bool Execute()
136
139
}
137
140
}
138
141
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
140
146
// all custom sources and all online sources based on the following logic:
141
147
// If there were existing mappings for online feeds, add cummulative mappings
142
148
// from all feeds to these two.
143
149
// If there were no existing mappings, add default mappings for all online feeds.
144
150
List < string > packagePatterns = pkgSrcMappingElement . Descendants ( )
145
151
. 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 ) ) )
147
153
. Select ( e => e . Attribute ( "pattern" ) . Value )
148
154
. Distinct ( )
149
155
. ToList ( ) ;
@@ -154,11 +160,7 @@ public override bool Execute()
154
160
}
155
161
156
162
AddMappingsForCustomSources ( pkgSrcMappingElement , pkgSourcesElement , packagePatterns ) ;
157
-
158
- if ( oldSourceMappingPatterns . Count == 0 )
159
- {
160
- AddMappingsForOnlineSources ( pkgSrcMappingElement , pkgSourcesElement , packagePatterns ) ;
161
- }
163
+ AddMappingsForOnlineSources ( pkgSrcMappingElement , pkgSourcesElement , packagePatterns ) ;
162
164
}
163
165
164
166
using ( var writer = XmlWriter . Create ( NuGetConfigFile , new XmlWriterSettings { NewLineChars = newLineChars , Indent = true } ) )
@@ -180,28 +182,41 @@ private void AddMappingsForCustomSources(XElement pkgSrcMappingElement, XElement
180
182
{
181
183
if ( null != GetElement ( pkgSourcesElement , "add" , sourceName ) )
182
184
{
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
+ }
184
197
}
185
198
}
186
199
}
187
200
188
- private void ReplaceSourceMappings ( XElement pkgSrcMappingElement , string sourceName , List < string > packagePatterns )
201
+ private void AddSourceMappings ( XElement pkgSrcMappingElement , string sourceName , List < string > packagePatterns )
189
202
{
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 ;
195
204
196
205
XElement existingPkgSrcElement = GetElement ( pkgSrcMappingElement , "packageSource" , sourceName ) ;
197
206
if ( existingPkgSrcElement != null )
198
207
{
199
- existingPkgSrcElement . ReplaceWith ( pkgSrc ) ;
208
+ pkgSrc = existingPkgSrcElement ;
200
209
}
201
210
else
202
211
{
212
+ pkgSrc = new XElement ( "packageSource" , new XAttribute ( "key" , sourceName ) ) ;
203
213
pkgSrcMappingElement . Add ( pkgSrc ) ;
204
214
}
215
+
216
+ foreach ( string packagePattern in packagePatterns )
217
+ {
218
+ pkgSrc . Add ( new XElement ( "package" , new XAttribute ( "pattern" , packagePattern ) ) ) ;
219
+ }
205
220
}
206
221
207
222
private void AddMappingsForOnlineSources ( XElement pkgSrcMappingElement , XElement pkgSourcesElement , List < string > packagePatterns )
@@ -215,7 +230,7 @@ private void AddMappingsForOnlineSources(XElement pkgSrcMappingElement, XElement
215
230
. Select ( e => e . Attribute ( "key" ) . Value )
216
231
. Distinct ( ) )
217
232
{
218
- ReplaceSourceMappings ( pkgSrcMappingElement , sourceName , packagePatterns ) ;
233
+ AddSourceMappings ( pkgSrcMappingElement , sourceName , packagePatterns ) ;
219
234
}
220
235
}
221
236
@@ -377,6 +392,10 @@ private void GetExistingFilteredSourceMappings(XElement pkgSrcMappingElement)
377
392
! prebuiltPackages . ContainsKey ( pattern ) )
378
393
{
379
394
filteredPatterns . Add ( pattern ) ;
395
+ if ( ! allOldSourceMappingPatterns . Contains ( pattern ) )
396
+ {
397
+ allOldSourceMappingPatterns . Add ( pattern ) ;
398
+ }
380
399
}
381
400
}
382
401
0 commit comments