Skip to content

Commit d62e888

Browse files
committed
C#: Code quality improvements.
1 parent 181a063 commit d62e888

File tree

3 files changed

+13
-16
lines changed

3 files changed

+13
-16
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ internal class Assets
1717
private readonly ILogger logger;
1818

1919
/// <summary>
20-
/// Contains the dependencies found in the parsed asset files.
20+
/// Contains the dependencies found in the parsed assets files.
2121
/// </summary>
2222
public DependencyContainer Dependencies { get; } = new();
2323

@@ -225,7 +225,7 @@ private static bool TryReadAllText(string path, ILogger logger, [NotNullWhen(ret
225225
/// <summary>
226226
/// Add the dependencies from the assets file to the dependencies.
227227
/// </summary>
228-
/// <param name="asset">Path to an asset file.</param>
228+
/// <param name="asset">Path to an assets file.</param>
229229
public void AddDependencies(string asset)
230230
{
231231
if (TryReadAllText(asset, logger, out var json))
@@ -237,7 +237,7 @@ public void AddDependencies(string asset)
237237
/// <summary>
238238
/// Add the dependencies from the assets files to the dependencies.
239239
/// </summary>
240-
/// <param name="assets">Collection of paths to asset files.</param>
240+
/// <param name="assets">Collection of paths to assets files.</param>
241241
public void AddDependenciesRange(IEnumerable<string> assets) =>
242242
assets.ForEach(AddDependencies);
243243
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public void Add(string package, string dependency)
4545
var p = package.Replace('/', Path.DirectorySeparatorChar);
4646
var d = dependency.Replace('/', Path.DirectorySeparatorChar);
4747

48-
// In most cases paths in asset files point to dll's or the empty _._ file.
48+
// In most cases paths in assets files point to dll's or the empty _._ file.
4949
// That is, for _._ we don't need to add anything.
5050
if (Path.GetFileName(d) == "_._")
5151
{
@@ -74,11 +74,11 @@ internal static class DependencyContainerExtensions
7474
/// <summary>
7575
/// Flatten a list of containers into a single container.
7676
/// </summary>
77-
public static DependencyContainer Flatten(this IEnumerable<DependencyContainer> container) =>
78-
container.Aggregate(new DependencyContainer(), (acc, c) =>
77+
public static DependencyContainer Flatten(this IEnumerable<DependencyContainer> containers, DependencyContainer init) =>
78+
containers.Aggregate(init, (acc, container) =>
7979
{
80-
acc.Paths.UnionWith(c.Paths);
81-
acc.Packages.UnionWith(c.Packages);
80+
acc.Paths.UnionWith(container.Paths);
81+
acc.Packages.UnionWith(container.Packages);
8282
return acc;
8383
});
8484
}

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Concurrent;
23
using System.Collections.Generic;
34
using System.IO;
45
using System.Linq;
@@ -148,8 +149,7 @@ public HashSet<AssemblyLookupLocation> Restore()
148149
var projects = fileProvider.Projects.Except(restoredProjects);
149150
RestoreProjects(projects, out var containers);
150151

151-
containers.Add(container);
152-
var dependencies = containers.Flatten();
152+
var dependencies = containers.Flatten(container);
153153

154154
var paths = dependencies
155155
.Paths
@@ -235,11 +235,11 @@ private IEnumerable<string> RestoreSolutions(out DependencyContainer dependencie
235235
/// Populates dependencies with the relative paths to the assets files generated by the restore.
236236
/// </summary>
237237
/// <param name="projects">A list of paths to project files.</param>
238-
private void RestoreProjects(IEnumerable<string> projects, out List<DependencyContainer> dependencies)
238+
private void RestoreProjects(IEnumerable<string> projects, out ConcurrentBag<DependencyContainer> dependencies)
239239
{
240240
var successCount = 0;
241241
var nugetSourceFailures = 0;
242-
List<DependencyContainer> collectedDependencies = [];
242+
ConcurrentBag<DependencyContainer> collectedDependencies = [];
243243
var sync = new object();
244244
var projectGroups = projects.GroupBy(Path.GetDirectoryName);
245245
Parallel.ForEach(projectGroups, new ParallelOptions { MaxDegreeOfParallelism = DependencyManager.Threads }, projectGroup =>
@@ -262,10 +262,7 @@ private void RestoreProjects(IEnumerable<string> projects, out List<DependencyCo
262262
}
263263
}
264264
}
265-
lock (sync)
266-
{
267-
collectedDependencies.Add(assets.Dependencies);
268-
}
265+
collectedDependencies.Add(assets.Dependencies);
269266
});
270267
dependencies = collectedDependencies;
271268
compilationInfoContainer.CompilationInfos.Add(("Successfully restored project files", successCount.ToString()));

0 commit comments

Comments
 (0)