@@ -46,7 +46,7 @@ public BuildScript Analyse(IAutobuilder<CSharpAutobuildOptions> builder, bool au
4646 builder . Log ( Severity . Info , "Attempting to build using .NET Core" ) ;
4747 }
4848
49- return WithDotNet ( builder , ( dotNetPath , environment ) =>
49+ return WithDotNet ( builder , ensureDotNetAvailable : false , ( dotNetPath , environment ) =>
5050 {
5151 var ret = GetInfoCommand ( builder . Actions , dotNetPath , environment ) ;
5252 foreach ( var projectOrSolution in builder . ProjectsOrSolutionsToBuild )
@@ -79,29 +79,29 @@ public BuildScript Analyse(IAutobuilder<CSharpAutobuildOptions> builder, bool au
7979 /// variables needed by the installed .NET Core (<code>null</code> when no variables
8080 /// are needed).
8181 /// </summary>
82- public static BuildScript WithDotNet ( IAutobuilder < AutobuildOptionsShared > builder , Func < string ? , IDictionary < string , string > ? , BuildScript > f )
82+ public static BuildScript WithDotNet ( IAutobuilder < AutobuildOptionsShared > builder , bool ensureDotNetAvailable , Func < string ? , IDictionary < string , string > ? , BuildScript > f )
8383 {
8484 var installDir = builder . Actions . PathCombine ( FileUtils . GetTemporaryWorkingDirectory ( builder . Actions . GetEnvironmentVariable , builder . Options . Language . UpperCaseName , out var _ ) , ".dotnet" ) ;
85- var installScript = DownloadDotNet ( builder , installDir ) ;
85+ var installScript = DownloadDotNet ( builder , installDir , ensureDotNetAvailable ) ;
8686 return BuildScript . Bind ( installScript , installed =>
8787 {
88- Dictionary < string , string > ? env ;
88+ var env = new Dictionary < string , string >
89+ {
90+ { "DOTNET_SKIP_FIRST_TIME_EXPERIENCE" , "true" } ,
91+ { "MSBUILDDISABLENODEREUSE" , "1" }
92+ } ;
8993 if ( installed == 0 )
9094 {
9195 // The installation succeeded, so use the newly installed .NET Core
9296 var path = builder . Actions . GetEnvironmentVariable ( "PATH" ) ;
9397 var delim = builder . Actions . IsWindows ( ) ? ";" : ":" ;
94- env = new Dictionary < string , string > {
95- { "DOTNET_MULTILEVEL_LOOKUP" , "false" } , // prevent look up of other .NET Core SDKs
96- { "DOTNET_SKIP_FIRST_TIME_EXPERIENCE" , "true" } ,
97- { "MSBUILDDISABLENODEREUSE" , "1" } ,
98- { "PATH" , installDir + delim + path }
99- } ;
98+ env . Add ( "DOTNET_MULTILEVEL_LOOKUP" , "false" ) ; // prevent look up of other .NET Core SDKs
99+ env . Add ( "PATH" , installDir + delim + path ) ;
100100 }
101101 else
102102 {
103+ // The .NET SDK was not installed, either because the installation failed or because it was already installed.
103104 installDir = null ;
104- env = null ;
105105 }
106106
107107 return f ( installDir , env ) ;
@@ -117,14 +117,14 @@ public static BuildScript WithDotNet(IAutobuilder<AutobuildOptionsShared> builde
117117 /// are needed).
118118 /// </summary>
119119 public static BuildScript WithDotNet ( IAutobuilder < AutobuildOptionsShared > builder , Func < IDictionary < string , string > ? , BuildScript > f )
120- => WithDotNet ( builder , ( _1 , env ) => f ( env ) ) ;
120+ => WithDotNet ( builder , ensureDotNetAvailable : false , ( _ , env ) => f ( env ) ) ;
121121
122122 /// <summary>
123123 /// Returns a script for downloading relevant versions of the
124124 /// .NET Core SDK. The SDK(s) will be installed at <code>installDir</code>
125125 /// (provided that the script succeeds).
126126 /// </summary>
127- private static BuildScript DownloadDotNet ( IAutobuilder < AutobuildOptionsShared > builder , string installDir )
127+ private static BuildScript DownloadDotNet ( IAutobuilder < AutobuildOptionsShared > builder , string installDir , bool ensureDotNetAvailable )
128128 {
129129 if ( ! string . IsNullOrEmpty ( builder . Options . DotNetVersion ) )
130130 // Specific version supplied in configuration: always use that
@@ -152,7 +152,17 @@ private static BuildScript DownloadDotNet(IAutobuilder<AutobuildOptionsShared> b
152152 validGlobalJson = true ;
153153 }
154154
155- return validGlobalJson ? installScript : BuildScript . Failure ;
155+ if ( validGlobalJson )
156+ {
157+ return installScript ;
158+ }
159+
160+ if ( ensureDotNetAvailable )
161+ {
162+ return DownloadDotNetVersion ( builder , installDir , Constants . LatestDotNetSdkVersion , needExactVersion : false ) ;
163+ }
164+
165+ return BuildScript . Failure ;
156166 }
157167
158168 /// <summary>
@@ -161,14 +171,25 @@ private static BuildScript DownloadDotNet(IAutobuilder<AutobuildOptionsShared> b
161171 ///
162172 /// See https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-install-script.
163173 /// </summary>
164- private static BuildScript DownloadDotNetVersion ( IAutobuilder < AutobuildOptionsShared > builder , string path , string version )
174+ private static BuildScript DownloadDotNetVersion ( IAutobuilder < AutobuildOptionsShared > builder , string path , string version , bool needExactVersion = true )
165175 {
166176 return BuildScript . Bind ( GetInstalledSdksScript ( builder . Actions ) , ( sdks , sdksRet ) =>
167177 {
168- if ( sdksRet == 0 && sdks . Count == 1 && sdks [ 0 ] . StartsWith ( version + " " , StringComparison . Ordinal ) )
178+ if ( needExactVersion && sdksRet == 0 && sdks . Count == 1 && sdks [ 0 ] . StartsWith ( version + " " , StringComparison . Ordinal ) )
179+ {
169180 // The requested SDK is already installed (and no other SDKs are installed), so
170181 // no need to reinstall
171182 return BuildScript . Failure ;
183+ }
184+ else if ( ! needExactVersion && sdksRet == 0 && sdks . Count > 0 )
185+ {
186+ // there's at least one SDK installed, so no need to reinstall
187+ return BuildScript . Failure ;
188+ }
189+ else if ( ! needExactVersion && sdksRet != 0 )
190+ {
191+ builder . Log ( Severity . Info , "No .NET Core SDK found." ) ;
192+ }
172193
173194 builder . Log ( Severity . Info , "Attempting to download .NET Core {0}" , version ) ;
174195
0 commit comments