Skip to content

Commit d742cd3

Browse files
committed
C#: Remove progress monitor from dependency fetcher, use logger directly
1 parent 13a8168 commit d742cd3

File tree

16 files changed

+190
-196
lines changed

16 files changed

+190
-196
lines changed

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

+11-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Collections.Generic;
22
using System.IO;
33
using System.Linq;
4+
using Semmle.Util.Logging;
45

56
namespace Semmle.Extraction.CSharp.DependencyFetching
67
{
@@ -18,10 +19,10 @@ internal class AssemblyCache
1819
/// Paths to search. Directories are searched recursively. Files are added directly to the
1920
/// assembly cache.
2021
/// </param>
21-
/// <param name="progressMonitor">Callback for progress.</param>
22-
public AssemblyCache(IEnumerable<string> paths, IEnumerable<string> frameworkPaths, ProgressMonitor progressMonitor)
22+
/// <param name="logger">Callback for progress.</param>
23+
public AssemblyCache(IEnumerable<string> paths, IEnumerable<string> frameworkPaths, ILogger logger)
2324
{
24-
this.progressMonitor = progressMonitor;
25+
this.logger = logger;
2526
foreach (var path in paths)
2627
{
2728
if (File.Exists(path))
@@ -32,12 +33,12 @@ public AssemblyCache(IEnumerable<string> paths, IEnumerable<string> frameworkPat
3233

3334
if (Directory.Exists(path))
3435
{
35-
progressMonitor.LogInfo($"Finding reference DLLs in {path}...");
36+
logger.LogInfo($"Finding reference DLLs in {path}...");
3637
AddReferenceDirectory(path);
3738
}
3839
else
3940
{
40-
progressMonitor.LogInfo("AssemblyCache: Path not found: " + path);
41+
logger.LogInfo("AssemblyCache: Path not found: " + path);
4142
}
4243
}
4344
IndexReferences(frameworkPaths);
@@ -63,15 +64,15 @@ private void AddReferenceDirectory(string dir)
6364
/// </summary>
6465
private void IndexReferences(IEnumerable<string> frameworkPaths)
6566
{
66-
progressMonitor.LogInfo($"Indexing {dllsToIndex.Count} assemblies...");
67+
logger.LogInfo($"Indexing {dllsToIndex.Count} assemblies...");
6768

6869
// Read all of the files
6970
foreach (var filename in dllsToIndex)
7071
{
7172
IndexReference(filename);
7273
}
7374

74-
progressMonitor.LogInfo($"Read {assemblyInfoByFileName.Count} assembly infos");
75+
logger.LogInfo($"Read {assemblyInfoByFileName.Count} assembly infos");
7576

7677
foreach (var info in assemblyInfoByFileName.Values
7778
.OrderBy(info => info.Name)
@@ -88,13 +89,13 @@ private void IndexReference(string filename)
8889
{
8990
try
9091
{
91-
progressMonitor.LogDebug($"Reading assembly info from {filename}");
92+
logger.LogDebug($"Reading assembly info from {filename}");
9293
var info = AssemblyInfo.ReadFromFile(filename);
9394
assemblyInfoByFileName[filename] = info;
9495
}
9596
catch (AssemblyLoadException)
9697
{
97-
progressMonitor.LogInfo($"Couldn't read assembly info from {filename}");
98+
logger.LogInfo($"Couldn't read assembly info from {filename}");
9899
}
99100
}
100101

@@ -168,6 +169,6 @@ public AssemblyInfo GetAssemblyInfo(string filepath)
168169

169170
private readonly HashSet<string> failedAssemblyInfoIds = new HashSet<string>();
170171

171-
private readonly ProgressMonitor progressMonitor;
172+
private readonly ILogger logger;
172173
}
173174
}

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

+15-14
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Linq;
66
using Newtonsoft.Json.Linq;
77
using Semmle.Util;
8+
using Semmle.Util.Logging;
89

910
namespace Semmle.Extraction.CSharp.DependencyFetching
1011
{
@@ -13,11 +14,11 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
1314
/// </summary>
1415
internal class Assets
1516
{
16-
private readonly ProgressMonitor progressMonitor;
17+
private readonly ILogger logger;
1718

18-
internal Assets(ProgressMonitor progressMonitor)
19+
internal Assets(ILogger logger)
1920
{
20-
this.progressMonitor = progressMonitor;
21+
this.logger = logger;
2122
}
2223

2324
/// <summary>
@@ -35,7 +36,7 @@ private record class ReferenceInfo(string? Type, Dictionary<string, object>? Com
3536

3637
/// <summary>
3738
/// Add the package dependencies from the assets file to dependencies.
38-
///
39+
///
3940
/// Parse a part of the JSon assets file and add the paths
4041
/// to the dependencies required for compilation (and collect
4142
/// information about used packages).
@@ -60,7 +61,7 @@ private record class ReferenceInfo(string? Type, Dictionary<string, object>? Com
6061
/// }
6162
/// }
6263
/// }
63-
///
64+
///
6465
/// Adds the following dependencies
6566
/// Paths: {
6667
/// "castle.core/4.4.1/lib/netstandard1.5/Castle.Core.dll",
@@ -85,7 +86,7 @@ private void AddPackageDependencies(JObject json, DependencyContainer dependenci
8586

8687
if (references is null)
8788
{
88-
progressMonitor.LogDebug("No references found in the targets section in the assets file.");
89+
logger.LogDebug("No references found in the targets section in the assets file.");
8990
return;
9091
}
9192

@@ -157,7 +158,7 @@ private void AddFrameworkDependencies(JObject json, DependencyContainer dependen
157158

158159
if (frameworks is null)
159160
{
160-
progressMonitor.LogDebug("No framework section in assets.json.");
161+
logger.LogDebug("No framework section in assets.json.");
161162
return;
162163
}
163164

@@ -171,7 +172,7 @@ private void AddFrameworkDependencies(JObject json, DependencyContainer dependen
171172

172173
if (references is null)
173174
{
174-
progressMonitor.LogDebug("No framework references in assets.json.");
175+
logger.LogDebug("No framework references in assets.json.");
175176
return;
176177
}
177178

@@ -196,12 +197,12 @@ public bool TryParse(string json, DependencyContainer dependencies)
196197
}
197198
catch (Exception e)
198199
{
199-
progressMonitor.LogDebug($"Failed to parse assets file (unexpected error): {e.Message}");
200+
logger.LogDebug($"Failed to parse assets file (unexpected error): {e.Message}");
200201
return false;
201202
}
202203
}
203204

204-
private static bool TryReadAllText(string path, ProgressMonitor progressMonitor, [NotNullWhen(returnValue: true)] out string? content)
205+
private static bool TryReadAllText(string path, ILogger logger, [NotNullWhen(returnValue: true)] out string? content)
205206
{
206207
try
207208
{
@@ -210,19 +211,19 @@ private static bool TryReadAllText(string path, ProgressMonitor progressMonitor,
210211
}
211212
catch (Exception e)
212213
{
213-
progressMonitor.LogInfo($"Failed to read assets file '{path}': {e.Message}");
214+
logger.LogInfo($"Failed to read assets file '{path}': {e.Message}");
214215
content = null;
215216
return false;
216217
}
217218
}
218219

219-
public static DependencyContainer GetCompilationDependencies(ProgressMonitor progressMonitor, IEnumerable<string> assets)
220+
public static DependencyContainer GetCompilationDependencies(ILogger logger, IEnumerable<string> assets)
220221
{
221-
var parser = new Assets(progressMonitor);
222+
var parser = new Assets(logger);
222223
var dependencies = new DependencyContainer();
223224
assets.ForEach(asset =>
224225
{
225-
if (TryReadAllText(asset, progressMonitor, out var json))
226+
if (TryReadAllText(asset, logger, out var json))
226227
{
227228
parser.TryParse(json, dependencies);
228229
}

0 commit comments

Comments
 (0)