@@ -96,40 +96,22 @@ public DependencyManager(string srcDir, IDependencyOptions options, ILogger logg
96
96
DownloadMissingPackages ( allNonBinaryFiles ) ;
97
97
}
98
98
99
+ var existsNetCoreRefNugetPackage = false ;
100
+ var existsNetFrameworkRefNugetPackage = false ;
101
+
99
102
// Find DLLs in the .Net / Asp.Net Framework
100
103
if ( options . ScanNetFrameworkDlls )
101
104
{
102
- // TODO: check if the nuget restore process has already downloaded any Core or Framework reference assemblies.
103
- // If so, we don't have to do the below.
104
- // `Microsoft.NETCore.App.Ref` or `Microsoft.NETFramework.ReferenceAssemblies.*`
105
-
106
- var runtime = new Runtime ( dotnet ) ;
107
- string ? runtimeLocation = null ;
105
+ existsNetCoreRefNugetPackage = IsNugetPackageAvailable ( "microsoft.netcore.app.ref" ) ;
106
+ existsNetFrameworkRefNugetPackage = IsNugetPackageAvailable ( "microsoft.netframework.referenceassemblies" ) ;
108
107
109
- if ( options . UseSelfContainedDotnet )
110
- {
111
- runtimeLocation = runtime . ExecutingRuntime ;
112
- }
113
- else if ( fileContent . IsNewProjectStructureUsed )
108
+ if ( existsNetCoreRefNugetPackage || existsNetFrameworkRefNugetPackage )
114
109
{
115
- runtimeLocation = runtime . NetCoreRuntime ;
110
+ progressMonitor . LogInfo ( "Found .NET Core/Framework DLLs in NuGet packages. Not adding installation directory." ) ;
116
111
}
117
- else if ( fileContent . IsLegacyProjectStructureUsed )
118
- {
119
- runtimeLocation = runtime . DesktopRuntime ;
120
- }
121
-
122
- runtimeLocation ??= runtime . ExecutingRuntime ;
123
-
124
- progressMonitor . LogInfo ( $ ".NET runtime location selected: { runtimeLocation } ") ;
125
- dllDirNames . Add ( runtimeLocation ) ;
126
-
127
- if ( fileContent . IsNewProjectStructureUsed
128
- && fileContent . UseAspNetCoreDlls
129
- && runtime . AspNetCoreRuntime is string aspRuntime )
112
+ else
130
113
{
131
- progressMonitor . LogInfo ( $ "ASP.NET runtime location selected: { aspRuntime } ") ;
132
- dllDirNames . Add ( aspRuntime ) ;
114
+ AddNetFrameworkDlls ( dllDirNames ) ;
133
115
}
134
116
}
135
117
@@ -141,7 +123,7 @@ public DependencyManager(string srcDir, IDependencyOptions options, ILogger logg
141
123
UseReference ( filename ) ;
142
124
}
143
125
144
- RemoveRuntimeNugetPackageReferences ( ) ;
126
+ RemoveUnnecessaryNugetPackages ( existsNetCoreRefNugetPackage , existsNetFrameworkRefNugetPackage ) ;
145
127
ResolveConflicts ( ) ;
146
128
147
129
// Output the findings
@@ -176,38 +158,110 @@ public DependencyManager(string srcDir, IDependencyOptions options, ILogger logg
176
158
DateTime . Now - startTime ) ;
177
159
}
178
160
179
- private void RemoveRuntimeNugetPackageReferences ( )
161
+ private void RemoveUnnecessaryNugetPackages ( bool existsNetCoreRefNugetPackage , bool existsNetFrameworkRefNugetPackage )
180
162
{
181
- if ( ! options . UseNuGet )
163
+ RemoveRuntimeNugetPackageReferences ( ) ;
164
+
165
+ if ( fileContent . IsNewProjectStructureUsed
166
+ && ! fileContent . UseAspNetCoreDlls )
182
167
{
183
- return ;
168
+ // This might have been restored by the CLI even though the project isn't an asp.net core one.
169
+ RemoveNugetPackageReference ( "microsoft.aspnetcore.app.ref" ) ;
184
170
}
185
171
186
- var packageFolder = packageDirectory . DirInfo . FullName . ToLowerInvariant ( ) ;
187
- var runtimePackageNamePrefixes = new [ ]
172
+ if ( existsNetCoreRefNugetPackage && existsNetFrameworkRefNugetPackage )
173
+ {
174
+ // Multiple packages are available, we keep only one:
175
+ RemoveNugetPackageReference ( "microsoft.netframework.referenceassemblies." ) ;
176
+ }
177
+
178
+ // TODO: There could be multiple `microsoft.netframework.referenceassemblies` packages,
179
+ // we could keep the newest one, but this is covered by the conflict resolution logic
180
+ // (if the file names match)
181
+ }
182
+
183
+ private void AddNetFrameworkDlls ( List < string > dllDirNames )
184
+ {
185
+ var runtime = new Runtime ( dotnet ) ;
186
+ string ? runtimeLocation = null ;
187
+
188
+ if ( options . UseSelfContainedDotnet )
189
+ {
190
+ runtimeLocation = runtime . ExecutingRuntime ;
191
+ }
192
+ else if ( fileContent . IsNewProjectStructureUsed )
193
+ {
194
+ runtimeLocation = runtime . NetCoreRuntime ;
195
+ }
196
+ else if ( fileContent . IsLegacyProjectStructureUsed )
188
197
{
189
- Path . Combine ( packageFolder , "microsoft.netcore.app.runtime" ) ,
190
- Path . Combine ( packageFolder , "microsoft.aspnetcore.app.runtime" ) ,
191
- Path . Combine ( packageFolder , "microsoft.windowsdesktop.app.runtime" ) ,
198
+ runtimeLocation = runtime . DesktopRuntime ;
199
+ }
200
+
201
+ runtimeLocation ??= runtime . ExecutingRuntime ;
202
+
203
+ progressMonitor . LogInfo ( $ ".NET runtime location selected: { runtimeLocation } ") ;
204
+ dllDirNames . Add ( runtimeLocation ) ;
205
+
206
+ if ( fileContent . IsNewProjectStructureUsed
207
+ && fileContent . UseAspNetCoreDlls
208
+ && runtime . AspNetCoreRuntime is string aspRuntime )
209
+ {
210
+ progressMonitor . LogInfo ( $ "ASP.NET runtime location selected: { aspRuntime } ") ;
211
+ dllDirNames . Add ( aspRuntime ) ;
212
+ }
213
+ }
214
+
215
+ private void RemoveRuntimeNugetPackageReferences ( )
216
+ {
217
+ var runtimePackagePrefixes = new [ ]
218
+ {
219
+ "microsoft.netcore.app.runtime" ,
220
+ "microsoft.aspnetcore.app.runtime" ,
221
+ "microsoft.windowsdesktop.app.runtime" ,
192
222
193
223
// legacy runtime packages:
194
- Path . Combine ( packageFolder , "runtime.linux-x64.microsoft.netcore.app" ) ,
195
- Path . Combine ( packageFolder , "runtime.osx-x64.microsoft.netcore.app" ) ,
196
- Path . Combine ( packageFolder , "runtime.win-x64.microsoft.netcore.app" ) ,
224
+ "runtime.linux-x64.microsoft.netcore.app" ,
225
+ "runtime.osx-x64.microsoft.netcore.app" ,
226
+ "runtime.win-x64.microsoft.netcore.app" ,
197
227
} ;
228
+ RemoveNugetPackageReference ( runtimePackagePrefixes ) ;
229
+ }
230
+
231
+ private void RemoveNugetPackageReference ( params string [ ] packagePrefixes )
232
+ {
233
+ if ( ! options . UseNuGet )
234
+ {
235
+ return ;
236
+ }
237
+
238
+ var packageFolder = packageDirectory . DirInfo . FullName . ToLowerInvariant ( ) ;
239
+ var packagePathPrefixes = packagePrefixes . Select ( p => Path . Combine ( packageFolder , p . ToLowerInvariant ( ) ) ) ;
198
240
199
241
foreach ( var filename in usedReferences . Keys )
200
242
{
201
243
var lowerFilename = filename . ToLowerInvariant ( ) ;
202
244
203
- if ( runtimePackageNamePrefixes . Any ( prefix => lowerFilename . StartsWith ( prefix ) ) )
245
+ if ( packagePathPrefixes . Any ( prefix => lowerFilename . StartsWith ( prefix ) ) )
204
246
{
205
247
usedReferences . Remove ( filename ) ;
206
248
progressMonitor . RemovedReference ( filename ) ;
207
249
}
208
250
}
209
251
}
210
252
253
+ private bool IsNugetPackageAvailable ( string packagePrefix )
254
+ {
255
+ if ( ! options . UseNuGet )
256
+ {
257
+ return false ;
258
+ }
259
+
260
+ return new DirectoryInfo ( packageDirectory . DirInfo . FullName )
261
+ . EnumerateDirectories ( packagePrefix + "*" , new EnumerationOptions { MatchCasing = MatchCasing . CaseInsensitive , RecurseSubdirectories = false } )
262
+ . Any ( ) ;
263
+ }
264
+
211
265
private void GenerateSourceFileFromImplicitUsings ( )
212
266
{
213
267
var usings = new HashSet < string > ( ) ;
0 commit comments