@@ -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,10 +79,10 @@ 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
88
Dictionary < string , string > ? env ;
@@ -100,8 +100,12 @@ public static BuildScript WithDotNet(IAutobuilder<AutobuildOptionsShared> builde
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
+ env = new Dictionary < string , string > {
106
+ { "DOTNET_SKIP_FIRST_TIME_EXPERIENCE" , "true" } ,
107
+ { "MSBUILDDISABLENODEREUSE" , "1" }
108
+ } ;
105
109
}
106
110
107
111
return f ( installDir , env ) ;
@@ -117,14 +121,14 @@ public static BuildScript WithDotNet(IAutobuilder<AutobuildOptionsShared> builde
117
121
/// are needed).
118
122
/// </summary>
119
123
public static BuildScript WithDotNet ( IAutobuilder < AutobuildOptionsShared > builder , Func < IDictionary < string , string > ? , BuildScript > f )
120
- => WithDotNet ( builder , ( _1 , env ) => f ( env ) ) ;
124
+ => WithDotNet ( builder , ensureDotNetAvailable : false , ( _ , env ) => f ( env ) ) ;
121
125
122
126
/// <summary>
123
127
/// Returns a script for downloading relevant versions of the
124
128
/// .NET Core SDK. The SDK(s) will be installed at <code>installDir</code>
125
129
/// (provided that the script succeeds).
126
130
/// </summary>
127
- private static BuildScript DownloadDotNet ( IAutobuilder < AutobuildOptionsShared > builder , string installDir )
131
+ private static BuildScript DownloadDotNet ( IAutobuilder < AutobuildOptionsShared > builder , string installDir , bool ensureDotNetAvailable )
128
132
{
129
133
if ( ! string . IsNullOrEmpty ( builder . Options . DotNetVersion ) )
130
134
// Specific version supplied in configuration: always use that
@@ -152,7 +156,28 @@ private static BuildScript DownloadDotNet(IAutobuilder<AutobuildOptionsShared> b
152
156
validGlobalJson = true ;
153
157
}
154
158
155
- return validGlobalJson ? installScript : BuildScript . Failure ;
159
+ if ( validGlobalJson )
160
+ {
161
+ return installScript ;
162
+ }
163
+
164
+ if ( ensureDotNetAvailable )
165
+ {
166
+ return BuildScript . Bind ( GetInfoScript ( builder . Actions ) , ( infoLines , infoRet ) =>
167
+ {
168
+ if ( infoRet == 0 )
169
+ {
170
+ return BuildScript . Failure ;
171
+ }
172
+
173
+ const string latestDotNetSdkVersion = "8.0.101" ;
174
+ builder . Log ( Severity . Info , $ "No .NET Core SDK found. Attempting to install version { latestDotNetSdkVersion } .") ;
175
+ return DownloadDotNetVersion ( builder , installDir , latestDotNetSdkVersion ) ;
176
+ } ) ;
177
+
178
+ }
179
+
180
+ return BuildScript . Failure ;
156
181
}
157
182
158
183
/// <summary>
@@ -238,6 +263,14 @@ private static BuildScript GetInstalledSdksScript(IBuildActions actions)
238
263
return listSdks . Script ;
239
264
}
240
265
266
+ private static BuildScript GetInfoScript ( IBuildActions actions )
267
+ {
268
+ var info = new CommandBuilder ( actions , silent : true ) .
269
+ RunCommand ( "dotnet" ) .
270
+ Argument ( "--info" ) ;
271
+ return info . Script ;
272
+ }
273
+
241
274
private static string DotNetCommand ( IBuildActions actions , string ? dotNetPath ) =>
242
275
dotNetPath is not null ? actions . PathCombine ( dotNetPath , "dotnet" ) : "dotnet" ;
243
276
0 commit comments