@@ -46,7 +46,7 @@ public BuildScript Analyse(IAutobuilder<CSharpAutobuildOptions> builder, bool au
46
46
builder . Log ( Severity . Info , "Attempting to build using .NET Core" ) ;
47
47
}
48
48
49
- return WithDotNet ( builder , ( dotNetPath , environment ) =>
49
+ return WithDotNet ( builder , ensureDotNetAvailable : false , ( dotNetPath , environment ) =>
50
50
{
51
51
var ret = GetInfoCommand ( builder . Actions , dotNetPath , environment ) ;
52
52
foreach ( var projectOrSolution in builder . ProjectsOrSolutionsToBuild )
@@ -79,29 +79,29 @@ public BuildScript Analyse(IAutobuilder<CSharpAutobuildOptions> builder, bool au
79
79
/// variables needed by the installed .NET Core (<code>null</code> when no variables
80
80
/// are needed).
81
81
/// </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 )
83
83
{
84
84
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 ) ;
86
86
return BuildScript . Bind ( installScript , installed =>
87
87
{
88
- Dictionary < string , string > ? env ;
88
+ var env = new Dictionary < string , string >
89
+ {
90
+ { "DOTNET_SKIP_FIRST_TIME_EXPERIENCE" , "true" } ,
91
+ { "MSBUILDDISABLENODEREUSE" , "1" }
92
+ } ;
89
93
if ( installed == 0 )
90
94
{
91
95
// The installation succeeded, so use the newly installed .NET Core
92
96
var path = builder . Actions . GetEnvironmentVariable ( "PATH" ) ;
93
97
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 ) ;
100
100
}
101
101
else
102
102
{
103
+ // The .NET SDK was not installed, either because the installation failed or because it was already installed.
103
104
installDir = null ;
104
- env = null ;
105
105
}
106
106
107
107
return f ( installDir , env ) ;
@@ -117,14 +117,14 @@ public static BuildScript WithDotNet(IAutobuilder<AutobuildOptionsShared> builde
117
117
/// are needed).
118
118
/// </summary>
119
119
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 ) ) ;
121
121
122
122
/// <summary>
123
123
/// Returns a script for downloading relevant versions of the
124
124
/// .NET Core SDK. The SDK(s) will be installed at <code>installDir</code>
125
125
/// (provided that the script succeeds).
126
126
/// </summary>
127
- private static BuildScript DownloadDotNet ( IAutobuilder < AutobuildOptionsShared > builder , string installDir )
127
+ private static BuildScript DownloadDotNet ( IAutobuilder < AutobuildOptionsShared > builder , string installDir , bool ensureDotNetAvailable )
128
128
{
129
129
if ( ! string . IsNullOrEmpty ( builder . Options . DotNetVersion ) )
130
130
// Specific version supplied in configuration: always use that
@@ -152,7 +152,17 @@ private static BuildScript DownloadDotNet(IAutobuilder<AutobuildOptionsShared> b
152
152
validGlobalJson = true ;
153
153
}
154
154
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 ;
156
166
}
157
167
158
168
/// <summary>
@@ -161,14 +171,25 @@ private static BuildScript DownloadDotNet(IAutobuilder<AutobuildOptionsShared> b
161
171
///
162
172
/// See https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-install-script.
163
173
/// </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 )
165
175
{
166
176
return BuildScript . Bind ( GetInstalledSdksScript ( builder . Actions ) , ( sdks , sdksRet ) =>
167
177
{
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
+ {
169
180
// The requested SDK is already installed (and no other SDKs are installed), so
170
181
// no need to reinstall
171
182
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
+ }
172
193
173
194
builder . Log ( Severity . Info , "Attempting to download .NET Core {0}" , version ) ;
174
195
0 commit comments