55using Semmle . Util ;
66using Semmle . Util . Logging ;
77using Semmle . Autobuild . Shared ;
8- using Newtonsoft . Json . Linq ;
8+ using Semmle . Extraction . CSharp . DependencyFetching ;
99
1010namespace Semmle . Autobuild . CSharp
1111{
@@ -39,11 +39,11 @@ public BuildScript Analyse(IAutobuilder<CSharpAutobuildOptions> builder, bool au
3939
4040 if ( notDotNetProject is not null )
4141 {
42- builder . Log ( Severity . Info , "Not using .NET Core because of incompatible project {0}" , notDotNetProject ) ;
42+ builder . Logger . Log ( Severity . Info , "Not using .NET Core because of incompatible project {0}" , notDotNetProject ) ;
4343 return BuildScript . Failure ;
4444 }
4545
46- builder . Log ( Severity . Info , "Attempting to build using .NET Core" ) ;
46+ builder . Logger . LogInfo ( "Attempting to build using .NET Core" ) ;
4747 }
4848
4949 return WithDotNet ( builder , ensureDotNetAvailable : false , ( dotNetPath , environment ) =>
@@ -81,29 +81,22 @@ public BuildScript Analyse(IAutobuilder<CSharpAutobuildOptions> builder, bool au
8181 /// </summary>
8282 public static BuildScript WithDotNet ( IAutobuilder < AutobuildOptionsShared > builder , bool ensureDotNetAvailable , Func < string ? , IDictionary < string , string > ? , BuildScript > f )
8383 {
84- var installDir = builder . Actions . PathCombine ( FileUtils . GetTemporaryWorkingDirectory ( builder . Actions . GetEnvironmentVariable , builder . Options . Language . UpperCaseName , out var _ ) , ".dotnet" ) ;
85- var installScript = DownloadDotNet ( builder , installDir , ensureDotNetAvailable ) ;
86- return BuildScript . Bind ( installScript , installed =>
84+ var temp = FileUtils . GetTemporaryWorkingDirectory ( builder . Actions . GetEnvironmentVariable , builder . Options . Language . UpperCaseName , out var shouldCleanUp ) ;
85+ return DotNet . WithDotNet ( builder . Actions , builder . Logger , builder . Paths . Select ( x => x . Item1 ) , temp , shouldCleanUp , ensureDotNetAvailable , builder . Options . DotNetVersion , installDir =>
8786 {
8887 var env = new Dictionary < string , string >
8988 {
9089 { "DOTNET_SKIP_FIRST_TIME_EXPERIENCE" , "true" } ,
9190 { "MSBUILDDISABLENODEREUSE" , "1" }
9291 } ;
93- if ( installed == 0 )
92+ if ( installDir is not null )
9493 {
95- // The installation succeeded, so use the newly installed .NET Core
94+ // The installation succeeded, so use the newly installed .NET
9695 var path = builder . Actions . GetEnvironmentVariable ( "PATH" ) ;
9796 var delim = builder . Actions . IsWindows ( ) ? ";" : ":" ;
98- env . Add ( "DOTNET_MULTILEVEL_LOOKUP" , "false" ) ; // prevent look up of other .NET Core SDKs
97+ env . Add ( "DOTNET_MULTILEVEL_LOOKUP" , "false" ) ; // prevent look up of other .NET SDKs
9998 env . Add ( "PATH" , installDir + delim + path ) ;
10099 }
101- else
102- {
103- // The .NET SDK was not installed, either because the installation failed or because it was already installed.
104- installDir = null ;
105- }
106-
107100 return f ( installDir , env ) ;
108101 } ) ;
109102 }
@@ -119,146 +112,6 @@ public static BuildScript WithDotNet(IAutobuilder<AutobuildOptionsShared> builde
119112 public static BuildScript WithDotNet ( IAutobuilder < AutobuildOptionsShared > builder , Func < IDictionary < string , string > ? , BuildScript > f )
120113 => WithDotNet ( builder , ensureDotNetAvailable : false , ( _ , env ) => f ( env ) ) ;
121114
122- /// <summary>
123- /// Returns a script for downloading relevant versions of the
124- /// .NET Core SDK. The SDK(s) will be installed at <code>installDir</code>
125- /// (provided that the script succeeds).
126- /// </summary>
127- private static BuildScript DownloadDotNet ( IAutobuilder < AutobuildOptionsShared > builder , string installDir , bool ensureDotNetAvailable )
128- {
129- if ( ! string . IsNullOrEmpty ( builder . Options . DotNetVersion ) )
130- // Specific version supplied in configuration: always use that
131- return DownloadDotNetVersion ( builder , installDir , builder . Options . DotNetVersion ) ;
132-
133- // Download versions mentioned in `global.json` files
134- // See https://docs.microsoft.com/en-us/dotnet/core/tools/global-json
135- var installScript = BuildScript . Success ;
136- var validGlobalJson = false ;
137- foreach ( var path in builder . Paths . Select ( p => p . Item1 ) . Where ( p => p . EndsWith ( "global.json" , StringComparison . Ordinal ) ) )
138- {
139- string version ;
140- try
141- {
142- var o = JObject . Parse ( File . ReadAllText ( path ) ) ;
143- version = ( string ) ( o ? [ "sdk" ] ? [ "version" ] ! ) ;
144- }
145- catch // lgtm[cs/catch-of-all-exceptions]
146- {
147- // not a valid global.json file
148- continue ;
149- }
150-
151- installScript &= DownloadDotNetVersion ( builder , installDir , version ) ;
152- validGlobalJson = true ;
153- }
154-
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 ;
166- }
167-
168- /// <summary>
169- /// Returns a script for downloading a specific .NET Core SDK version, if the
170- /// version is not already installed.
171- ///
172- /// See https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-install-script.
173- /// </summary>
174- private static BuildScript DownloadDotNetVersion ( IAutobuilder < AutobuildOptionsShared > builder , string path , string version , bool needExactVersion = true )
175- {
176- return BuildScript . Bind ( GetInstalledSdksScript ( builder . Actions ) , ( sdks , sdksRet ) =>
177- {
178- if ( needExactVersion && sdksRet == 0 && sdks . Count == 1 && sdks [ 0 ] . StartsWith ( version + " " , StringComparison . Ordinal ) )
179- {
180- // The requested SDK is already installed (and no other SDKs are installed), so
181- // no need to reinstall
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- }
193-
194- builder . Log ( Severity . Info , "Attempting to download .NET Core {0}" , version ) ;
195-
196- if ( builder . Actions . IsWindows ( ) )
197- {
198-
199- var psCommand = $ "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; &([scriptblock]::Create((Invoke-WebRequest -UseBasicParsing 'https://dot.net/v1/dotnet-install.ps1'))) -Version { version } -InstallDir { path } ";
200-
201- BuildScript GetInstall ( string pwsh ) =>
202- new CommandBuilder ( builder . Actions ) .
203- RunCommand ( pwsh ) .
204- Argument ( "-NoProfile" ) .
205- Argument ( "-ExecutionPolicy" ) .
206- Argument ( "unrestricted" ) .
207- Argument ( "-Command" ) .
208- Argument ( "\" " + psCommand + "\" " ) .
209- Script ;
210-
211- return GetInstall ( "pwsh" ) | GetInstall ( "powershell" ) ;
212- }
213- else
214- {
215- var dotnetInstallPath = builder . Actions . PathCombine ( FileUtils . GetTemporaryWorkingDirectory (
216- builder . Actions . GetEnvironmentVariable ,
217- builder . Options . Language . UpperCaseName ,
218- out var shouldCleanUp ) , ".dotnet" , "dotnet-install.sh" ) ;
219-
220- var downloadDotNetInstallSh = BuildScript . DownloadFile (
221- "https://dot.net/v1/dotnet-install.sh" ,
222- dotnetInstallPath ,
223- e => builder . Log ( Severity . Warning , $ "Failed to download 'dotnet-install.sh': { e . Message } ") ) ;
224-
225- var chmod = new CommandBuilder ( builder . Actions ) .
226- RunCommand ( "chmod" ) .
227- Argument ( "u+x" ) .
228- Argument ( dotnetInstallPath ) ;
229-
230- var install = new CommandBuilder ( builder . Actions ) .
231- RunCommand ( dotnetInstallPath ) .
232- Argument ( "--channel" ) .
233- Argument ( "release" ) .
234- Argument ( "--version" ) .
235- Argument ( version ) .
236- Argument ( "--install-dir" ) .
237- Argument ( path ) ;
238-
239- var buildScript = downloadDotNetInstallSh & chmod . Script & install . Script ;
240-
241- if ( shouldCleanUp )
242- {
243- var removeScript = new CommandBuilder ( builder . Actions ) .
244- RunCommand ( "rm" ) .
245- Argument ( dotnetInstallPath ) ;
246- buildScript &= removeScript . Script ;
247- }
248-
249- return buildScript ;
250- }
251- } ) ;
252- }
253-
254- private static BuildScript GetInstalledSdksScript ( IBuildActions actions )
255- {
256- var listSdks = new CommandBuilder ( actions , silent : true ) .
257- RunCommand ( "dotnet" ) .
258- Argument ( "--list-sdks" ) ;
259- return listSdks . Script ;
260- }
261-
262115 private static string DotNetCommand ( IBuildActions actions , string ? dotNetPath ) =>
263116 dotNetPath is not null ? actions . PathCombine ( dotNetPath , "dotnet" ) : "dotnet" ;
264117
0 commit comments