Skip to content

Commit 646b272

Browse files
committed
C#: Move the AssemblyPath class to its own file.
1 parent 6299d9c commit 646b272

File tree

2 files changed

+81
-76
lines changed

2 files changed

+81
-76
lines changed

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

Lines changed: 1 addition & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,9 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.IO;
1+
using System.Collections.Generic;
42
using System.Linq;
53
using Semmle.Util.Logging;
64

75
namespace Semmle.Extraction.CSharp.DependencyFetching
86
{
9-
/// <summary>
10-
/// Used to represent a path to an assembly or a directory containing assemblies
11-
/// and a selector function to determine which files to include, when indexing the assemblies.
12-
/// </summary>
13-
internal sealed class AssemblyPath(string p, Func<string, bool> includeFileName)
14-
{
15-
public string Path => p;
16-
17-
public AssemblyPath(string p) : this(p, _ => true) { }
18-
19-
public static implicit operator AssemblyPath(string path) => new(path);
20-
21-
/// <summary>
22-
/// Finds all assemblies nested within the directory `path`
23-
/// and adds them to the a list of assembly names to index.
24-
/// Indexing is performed at a later stage. This only collects the names.
25-
/// </summary>
26-
/// <param name="dir">The directory to index.</param>
27-
private void AddReferenceDirectory(List<string> dllsToIndex, ILogger logger)
28-
{
29-
foreach (var dll in new DirectoryInfo(p).EnumerateFiles("*.dll", SearchOption.AllDirectories))
30-
{
31-
if (includeFileName(dll.Name))
32-
{
33-
dllsToIndex.Add(dll.FullName);
34-
}
35-
else
36-
{
37-
logger.LogInfo($"AssemblyPath: Skipping {dll.FullName}.");
38-
}
39-
}
40-
}
41-
42-
/// <summary>
43-
/// Finds all assemblies in `p` that should be indexed and adds them to
44-
/// the list of assembly names to index.
45-
/// </summary>
46-
/// <param name="dllsToIndex">List of assembly names to index.</param>
47-
/// <param name="logger">Logger</param>
48-
public void Process(List<string> dllsToIndex, ILogger logger)
49-
{
50-
if (File.Exists(p))
51-
{
52-
if (includeFileName(System.IO.Path.GetFileName(p)))
53-
{
54-
dllsToIndex.Add(p);
55-
}
56-
else
57-
{
58-
logger.LogInfo($"AssemblyPath: Skipping {p}.");
59-
}
60-
return;
61-
}
62-
63-
if (Directory.Exists(p))
64-
{
65-
logger.LogInfo($"AssemblyPath: Finding reference DLLs in {p}...");
66-
AddReferenceDirectory(dllsToIndex, logger);
67-
}
68-
else
69-
{
70-
logger.LogInfo("AssemblyCache: Path not found: " + p);
71-
}
72-
}
73-
74-
public override bool Equals(object? obj) =>
75-
obj is AssemblyPath ap && p.Equals(ap.Path);
76-
77-
public override int GetHashCode() => p.GetHashCode();
78-
79-
public override string ToString() => p;
80-
}
81-
827
/// <summary>
838
/// Manages the set of assemblies.
849
/// Searches for assembly DLLs, indexes them and provides
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using Semmle.Util.Logging;
5+
6+
namespace Semmle.Extraction.CSharp.DependencyFetching
7+
{
8+
/// <summary>
9+
/// Used to represent a path to an assembly or a directory containing assemblies
10+
/// and a selector function to determine which files to include, when indexing the assemblies.
11+
/// </summary>
12+
internal sealed class AssemblyPath(string p, Func<string, bool> includeFileName)
13+
{
14+
public string Path => p;
15+
16+
public AssemblyPath(string p) : this(p, _ => true) { }
17+
18+
public static implicit operator AssemblyPath(string path) => new(path);
19+
20+
/// <summary>
21+
/// Finds all assemblies nested within the directory `path`
22+
/// and adds them to the a list of assembly names to index.
23+
/// Indexing is performed at a later stage. This only collects the names.
24+
/// </summary>
25+
/// <param name="dir">The directory to index.</param>
26+
private void AddReferenceDirectory(List<string> dllsToIndex, ILogger logger)
27+
{
28+
foreach (var dll in new DirectoryInfo(p).EnumerateFiles("*.dll", SearchOption.AllDirectories))
29+
{
30+
if (includeFileName(dll.Name))
31+
{
32+
dllsToIndex.Add(dll.FullName);
33+
}
34+
else
35+
{
36+
logger.LogInfo($"AssemblyPath: Skipping {dll.FullName}.");
37+
}
38+
}
39+
}
40+
41+
/// <summary>
42+
/// Finds all assemblies in `p` that should be indexed and adds them to
43+
/// the list of assembly names to index.
44+
/// </summary>
45+
/// <param name="dllsToIndex">List of assembly names to index.</param>
46+
/// <param name="logger">Logger</param>
47+
public void Process(List<string> dllsToIndex, ILogger logger)
48+
{
49+
if (File.Exists(p))
50+
{
51+
if (includeFileName(System.IO.Path.GetFileName(p)))
52+
{
53+
dllsToIndex.Add(p);
54+
}
55+
else
56+
{
57+
logger.LogInfo($"AssemblyPath: Skipping {p}.");
58+
}
59+
return;
60+
}
61+
62+
if (Directory.Exists(p))
63+
{
64+
logger.LogInfo($"AssemblyPath: Finding reference DLLs in {p}...");
65+
AddReferenceDirectory(dllsToIndex, logger);
66+
}
67+
else
68+
{
69+
logger.LogInfo("AssemblyCache: Path not found: " + p);
70+
}
71+
}
72+
73+
public override bool Equals(object? obj) =>
74+
obj is AssemblyPath ap && p.Equals(ap.Path);
75+
76+
public override int GetHashCode() => p.GetHashCode();
77+
78+
public override string ToString() => p;
79+
}
80+
}

0 commit comments

Comments
 (0)