3
3
using System . IO ;
4
4
using System . Linq ;
5
5
using Newtonsoft . Json . Linq ;
6
+ using Semmle . Util ;
6
7
7
8
namespace Semmle . Extraction . CSharp . DependencyFetching
8
9
{
@@ -29,21 +30,15 @@ internal Assets(ProgressMonitor progressMonitor)
29
30
/// <summary>
30
31
/// Class needed for deserializing parts of an assets file.
31
32
/// It holds information about a reference.
33
+ ///
34
+ /// Type carries the type of the reference.
35
+ /// We are only interested in package references.
36
+ ///
37
+ /// Compile holds information about the files needed for compilation.
38
+ /// However, if it is a .NET framework reference we assume that all files in the
39
+ /// package are needed for compilation.
32
40
/// </summary>
33
- private class ReferenceInfo
34
- {
35
- /// <summary>
36
- /// This carries the type of the reference.
37
- /// We are only interested in package references.
38
- /// </summary>
39
- public string Type { get ; set ; } = "" ;
40
-
41
- /// <summary>
42
- /// If not a .NET framework reference we assume that only the files mentioned
43
- /// in the compile section are needed for compilation.
44
- /// </summary>
45
- public Dictionary < string , object > Compile { get ; set ; } = new ( ) ;
46
- }
41
+ private record class ReferenceInfo ( string ? Type , Dictionary < string , object > ? Compile ) ;
47
42
48
43
/// <summary>
49
44
/// Add the package dependencies from the assets file to dependencies.
@@ -74,7 +69,7 @@ private class ReferenceInfo
74
69
/// }
75
70
///
76
71
/// Returns dependencies
77
- /// Required = {
72
+ /// RequiredPaths = {
78
73
/// "castle.core/4.4.1/lib/netstandard1.5/Castle.Core.dll",
79
74
/// "json.net/1.0.33/lib/netstandard2.0/Json.Net.dll"
80
75
/// }
@@ -83,7 +78,7 @@ private class ReferenceInfo
83
78
/// "json.net"
84
79
/// }
85
80
/// </summary>
86
- private Dependencies AddPackageDependencies ( JObject json , Dependencies dependencies )
81
+ private DependencyContainer AddPackageDependencies ( JObject json , DependencyContainer dependencies )
87
82
{
88
83
// If there are more than one framework we need to pick just one.
89
84
// To ensure stability we pick one based on the lexicographic order of
@@ -103,31 +98,37 @@ private Dependencies AddPackageDependencies(JObject json, Dependencies dependenc
103
98
104
99
// Find all the compile dependencies for each reference and
105
100
// create the relative path to the dependency.
106
- return references
107
- . Aggregate ( dependencies , ( deps , r ) =>
101
+ references
102
+ . ForEach ( r =>
108
103
{
109
104
var info = r . Value ;
110
105
var name = r . Key . ToLowerInvariant ( ) ;
111
106
if ( info . Type != "package" )
112
107
{
113
- return deps ;
108
+ return ;
114
109
}
115
110
116
111
// If this is a .NET framework reference then include everything.
117
- return netFrameworks . Any ( framework => name . StartsWith ( framework ) )
118
- ? deps . Add ( name , "" )
119
- : info
120
- . Compile
121
- . Aggregate ( deps , ( d , p ) => d . Add ( name , p . Key ) ) ;
112
+ if ( netFrameworks . Any ( framework => name . StartsWith ( framework ) ) )
113
+ {
114
+ dependencies . Add ( name ) ;
115
+ }
116
+ else
117
+ {
118
+ info . Compile ?
119
+ . ForEach ( r => dependencies . Add ( name , r . Key ) ) ;
120
+ }
122
121
} ) ;
122
+
123
+ return dependencies ;
123
124
}
124
125
125
126
/// <summary>
126
127
/// Parse `json` as project.assets.json content and add relative paths to the dependencies
127
128
/// (together with used package information) required for compilation.
128
129
/// </summary>
129
130
/// <returns>True if parsing succeeds, otherwise false.</returns>
130
- public bool TryParse ( string json , Dependencies dependencies )
131
+ public bool TryParse ( string json , DependencyContainer dependencies )
131
132
{
132
133
try
133
134
{
@@ -142,15 +143,16 @@ public bool TryParse(string json, Dependencies dependencies)
142
143
}
143
144
}
144
145
145
- public static Dependencies GetCompilationDependencies ( ProgressMonitor progressMonitor , IEnumerable < string > assets )
146
+ public static DependencyContainer GetCompilationDependencies ( ProgressMonitor progressMonitor , IEnumerable < string > assets )
146
147
{
147
148
var parser = new Assets ( progressMonitor ) ;
148
- return assets . Aggregate ( new Dependencies ( ) , ( dependencies , asset ) =>
149
+ var dependencies = new DependencyContainer ( ) ;
150
+ assets . ForEach ( asset =>
149
151
{
150
152
var json = File . ReadAllText ( asset ) ;
151
153
parser . TryParse ( json , dependencies ) ;
152
- return dependencies ;
153
154
} ) ;
155
+ return dependencies ;
154
156
}
155
157
}
156
158
0 commit comments