Skip to content

Commit 82cbf83

Browse files
committed
C#: Move feed prefix logic out of the feed manager.
1 parent d2d805a commit 82cbf83

4 files changed

Lines changed: 13 additions & 25 deletions

File tree

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DotNet.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ private List<string> GetRestoreArgs(RestoreSettings restoreSettings)
9090
args.Add("/p:EnableWindowsTargeting=true");
9191
}
9292

93-
args.AddRange(restoreSettings.NugetSources);
93+
var nugetSources = restoreSettings.NugetSources.SelectMany<string, string>(source => ["-s", source]).ToList();
94+
args.AddRange(nugetSources);
9495

9596
return args;
9697
}

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/FeedManager.cs

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -157,19 +157,16 @@ private IEnumerable<string> GetFeedsFromNugetConfig(string nugetConfigPath) =>
157157
/// If there are no feeds, a dummy source argument is added to override any default feeds that `restore` would use.
158158
/// </summary>
159159
/// <param name="feeds">The list of feeds to use for the restore command.</param>
160-
/// <param name="sourceArgumentPrefix">The prefix to use for each source argument (e.g., "-s").</param>
161160
/// <returns>The list of NuGet sources arguments for the restore command.</returns>
162-
public List<string> FeedsToRestoreArgument(IEnumerable<string> feeds, string sourceArgumentPrefix)
161+
public List<string> RestoreFeeds(IEnumerable<string> feeds)
163162
{
164163
// If there are no feeds, we want to override any default feeds that `restore` would use by passing a dummy source argument.
165164
if (!feeds.Any())
166165
{
167-
return [sourceArgumentPrefix, emptyPackageDirectory.DirInfo.FullName];
166+
return [emptyPackageDirectory.DirInfo.FullName];
168167
}
169168

170-
// Add package sources. If any are present, they override all sources specified in
171-
// the configuration file(s).
172-
return feeds.SelectMany<string, string>(feed => [sourceArgumentPrefix, feed]).ToList();
169+
return feeds.ToList();
173170
}
174171

175172
private IEnumerable<string> FeedsToUseAux(HashSet<string> feedsToConsider)
@@ -202,24 +199,14 @@ public IEnumerable<string> FeedsToUse(string path)
202199
return FeedsToUseAux(feedsToConsider);
203200
}
204201

205-
/// <summary>
206-
/// Constructs the NuGet sources argument for the `dotnet restore` command based on the given feeds.
207-
/// </summary>
208-
/// <param name="feeds">The list of NuGet feeds to use for the restore command.</param>
209-
/// <returns>A list representing the NuGet sources arguments for the `dotnet restore` command.</returns>
210-
public List<string> FeedsToDotnetRestoreArgument(IEnumerable<string> feeds)
211-
{
212-
return FeedsToRestoreArgument(feeds, "-s");
213-
}
214-
215202
/// <summary>
216203
/// Constructs the list of NuGet sources to use for dotnet restore.
217204
/// (1) Use the feeds we get from `dotnet nuget list source`
218205
/// (2) Use private registries, if they are configured
219206
/// </summary>
220207
/// <param name="path">Path to project/solution</param>
221208
/// <returns>A list representing the NuGet sources arguments for the `dotnet restore` command.</returns>
222-
public List<string> MakeDotnetRestoreSourcesArguments(string path)
209+
public List<string> MakeRestoreFeeds(string path)
223210
{
224211
// Do not construct a set of explicit NuGet sources to use for restore.
225212
if (!CheckNugetFeedResponsiveness && !HasPrivateRegistryFeeds)
@@ -229,7 +216,7 @@ public List<string> MakeDotnetRestoreSourcesArguments(string path)
229216

230217
var feedsToUse = FeedsToUse(path);
231218

232-
return FeedsToDotnetRestoreArgument(feedsToUse);
219+
return RestoreFeeds(feedsToUse);
233220
}
234221

235222
private (int initialTimeout, int tryCount) GetFeedRequestSettings(bool isFallback)

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/NugetPackageRestorer.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public NugetPackageRestorer(
5353
public string? TryRestore(string package)
5454
{
5555
var feeds = feedManager.CheckNugetFeedResponsiveness ? feedManager.ReachableFeeds : feedManager.AllFeeds;
56-
var nugetSources = feedManager.FeedsToDotnetRestoreArgument(feeds);
56+
var nugetSources = feedManager.RestoreFeeds(feeds);
5757
if (TryRestorePackageManually(package, nugetSources))
5858
{
5959
var packageDir = DependencyManager.GetPackageDirectory(package, missingPackageDirectory.DirInfo);
@@ -216,7 +216,7 @@ private IEnumerable<string> RestoreSolutions(out DependencyContainer dependencie
216216
var projects = fileProvider.Solutions.SelectMany(solution =>
217217
{
218218
logger.LogInfo($"Restoring solution {solution}...");
219-
var nugetSources = feedManager.MakeDotnetRestoreSourcesArguments(solution);
219+
var nugetSources = feedManager.MakeRestoreFeeds(solution);
220220
var res = dotnet.Restore(new(solution, PackageDirectory.DirInfo.FullName, ForceDotnetRefAssemblyFetching: true, NugetSources: nugetSources, TargetWindows: isWindows));
221221
if (res.Success)
222222
{
@@ -264,7 +264,7 @@ private void RestoreProjects(IEnumerable<string> projects, out ConcurrentBag<Dep
264264
foreach (var project in projectGroup)
265265
{
266266
logger.LogInfo($"Restoring project {project}...");
267-
var nugetSources = feedManager.MakeDotnetRestoreSourcesArguments(project);
267+
var nugetSources = feedManager.MakeRestoreFeeds(project);
268268
var res = dotnet.Restore(new(project, PackageDirectory.DirInfo.FullName, ForceDotnetRefAssemblyFetching: true, NugetSources: nugetSources, TargetWindows: isWindows));
269269
assets.AddDependenciesRange(res.AssetsFilePaths);
270270
lock (sync)
@@ -312,7 +312,7 @@ private void RestoreProjects(IEnumerable<string> projects, out ConcurrentBag<Dep
312312
feeds = feedManager.AllFeeds;
313313
}
314314

315-
var nugetSources = feedManager.FeedsToDotnetRestoreArgument(feeds);
315+
var nugetSources = feedManager.RestoreFeeds(feeds);
316316
var alreadyDownloadedPackages = usedPackageNames.Select(p => p.ToLowerInvariant());
317317
var alreadyDownloadedLegacyPackages = GetRestoredLegacyPackageNames();
318318

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/PackagesConfigRestorer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Collections.Immutable;
43
using System.Diagnostics;
54
using System.IO;
65
using System.Linq;
@@ -180,7 +179,8 @@ private bool TryRestoreNugetPackage(string packagesConfig)
180179
{
181180
feedsToUse.Add(FeedManager.PublicNugetOrgFeed);
182181
}
183-
sourcesArgument = feedManager.FeedsToRestoreArgument(feedsToUse, "-Source");
182+
var restoreFeeds = feedManager.RestoreFeeds(feedsToUse);
183+
sourcesArgument = restoreFeeds.SelectMany<string, string>(feed => ["-Source", feed]).ToList();
184184
}
185185

186186
/* Use nuget.exe to install a package.

0 commit comments

Comments
 (0)