1- using Microsoft . Build . Locator ;
1+ // Copyright (c) SharpCrafters s.r.o. See the LICENSE.md file in the root directory of this repository root for details.
2+
3+ using Microsoft . Build . Locator ;
24using Microsoft . VisualStudio . Setup . Configuration ;
35using System ;
46using System . Collections . Generic ;
@@ -18,17 +20,17 @@ internal static class MSBuildHelper
1820
1921 public static void InitializeLocator ( )
2022 {
21- if ( ! _isInitialized )
23+ if ( ! _isInitialized )
2224 {
23- if ( MSBuildLocator . CanRegister )
25+ if ( MSBuildLocator . CanRegister )
2426 {
2527 try
2628 {
2729 MSBuildLocator . RegisterDefaults ( ) ;
2830
2931 _isInitialized = true ;
3032 }
31- catch ( Exception e )
33+ catch ( Exception e )
3234 {
3335 throw new InvalidOperationException (
3436 $ "Cannot find a suitable version of MSBuild for "
@@ -45,7 +47,7 @@ public static void InitializeLocator()
4547 {
4648 var directory = FindLatestMSBuildDirectory ( context ) ;
4749
48- if ( directory == null )
50+ if ( directory == null )
4951 {
5052 return directory ;
5153 }
@@ -57,14 +59,16 @@ public static void InitializeLocator()
5759
5860 private static string ? FindLatestMSBuildDirectory ( BuildContext context )
5961 {
62+ InitializeLocator ( ) ;
63+
6064 var instances = GetVisualStudioInstances ( context ) . OrderByDescending ( i => i . Version ) ;
6165
62- foreach ( var instance in instances )
66+ foreach ( var instance in instances )
6367 {
6468 // We got a Visual Studio instance but not all of them have an MSBuild instance. For instance, a Test Agent instance does not have.
6569 var directory = Path . Combine ( instance . Path , "MSBuild" , "Current" , "Bin" ) ;
6670
67- if ( Directory . Exists ( directory ) )
71+ if ( Directory . Exists ( directory ) )
6872 {
6973 return directory ;
7074 }
@@ -75,7 +79,9 @@ public static void InitializeLocator()
7579
7680 public static IEnumerable < VisualStudioInstance > GetVisualStudioInstances ( BuildContext context )
7781 {
78- if ( ! RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
82+ InitializeLocator ( ) ;
83+
84+ if ( ! RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
7985 {
8086 return [ ] ;
8187 }
@@ -95,9 +101,9 @@ public static IEnumerable<VisualStudioInstance> GetVisualStudioInstances( BuildC
95101 {
96102 enumInstances . Next ( 1 , instances , out fetched ) ;
97103
98- if ( fetched > 0 )
104+ if ( fetched > 0 )
99105 {
100- var instance = ( ISetupInstance2 ) instances [ 0 ] ;
106+ var instance = ( ISetupInstance2 ) instances [ 0 ] ;
101107
102108 list . Add (
103109 new VisualStudioInstance (
@@ -106,9 +112,9 @@ public static IEnumerable<VisualStudioInstance> GetVisualStudioInstances( BuildC
106112 instance . GetInstallationPath ( ) ) ) ;
107113 }
108114 }
109- while ( fetched > 0 ) ;
115+ while ( fetched > 0 ) ;
110116 }
111- catch ( COMException exception )
117+ catch ( COMException exception )
112118 {
113119 context . Console . WriteWarning ( $ "Cannot find VS instances: { exception . Message } " ) ;
114120
0 commit comments