forked from microsoft/component-detection
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDependencyGraph.cs
More file actions
265 lines (212 loc) · 9.43 KB
/
Copy pathDependencyGraph.cs
File metadata and controls
265 lines (212 loc) · 9.43 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#nullable disable
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using Microsoft.ComponentDetection.Contracts;
using Microsoft.ComponentDetection.Contracts.BcdeModels;
using Microsoft.ComponentDetection.Contracts.TypedComponent;
[assembly: InternalsVisibleTo("Microsoft.ComponentDetection.Common.Tests")]
namespace Microsoft.ComponentDetection.Common.DependencyGraph;
internal class DependencyGraph : IDependencyGraph
{
private static readonly CompositeFormat MissingNodeFormat = CompositeFormat.Parse(Resources.MissingNodeInDependencyGraph);
private readonly ConcurrentDictionary<string, ComponentRefNode> componentNodes;
private readonly bool enableManualTrackingOfExplicitReferences;
public DependencyGraph(bool enableManualTrackingOfExplicitReferences)
{
this.componentNodes = new ConcurrentDictionary<string, ComponentRefNode>();
this.enableManualTrackingOfExplicitReferences = enableManualTrackingOfExplicitReferences;
}
internal ConcurrentDictionary<string, byte> AdditionalRelatedFiles { get; } = new ConcurrentDictionary<string, byte>();
public void AddComponent(ComponentRefNode componentNode, string parentComponentId = null)
{
ArgumentNullException.ThrowIfNull(componentNode);
if (string.IsNullOrWhiteSpace(componentNode.Id))
{
throw new ArgumentNullException(nameof(componentNode), "Invalid component node id");
}
this.componentNodes.AddOrUpdate(componentNode.Id, componentNode, (key, currentNode) =>
{
currentNode.IsExplicitReferencedDependency |= componentNode.IsExplicitReferencedDependency;
// If incoming component has a dev dependency value, and it with whatever is in storage. Otherwise, leave storage alone.
if (componentNode.IsDevelopmentDependency.HasValue)
{
currentNode.IsDevelopmentDependency = currentNode.IsDevelopmentDependency.GetValueOrDefault(true) && componentNode.IsDevelopmentDependency.Value;
}
if (componentNode.DependencyScope.HasValue)
{
currentNode.DependencyScope = DependencyScopeComparer.GetMergedDependencyScope(currentNode.DependencyScope, componentNode.DependencyScope);
}
return currentNode;
});
this.AddDependency(componentNode.Id, parentComponentId);
}
public bool Contains(string componentId)
{
return this.componentNodes.ContainsKey(componentId);
}
public ICollection<string> GetDependenciesForComponent(string componentId)
{
return this.componentNodes[componentId].DependencyIds;
}
public ICollection<string> GetExplicitReferencedDependencyIds(string componentId)
{
if (string.IsNullOrWhiteSpace(componentId))
{
throw new ArgumentNullException(nameof(componentId));
}
if (!this.componentNodes.TryGetValue(componentId, out var componentRef))
{
throw new ArgumentException(string.Format(null, MissingNodeFormat, componentId), paramName: nameof(componentId));
}
IList<string> explicitReferencedDependencyIds = [];
this.GetExplicitReferencedDependencies(componentRef, explicitReferencedDependencyIds, new HashSet<string>());
return explicitReferencedDependencyIds;
}
/// <summary>
/// Any file added here will be reported as a location on ALL components found in current graph.
/// </summary>
public void AddAdditionalRelatedFile(string additionalRelatedFile)
{
this.AdditionalRelatedFiles.AddOrUpdate(additionalRelatedFile, 0, (notUsed, notUsed2) => 0);
}
public HashSet<string> GetAdditionalRelatedFiles()
{
return this.AdditionalRelatedFiles.Keys.ToHashSet();
}
public bool HasComponents()
{
return !this.componentNodes.IsEmpty;
}
public bool? IsDevelopmentDependency(string componentId)
{
return this.componentNodes[componentId].IsDevelopmentDependency;
}
public DependencyScope? GetDependencyScope(string componentId)
{
return this.componentNodes[componentId].DependencyScope;
}
public IEnumerable<string> GetAllExplicitlyReferencedComponents()
{
return this.componentNodes.Values
.Where(componentRefNode => this.IsExplicitReferencedDependency(componentRefNode))
.Select(componentRefNode => componentRefNode.Id);
}
public ICollection<string> GetAncestors(string componentId)
{
ArgumentNullException.ThrowIfNull(componentId);
if (!this.componentNodes.TryGetValue(componentId, out var componentRef))
{
// this component isn't in the graph, so it has no ancestors
return [];
}
// store the component id and the depth we found it at
var ancestors = new Dictionary<string, int>();
this.GetAncestorsRecursive(componentRef, ancestors, 1);
return ancestors.OrderBy(x => x.Value)
.Select(x => x.Key)
.Where(x => !x.Equals(componentId))
.ToList();
}
public HashSet<TypedComponent> GetAncestorsAsTypedComponents(string componentId, Func<string, TypedComponent> toTypedComponent)
{
ArgumentNullException.ThrowIfNull(componentId);
return this.GetAncestors(componentId)
.Select(a => this.componentNodes.TryGetValue(a, out var component) ? component : null)
.Where(a => a != null)
.Select(a => a.TypedComponent ?? toTypedComponent(a.Id))
.ToHashSet(new ComponentComparer());
}
public HashSet<TypedComponent> GetRootsAsTypedComponents(string componentId, Func<string, TypedComponent> toTypedComponent)
{
ArgumentNullException.ThrowIfNull(componentId);
return this.GetExplicitReferencedDependencyIds(componentId)
.Select(r => this.componentNodes.TryGetValue(r, out var component) ? component : null)
.Where(r => r != null)
.Select(r => r.TypedComponent ?? toTypedComponent(r.Id))
.ToHashSet(new ComponentComparer());
}
public void FillTypedComponents(Func<string, TypedComponent> toTypedComponent)
{
foreach (var componentId in this.componentNodes.Values)
{
componentId.TypedComponent = toTypedComponent(componentId.Id);
}
}
IEnumerable<string> IDependencyGraph.GetDependenciesForComponent(string componentId)
{
return this.GetDependenciesForComponent(componentId).ToImmutableList();
}
IEnumerable<string> IDependencyGraph.GetComponents()
{
return this.componentNodes.Keys.ToImmutableList();
}
bool IDependencyGraph.IsComponentExplicitlyReferenced(string componentId)
{
return this.IsExplicitReferencedDependency(this.componentNodes[componentId]);
}
private void GetExplicitReferencedDependencies(ComponentRefNode component, IList<string> explicitReferencedDependencyIds, ISet<string> visited)
{
if (this.IsExplicitReferencedDependency(component))
{
explicitReferencedDependencyIds.Add(component.Id);
}
visited.Add(component.Id);
foreach (var parentId in component.DependedOnByIds)
{
if (!visited.Contains(parentId))
{
this.GetExplicitReferencedDependencies(this.componentNodes[parentId], explicitReferencedDependencyIds, visited);
}
}
}
private bool IsExplicitReferencedDependency(ComponentRefNode component)
{
return (this.enableManualTrackingOfExplicitReferences && component.IsExplicitReferencedDependency) ||
(!this.enableManualTrackingOfExplicitReferences && !component.DependedOnByIds.Any());
}
private void AddDependency(string componentId, string parentComponentId)
{
if (string.IsNullOrWhiteSpace(parentComponentId))
{
return;
}
if (!this.componentNodes.TryGetValue(parentComponentId, out var parentComponentRefNode))
{
throw new ArgumentException(string.Format(null, MissingNodeFormat, parentComponentId), nameof(parentComponentId));
}
parentComponentRefNode.DependencyIds.Add(componentId);
this.componentNodes[componentId].DependedOnByIds.Add(parentComponentId);
}
private void GetAncestorsRecursive(ComponentRefNode componentRef, IDictionary<string, int> ancestors, int depth)
{
foreach (var parentId in componentRef.DependedOnByIds)
{
if (ancestors.ContainsKey(parentId))
{
continue;
}
ancestors.Add(parentId, depth);
this.GetAncestorsRecursive(this.componentNodes[parentId], ancestors, depth + 1);
}
}
internal class ComponentRefNode
{
internal ComponentRefNode()
{
this.DependencyIds = new HashSet<string>();
this.DependedOnByIds = new HashSet<string>();
}
internal bool IsExplicitReferencedDependency { get; set; }
internal string Id { get; set; }
internal ISet<string> DependencyIds { get; private set; }
internal ISet<string> DependedOnByIds { get; private set; }
internal bool? IsDevelopmentDependency { get; set; }
internal DependencyScope? DependencyScope { get; set; }
internal TypedComponent TypedComponent { get; set; }
}
}