33using System . IO ;
44using System . Linq ;
55using Newtonsoft . Json . Linq ;
6+ using Semmle . Util ;
67
78namespace Semmle . Extraction . CSharp . DependencyFetching
89{
@@ -29,21 +30,15 @@ internal Assets(ProgressMonitor progressMonitor)
2930 /// <summary>
3031 /// Class needed for deserializing parts of an assets file.
3132 /// 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.
3240 /// </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 ) ;
4742
4843 /// <summary>
4944 /// Add the package dependencies from the assets file to dependencies.
@@ -74,7 +69,7 @@ private class ReferenceInfo
7469 /// }
7570 ///
7671 /// Returns dependencies
77- /// Required = {
72+ /// RequiredPaths = {
7873 /// "castle.core/4.4.1/lib/netstandard1.5/Castle.Core.dll",
7974 /// "json.net/1.0.33/lib/netstandard2.0/Json.Net.dll"
8075 /// }
@@ -83,7 +78,7 @@ private class ReferenceInfo
8378 /// "json.net"
8479 /// }
8580 /// </summary>
86- private Dependencies AddPackageDependencies ( JObject json , Dependencies dependencies )
81+ private DependencyContainer AddPackageDependencies ( JObject json , DependencyContainer dependencies )
8782 {
8883 // If there are more than one framework we need to pick just one.
8984 // To ensure stability we pick one based on the lexicographic order of
@@ -103,31 +98,37 @@ private Dependencies AddPackageDependencies(JObject json, Dependencies dependenc
10398
10499 // Find all the compile dependencies for each reference and
105100 // create the relative path to the dependency.
106- return references
107- . Aggregate ( dependencies , ( deps , r ) =>
101+ references
102+ . ForEach ( r =>
108103 {
109104 var info = r . Value ;
110105 var name = r . Key . ToLowerInvariant ( ) ;
111106 if ( info . Type != "package" )
112107 {
113- return deps ;
108+ return ;
114109 }
115110
116111 // 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+ }
122121 } ) ;
122+
123+ return dependencies ;
123124 }
124125
125126 /// <summary>
126127 /// Parse `json` as project.assets.json content and add relative paths to the dependencies
127128 /// (together with used package information) required for compilation.
128129 /// </summary>
129130 /// <returns>True if parsing succeeds, otherwise false.</returns>
130- public bool TryParse ( string json , Dependencies dependencies )
131+ public bool TryParse ( string json , DependencyContainer dependencies )
131132 {
132133 try
133134 {
@@ -142,15 +143,16 @@ public bool TryParse(string json, Dependencies dependencies)
142143 }
143144 }
144145
145- public static Dependencies GetCompilationDependencies ( ProgressMonitor progressMonitor , IEnumerable < string > assets )
146+ public static DependencyContainer GetCompilationDependencies ( ProgressMonitor progressMonitor , IEnumerable < string > assets )
146147 {
147148 var parser = new Assets ( progressMonitor ) ;
148- return assets . Aggregate ( new Dependencies ( ) , ( dependencies , asset ) =>
149+ var dependencies = new DependencyContainer ( ) ;
150+ assets . ForEach ( asset =>
149151 {
150152 var json = File . ReadAllText ( asset ) ;
151153 parser . TryParse ( json , dependencies ) ;
152- return dependencies ;
153154 } ) ;
155+ return dependencies ;
154156 }
155157 }
156158
0 commit comments