1
- // Licensed to the .NET Foundation under one or more agreements.
1
+ // Licensed to the .NET Foundation under one or more agreements.
2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
// See the LICENSE file in the project root for more information.
4
4
5
+ #nullable enable
6
+
5
7
using System ;
6
8
using System . Collections . Generic ;
7
9
using System . IO ;
@@ -21,7 +23,7 @@ namespace Microsoft.DotNet.UnifiedBuild.Tasks
21
23
public class RemoveInternetSourcesFromNuGetConfig : Task
22
24
{
23
25
[ Required ]
24
- public string NuGetConfigFile { get ; set ; }
26
+ public required string NuGetConfigFile { get ; set ; }
25
27
26
28
/// <summary>
27
29
/// Whether to work in offline mode (remove all internet sources) or online mode (remove only authenticated sources)
@@ -33,54 +35,73 @@ public class RemoveInternetSourcesFromNuGetConfig : Task
33
35
/// example, a source named 'darc-pub-dotnet-aspnetcore-e81033e' will be kept if the prefix
34
36
/// 'darc-pub-dotnet-aspnetcore-' is in this list.
35
37
/// </summary>
36
- public string [ ] KeepFeedPrefixes { get ; set ; }
38
+ public string [ ] KeepFeedPrefixes { get ; set ; } = [ ] ;
39
+
40
+ private readonly string [ ] Sections = [ "packageSources" , "auditSources" ] ;
37
41
38
42
public override bool Execute ( )
39
43
{
40
44
string xml = File . ReadAllText ( NuGetConfigFile ) ;
41
45
string newLineChars = FileUtilities . DetectNewLineChars ( xml ) ;
42
46
XDocument d = XDocument . Parse ( xml ) ;
43
- XElement packageSourcesElement = d . Root . Descendants ( ) . First ( e => e . Name == "packageSources" ) ;
44
- XElement disabledPackageSourcesElement = d . Root . Descendants ( ) . FirstOrDefault ( e => e . Name == "disabledPackageSources" ) ;
47
+ XElement ? disabledPackageSourcesElement = d . Root ? . Descendants ( ) . FirstOrDefault ( e => e . Name == "disabledPackageSources" ) ;
48
+
49
+ foreach ( string sectionName in Sections )
50
+ {
51
+ ProcessSection ( d , sectionName ) ;
52
+ }
53
+
54
+ // Remove disabledPackageSources element so if any internal packages remain, they are used in source-build
55
+ disabledPackageSourcesElement ? . ReplaceNodes ( new XElement ( "clear" ) ) ;
56
+
57
+ using ( var w = XmlWriter . Create ( NuGetConfigFile , new XmlWriterSettings { NewLineChars = newLineChars , Indent = true } ) )
58
+ {
59
+ d . Save ( w ) ;
60
+ }
61
+
62
+ return true ;
63
+ }
45
64
46
- IEnumerable < XElement > local = packageSourcesElement . Descendants ( ) . Where ( e =>
65
+ private void ProcessSection ( XDocument d , string sectionName )
66
+ {
67
+ XElement ? sectionElement = d . Root ? . Descendants ( ) . FirstOrDefault ( e => e . Name == sectionName ) ;
68
+ if ( sectionElement == null )
69
+ {
70
+ return ;
71
+ }
72
+
73
+ IEnumerable < XElement > local = sectionElement . Descendants ( ) . Where ( e =>
47
74
{
48
75
if ( e . Name == "add" )
49
76
{
50
- string feedName = e . Attribute ( "key" ) . Value ;
51
- if ( KeepFeedPrefixes
77
+ string ? feedName = e . Attribute ( "key" ) ? . Value ;
78
+ if ( feedName != null &&
79
+ KeepFeedPrefixes
52
80
? . Any ( prefix => feedName . StartsWith ( prefix , StringComparison . OrdinalIgnoreCase ) )
53
81
== true )
54
82
{
55
83
return true ;
56
84
}
57
85
58
- string feedUrl = e . Attribute ( "value" ) . Value ;
59
- if ( BuildWithOnlineFeeds )
86
+ string ? feedUrl = e . Attribute ( "value" ) ? . Value ;
87
+ if ( feedUrl != null )
60
88
{
61
- return ! ( feedUrl . StartsWith ( "https://pkgs.dev.azure.com/dnceng/_packaging" , StringComparison . OrdinalIgnoreCase ) ||
62
- feedUrl . StartsWith ( "https://pkgs.dev.azure.com/dnceng/internal/_packaging" , StringComparison . OrdinalIgnoreCase ) ) ;
63
- }
64
- else
65
- {
66
- return ! ( feedUrl . StartsWith ( "http://" , StringComparison . OrdinalIgnoreCase ) || feedUrl . StartsWith ( "https://" , StringComparison . OrdinalIgnoreCase ) ) ;
89
+ if ( BuildWithOnlineFeeds )
90
+ {
91
+ return ! ( feedUrl . StartsWith ( "https://pkgs.dev.azure.com/dnceng/_packaging" , StringComparison . OrdinalIgnoreCase ) ||
92
+ feedUrl . StartsWith ( "https://pkgs.dev.azure.com/dnceng/internal/_packaging" , StringComparison . OrdinalIgnoreCase ) ) ;
93
+ }
94
+ else
95
+ {
96
+ return ! ( feedUrl . StartsWith ( "http://" , StringComparison . OrdinalIgnoreCase ) || feedUrl . StartsWith ( "https://" , StringComparison . OrdinalIgnoreCase ) ) ;
97
+ }
67
98
}
68
99
}
69
100
70
101
return true ;
71
102
} ) ;
72
103
73
- packageSourcesElement . ReplaceNodes ( local . ToArray ( ) ) ;
74
-
75
- // Remove disabledPackageSources element so if any internal packages remain, they are used in source-build
76
- disabledPackageSourcesElement ? . ReplaceNodes ( new XElement ( "clear" ) ) ;
77
-
78
- using ( var w = XmlWriter . Create ( NuGetConfigFile , new XmlWriterSettings { NewLineChars = newLineChars , Indent = true } ) )
79
- {
80
- d . Save ( w ) ;
81
- }
82
-
83
- return true ;
104
+ sectionElement . ReplaceNodes ( local . ToArray ( ) ) ;
84
105
}
85
106
}
86
- }
107
+ }
0 commit comments