@@ -21,27 +21,11 @@ internal class Assets
2121 "netstandard.library.ref"
2222 } ;
2323
24-
2524 internal Assets ( ProgressMonitor progressMonitor )
2625 {
2726 this . progressMonitor = progressMonitor ;
2827 }
2928
30- /// <summary>
31- /// In most cases paths in asset files point to dll's or the empty _._ file, which
32- /// is sometimes there to avoid the directory being empty.
33- /// That is, if the path specifically adds a .dll we use that, otherwise we as a fallback
34- /// add the entire directory (which should be fine in case of _._ as well).
35- /// </summary>
36- private static string ParseFilePath ( string path )
37- {
38- if ( path . EndsWith ( ".dll" ) )
39- {
40- return path ;
41- }
42- return Path . GetDirectoryName ( path ) ?? path ;
43- }
44-
4529 /// <summary>
4630 /// Class needed for deserializing parts of an assets file.
4731 /// It holds information about a reference.
@@ -62,10 +46,11 @@ private class ReferenceInfo
6246 }
6347
6448 /// <summary>
65- /// Gets the package dependencies from the assets file.
49+ /// Add the package dependencies from the assets file to dependencies .
6650 ///
67- /// Parse a part of the JSon assets file and returns the paths
68- /// to the dependencies required for compilation.
51+ /// Parse a part of the JSon assets file and add the paths
52+ /// to the dependencies required for compilation (and collect
53+ /// information about used packages).
6954 ///
7055 /// Example:
7156 /// {
@@ -88,12 +73,17 @@ private class ReferenceInfo
8873 /// }
8974 /// }
9075 ///
91- /// Returns {
92- /// "castle.core/4.4.1/lib/netstandard1.5/Castle.Core.dll",
93- /// "json.net/1.0.33/lib/netstandard2.0/Json.Net.dll"
94- /// }
76+ /// Returns dependencies
77+ /// Required = {
78+ /// "castle.core/4.4.1/lib/netstandard1.5/Castle.Core.dll",
79+ /// "json.net/1.0.33/lib/netstandard2.0/Json.Net.dll"
80+ /// }
81+ /// UsedPackages = {
82+ /// "castle.core",
83+ /// "json.net"
84+ /// }
9585 /// </summary>
96- private IEnumerable < string > GetPackageDependencies ( JObject json )
86+ private Dependencies AddPackageDependencies ( JObject json , Dependencies dependencies )
9787 {
9888 // If there are more than one framework we need to pick just one.
9989 // To ensure stability we pick one based on the lexicographic order of
@@ -108,49 +98,43 @@ private IEnumerable<string> GetPackageDependencies(JObject json)
10898 if ( references is null )
10999 {
110100 progressMonitor . LogDebug ( "No references found in the targets section in the assets file." ) ;
111- return Array . Empty < string > ( ) ;
101+ return dependencies ;
112102 }
113103
114104 // Find all the compile dependencies for each reference and
115105 // create the relative path to the dependency.
116- var dependencies = references
117- . SelectMany ( r =>
106+ references
107+ . Aggregate ( dependencies , ( deps , r ) =>
118108 {
119109 var info = r . Value ;
120110 var name = r . Key . ToLowerInvariant ( ) ;
121111 if ( info . Type != "package" )
122112 {
123- return Array . Empty < string > ( ) ;
113+ return deps ;
124114 }
125115
126116 // If this is a .NET framework reference then include everything.
127117 return netFrameworks . Any ( framework => name . StartsWith ( framework ) )
128- ? new [ ] { name }
118+ ? deps . Add ( name , "" )
129119 : info
130120 . Compile
131- . Select ( p => Path . Combine ( name , ParseFilePath ( p . Key ) ) ) ;
132- } )
133- . ToList ( ) ;
121+ . Aggregate ( deps , ( d , p ) => d . Add ( name , p . Key ) ) ;
122+ } ) ;
134123
135124 return dependencies ;
136125 }
137126
138127 /// <summary>
139- /// Parse `json` as project.assets.json content and populate `dependencies` with the
140- /// relative paths to the dependencies required for compilation.
128+ /// Parse `json` as project.assets.json content and add relative paths to the dependencies
129+ /// (together with used package information) required for compilation.
141130 /// </summary>
142131 /// <returns>True if parsing succeeds, otherwise false.</returns>
143- public bool TryParse ( string json , out IEnumerable < string > dependencies )
132+ public bool TryParse ( string json , Dependencies dependencies )
144133 {
145- dependencies = Array . Empty < string > ( ) ;
146-
147134 try
148135 {
149136 var obj = JObject . Parse ( json ) ;
150- var packages = GetPackageDependencies ( obj ) ;
151-
152- dependencies = packages . ToList ( ) ;
153-
137+ AddPackageDependencies ( obj , dependencies ) ;
154138 return true ;
155139 }
156140 catch ( Exception e )
@@ -160,13 +144,14 @@ public bool TryParse(string json, out IEnumerable<string> dependencies)
160144 }
161145 }
162146
163- public static IEnumerable < string > GetCompilationDependencies ( ProgressMonitor progressMonitor , IEnumerable < string > assets )
147+ public static Dependencies GetCompilationDependencies ( ProgressMonitor progressMonitor , IEnumerable < string > assets )
164148 {
165149 var parser = new Assets ( progressMonitor ) ;
166- return assets . SelectMany ( asset =>
150+ return assets . Aggregate ( new Dependencies ( ) , ( dependencies , asset ) =>
167151 {
168152 var json = File . ReadAllText ( asset ) ;
169- return parser . TryParse ( json , out var dependencies ) ? dependencies : Array . Empty < string > ( ) ;
153+ parser . TryParse ( json , dependencies ) ;
154+ return dependencies ;
170155 } ) ;
171156 }
172157 }
0 commit comments