11using System ;
22using System . Collections . Generic ;
33using System . Collections . Immutable ;
4+ using System . IO ;
45using System . Linq ;
56using System . Net ;
67using System . Net . Http ;
@@ -20,23 +21,38 @@ internal sealed partial class FeedManager : IDisposable
2021
2122 private readonly ILogger logger ;
2223 private readonly IDotNet dotnet ;
24+ private readonly FileProvider fileProvider ;
2325 private readonly DependabotProxy ? dependabotProxy ;
2426 private readonly DependencyDirectory emptyPackageDirectory ;
2527
2628 public ImmutableHashSet < string > PrivateRegistryFeeds { get ; }
2729 public bool HasPrivateRegistryFeeds { get ; }
2830 public bool CheckNugetFeedResponsiveness { get ; } = EnvironmentVariables . GetBooleanOptOut ( EnvironmentVariableNames . CheckNugetFeedResponsiveness ) ;
2931
30- public FeedManager ( ILogger logger , IDotNet dotnet , DependabotProxy ? dependabotProxy )
32+ public FeedManager ( ILogger logger , IDotNet dotnet , DependabotProxy ? dependabotProxy , FileProvider fileProvider )
3133 {
3234 this . logger = logger ;
3335 this . dotnet = dotnet ;
3436 this . dependabotProxy = dependabotProxy ;
37+ this . fileProvider = fileProvider ;
3538 PrivateRegistryFeeds = dependabotProxy ? . RegistryURLs . ToImmutableHashSet ( ) ?? [ ] ;
3639 HasPrivateRegistryFeeds = PrivateRegistryFeeds . Count > 0 ;
3740 emptyPackageDirectory = new DependencyDirectory ( "empty" , "empty package" , logger ) ;
3841 }
3942
43+ private string ? GetDirectoryName ( string path )
44+ {
45+ try
46+ {
47+ return new FileInfo ( path ) . Directory ? . FullName ;
48+ }
49+ catch ( Exception exc )
50+ {
51+ logger . LogWarning ( $ "Failed to get directory of '{ path } ': { exc } ") ;
52+ }
53+ return null ;
54+ }
55+
4056 private IEnumerable < string > GetFeeds ( Func < IList < string > > getNugetFeeds )
4157 {
4258 var results = getNugetFeeds ( ) ;
@@ -65,11 +81,11 @@ private IEnumerable<string> GetFeeds(Func<IList<string>> getNugetFeeds)
6581 }
6682 }
6783
68- public IEnumerable < string > GetFeedsFromFolder ( string folderPath ) =>
84+ private IEnumerable < string > GetFeedsFromFolder ( string folderPath ) =>
6985 GetFeeds ( ( ) => dotnet . GetNugetFeedsFromFolder ( folderPath ) ) ;
7086
7187
72- public IEnumerable < string > GetFeedsFromNugetConfig ( string nugetConfigPath ) =>
88+ private IEnumerable < string > GetFeedsFromNugetConfig ( string nugetConfigPath ) =>
7389 GetFeeds ( ( ) => dotnet . GetNugetFeeds ( nugetConfigPath ) ) ;
7490
7591 private string FeedsToRestoreArgument ( IEnumerable < string > feeds )
@@ -108,7 +124,7 @@ private string FeedsToRestoreArgument(IEnumerable<string> feeds)
108124 }
109125
110126 // Find the path specific feeds.
111- var folder = FileUtils . GetDirectoryName ( path , logger ) ;
127+ var folder = GetDirectoryName ( path ) ;
112128 var feedsToConsider = folder is not null ? GetFeedsFromFolder ( folder ) . ToHashSet ( ) : new HashSet < string > ( ) ;
113129
114130 if ( HasPrivateRegistryFeeds )
@@ -282,7 +298,7 @@ public bool IsDefaultFeedReachable()
282298 /// <param name="isFallback">Whether the feeds are fallback feeds or not.</param>
283299 /// <param name="isTimeout">Whether a timeout occurred while checking the feeds.</param>
284300 /// <returns>The list of feeds that could be reached.</returns>
285- public List < string > GetReachableNuGetFeeds ( HashSet < string > feedsToCheck , bool isFallback , out bool isTimeout )
301+ private List < string > GetReachableNuGetFeeds ( HashSet < string > feedsToCheck , bool isFallback , out bool isTimeout )
286302 {
287303 var fallbackStr = isFallback ? "fallback " : "" ;
288304 logger . LogInfo ( $ "Checking { fallbackStr } NuGet feed reachability on feeds: { string . Join ( ", " , feedsToCheck . OrderBy ( f => f ) ) } ") ;
@@ -334,6 +350,58 @@ public List<string> GetReachableFallbackNugetFeeds(HashSet<string>? feedsFromNug
334350 return GetReachableNuGetFeeds ( fallbackFeeds , isFallback : true , out var _ ) ;
335351 }
336352
353+ public ( HashSet < string > explicitFeeds , HashSet < string > allFeeds ) GetAllFeeds ( )
354+ {
355+ var nugetConfigs = fileProvider . NugetConfigs ;
356+
357+ // Find feeds that are explicitly configured in the NuGet configuration files that we found.
358+ var explicitFeeds = nugetConfigs
359+ . SelectMany ( GetFeedsFromNugetConfig )
360+ . ToHashSet ( ) ;
361+
362+ if ( explicitFeeds . Count > 0 )
363+ {
364+ logger . LogInfo ( $ "Found { explicitFeeds . Count } NuGet feeds in nuget.config files: { string . Join ( ", " , explicitFeeds . OrderBy ( f => f ) ) } ") ;
365+ }
366+ else
367+ {
368+ logger . LogDebug ( "No NuGet feeds found in nuget.config files." ) ;
369+ }
370+
371+ // If private package registries are configured for C#, then consider those
372+ // in addition to the ones that are configured in `nuget.config` files.
373+ if ( HasPrivateRegistryFeeds )
374+ {
375+ logger . LogInfo ( $ "Found { PrivateRegistryFeeds . Count } private registry feeds configured for C#: { string . Join ( ", " , PrivateRegistryFeeds . OrderBy ( f => f ) ) } ") ;
376+ explicitFeeds . UnionWith ( PrivateRegistryFeeds ) ;
377+ }
378+
379+ HashSet < string > allFeeds = [ ] ;
380+
381+ // Add all explicitFeeds to the set of all feeds.
382+ allFeeds . UnionWith ( explicitFeeds ) ;
383+
384+ // Obtain the list of feeds from the root source directory.
385+ // If a NuGet file is present it will be respected, otherwise we will just get the machine/environment specific feeds.
386+ var nugetFeedsFromRoot = GetFeedsFromFolder ( fileProvider . SourceDir . FullName ) ;
387+ allFeeds . UnionWith ( nugetFeedsFromRoot ) ;
388+
389+ if ( nugetConfigs . Count > 0 )
390+ {
391+ var nugetConfigFeeds = nugetConfigs
392+ . Select ( GetDirectoryName )
393+ . Where ( folder => folder != null )
394+ . SelectMany ( folder => GetFeedsFromFolder ( folder ! ) )
395+ . ToHashSet ( ) ;
396+
397+ allFeeds . UnionWith ( nugetConfigFeeds ) ;
398+ }
399+
400+ logger . LogInfo ( $ "Found { allFeeds . Count } NuGet feeds (with inherited ones) in nuget.config files: { string . Join ( ", " , allFeeds . OrderBy ( f => f ) ) } ") ;
401+
402+ return ( explicitFeeds , allFeeds ) ;
403+ }
404+
337405 [ GeneratedRegex ( @"^E\s(.*)$" , RegexOptions . IgnoreCase | RegexOptions . Compiled | RegexOptions . Singleline ) ]
338406 private static partial Regex EnabledNugetFeed ( ) ;
339407
0 commit comments