1818
1919using System ;
2020using System . Collections . Generic ;
21+ using System . Diagnostics ;
2122using System . IO ;
2223using System . Linq ;
2324using System . Threading ;
2425using System . Threading . Tasks ;
2526
26- using ICSharpCode . Decompiler . Util ;
27+ using ICSharpCode . Decompiler . Metadata ;
2728
2829using NuGet . Common ;
2930using NuGet . Packaging ;
3031using NuGet . Protocol ;
3132using NuGet . Protocol . Core . Types ;
3233using NuGet . Versioning ;
3334
35+ using NUnit . Framework ;
36+
3437namespace ICSharpCode . Decompiler . Tests . Helpers
3538{
3639 abstract class AbstractToolset
@@ -50,29 +53,45 @@ public AbstractToolset(string baseDir)
5053
5154 protected async Task FetchPackage ( string packageName , string version , string sourcePath , string outputPath )
5255 {
56+ if ( ! Directory . Exists ( Path . Combine ( Roundtrip . RoundtripAssembly . TestDir , "nuget" ) ) )
57+ Assert . Fail ( "No nuget cache found!" ) ;
58+
5359 ILogger logger = NullLogger . Instance ;
5460 CancellationToken cancellationToken = CancellationToken . None ;
55- using MemoryStream packageStream = new MemoryStream ( ) ;
56-
57- await resource . CopyNupkgToStreamAsync (
58- packageName ,
59- NuGetVersion . Parse ( version ) ,
60- packageStream ,
61- cache ,
62- logger ,
63- cancellationToken ) . ConfigureAwait ( false ) ;
64-
65- using PackageArchiveReader packageReader = new PackageArchiveReader ( packageStream ) ;
66- NuspecReader nuspecReader = await packageReader . GetNuspecReaderAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
67-
68- var files = ( await packageReader . GetFilesAsync ( cancellationToken ) . ConfigureAwait ( false ) ) . ToArray ( ) ;
69- files = files . Where ( f => f . StartsWith ( sourcePath , StringComparison . OrdinalIgnoreCase ) ) . ToArray ( ) ;
70- await packageReader . CopyFilesAsync ( outputPath , files ,
71- ( sourceFile , targetPath , fileStream ) => {
72- fileStream . CopyToFile ( targetPath ) ;
73- return targetPath ;
74- } ,
75- logger , cancellationToken ) . ConfigureAwait ( false ) ;
61+ string pathToPackage = Path . Combine ( Roundtrip . RoundtripAssembly . TestDir , "nuget" , $ "{ packageName } -{ version } .nupkg") ;
62+ Stream packageStream ;
63+ if ( File . Exists ( pathToPackage ) )
64+ {
65+ packageStream = File . OpenRead ( pathToPackage ) ;
66+ }
67+ else
68+ {
69+ packageStream = new MemoryStream ( ) ;
70+
71+ await resource . CopyNupkgToStreamAsync (
72+ packageName ,
73+ NuGetVersion . Parse ( version ) ,
74+ packageStream ,
75+ cache ,
76+ logger ,
77+ cancellationToken ) . ConfigureAwait ( false ) ;
78+
79+ packageStream . Position = 0 ;
80+ }
81+ using ( packageStream )
82+ {
83+ using PackageArchiveReader packageReader = new PackageArchiveReader ( packageStream ) ;
84+ NuspecReader nuspecReader = await packageReader . GetNuspecReaderAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
85+
86+ var files = ( await packageReader . GetFilesAsync ( cancellationToken ) . ConfigureAwait ( false ) ) . ToArray ( ) ;
87+ files = files . Where ( f => f . StartsWith ( sourcePath , StringComparison . OrdinalIgnoreCase ) ) . ToArray ( ) ;
88+ await packageReader . CopyFilesAsync ( outputPath , files ,
89+ ( sourceFile , targetPath , fileStream ) => {
90+ fileStream . CopyToFile ( targetPath ) ;
91+ return targetPath ;
92+ } ,
93+ logger , cancellationToken ) . ConfigureAwait ( false ) ;
94+ }
7695 }
7796 }
7897
@@ -145,4 +164,44 @@ public async Task Fetch()
145164
146165 public string GetVsWhere ( ) => vswherePath ;
147166 }
167+
168+ class RefAssembliesToolset : AbstractToolset
169+ {
170+ readonly Dictionary < string , string > installedFrameworks = new Dictionary < string , string > {
171+ { "legacy" , Path . Combine ( Roundtrip . RoundtripAssembly . TestDir , "dotnet" , "legacy" ) } ,
172+ { "2.2.0" , Path . Combine ( Roundtrip . RoundtripAssembly . TestDir , "dotnet" , "netcore-2.2" ) } ,
173+ } ;
174+
175+ public RefAssembliesToolset ( )
176+ : base ( Path . Combine ( AppContext . BaseDirectory , "netfx" ) )
177+ {
178+ }
179+
180+ public async Task Fetch ( string version , string packageName = "Microsoft.NETCore.App.Ref" , string sourcePath = "ref/net5.0" )
181+ {
182+ string path = Path . Combine ( baseDir , version , sourcePath ) ;
183+ if ( ! Directory . Exists ( path ) )
184+ {
185+ await FetchPackage ( packageName , version , sourcePath , Path . Combine ( baseDir , version ) ) . ConfigureAwait ( false ) ;
186+ }
187+
188+ installedFrameworks . Add ( RoslynToolset . SanitizeVersion ( version ) , path ) ;
189+ }
190+
191+ internal string GetPath ( string targetFramework )
192+ {
193+ var ( id , version ) = UniversalAssemblyResolver . ParseTargetFramework ( targetFramework ) ;
194+ string path ;
195+ if ( id == TargetFrameworkIdentifier . NETFramework )
196+ {
197+ path = installedFrameworks [ "legacy" ] ;
198+ }
199+ else
200+ {
201+ path = installedFrameworks [ version . ToString ( 3 ) ] ;
202+ }
203+ Debug . Assert ( Path . Exists ( path ) ) ;
204+ return path ;
205+ }
206+ }
148207}
0 commit comments