33using System . CommandLine ;
44using System . CommandLine . Invocation ;
55using System . IO ;
6- using System . Linq ;
7- using System . Reflection ;
8- using System . Runtime . CompilerServices ;
9- using System . Runtime . Loader ;
106using System . Text . Json ;
117using System . Threading . Tasks ;
128using Microsoft . Build . Locator ;
@@ -20,12 +16,7 @@ public static class Program
2016 {
2117 public static async Task Main ( string [ ] args )
2218 {
23- if ( ! Program . FindMsBuild ( ) )
24- {
25- return ;
26- }
27-
28- Program . SetMsBuildDepsResolver ( ) ;
19+ MSBuildLocator . RegisterDefaults ( ) ;
2920
3021 RootCommand rootCommand = Program . GenerateCommands ( ) ;
3122 rootCommand . Handler = CommandHandler . Create ( async ( bool generatePointsFile , bool runTests , DirectoryInfo ? projectDir , FileInfo ? outputFile ) =>
@@ -129,47 +120,5 @@ private static RootCommand GenerateCommands()
129120 } , description : "The output file used to write results." )
130121 } ;
131122 }
132-
133- private static bool FindMsBuild ( )
134- {
135- string ? msbuildPath = Environment . GetEnvironmentVariable ( "MSBUILD_EXE_PATH" ) ;
136- if ( msbuildPath is not null )
137- {
138- return true ;
139- }
140-
141- VisualStudioInstance ? vsInstance = MSBuildLocator . QueryVisualStudioInstances ( VisualStudioInstanceQueryOptions . Default )
142- . FirstOrDefault ( i => i . Version . Major == Environment . Version . Major && i . Version . Minor == Environment . Version . Minor ) ;
143- if ( vsInstance is not null )
144- {
145- MSBuildLocator . RegisterInstance ( vsInstance ) ;
146-
147- return true ;
148- }
149-
150- Console . WriteLine ( $ "No environment variable MSBUILD_EXE_PATH has been set and we were unable to locate it automatically! You need to install SDK for { Environment . Version . ToString ( 2 ) } ") ;
151-
152- return false ;
153- }
154-
155- private static void SetMsBuildDepsResolver ( )
156- {
157- string msbuildDir = Path . GetDirectoryName ( Environment . GetEnvironmentVariable ( "MSBUILD_EXE_PATH" ) ) ! ;
158-
159- AppDomain . CurrentDomain . AssemblyResolve += ( sender , args ) =>
160- {
161- AssemblyName name = new AssemblyName ( args . Name ) ;
162-
163- string assemblyName = $ "{ name . Name } .dll";
164- string sdkFileName = Path . Combine ( msbuildDir , assemblyName ) ;
165-
166- if ( File . Exists ( sdkFileName ) )
167- {
168- return Assembly . LoadFile ( sdkFileName ) ;
169- }
170-
171- return null ;
172- } ;
173- }
174123 }
175124}
0 commit comments