Repository navigation
Expand file tree
/
Copy pathDependencyContainer.cs
More file actions
83 lines (73 loc) · 2.64 KB
/
Copy pathDependencyContainer.cs
File metadata and controls
83 lines (73 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace Semmle.Extraction.CSharp.DependencyFetching
{
/// <summary>
/// Container class for dependencies found in the assets file.
/// </summary>
internal class DependencyContainer
{
/// <summary>
/// Paths to dependencies required for compilation.
/// </summary>
public HashSet<string> Paths { get; } = [];
/// <summary>
/// Packages that are used as a part of the required dependencies.
/// </summary>
public HashSet<string> Packages { get; } = [];
/// <summary>
/// If the path specifically adds a .dll we use that, otherwise we as a fallback
/// add the entire directory.
/// </summary>
private static string ParseFilePath(string path)
{
if (path.EndsWith(".dll"))
{
return path;
}
return Path.GetDirectoryName(path) ?? path;
}
private static string GetPackageName(string package) =>
package.Split(Path.DirectorySeparatorChar)[0];
/// <summary>
/// Add a dependency inside a package.
/// </summary>
public void Add(string package, string dependency)
{
var p = package.Replace('/', Path.DirectorySeparatorChar);
var d = dependency.Replace('/', Path.DirectorySeparatorChar);
// In most cases paths in assets files point to dll's or the empty _._ file.
// That is, for _._ we don't need to add anything.
if (Path.GetFileName(d) == "_._")
{
return;
}
var path = Path.Join(p, ParseFilePath(d));
Paths.Add(path);
Packages.Add(GetPackageName(p));
}
/// <summary>
/// Add a dependency to an entire framework package.
/// </summary>
public void AddFramework(string framework)
{
var p = framework.Replace('/', Path.DirectorySeparatorChar);
Paths.Add(p);
Packages.Add(GetPackageName(p));
}
}
internal static class DependencyContainerExtensions
{
/// <summary>
/// Flatten a list of containers into a single container.
/// </summary>
public static DependencyContainer Flatten(this IEnumerable<DependencyContainer> containers, DependencyContainer init) =>
containers.Aggregate(init, (acc, container) =>
{
acc.Paths.UnionWith(container.Paths);
acc.Packages.UnionWith(container.Packages);
return acc;
});
}
}