Skip to content

Commit 9b0ef2f

Browse files
authored
Merge pull request github#5654 from hvitved/csharp/autobuilder/pwsh
C#: First try `pwsh` and then `powershell` when calling `dotnet-install.ps1`
2 parents 58d1982 + 15c103e commit 9b0ef2f

File tree

2 files changed

+32
-46
lines changed

2 files changed

+32
-46
lines changed

csharp/autobuilder/Semmle.Autobuild.CSharp.Tests/BuildScripts.cs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -976,13 +976,11 @@ Microsoft.NETCore.App 2.1.3 [/usr/local/share/dotnet/shared/Microsoft.NETCore.Ap
976976
TestAutobuilderScript(autobuilder, 0, 9);
977977
}
978978

979-
[Fact]
980-
public void TestDotnetVersionWindows()
979+
private void TestDotnetVersionWindows(Action action, int commandsRun)
981980
{
982981
actions.RunProcess["cmd.exe /C dotnet --list-sdks"] = 0;
983982
actions.RunProcessOut["cmd.exe /C dotnet --list-sdks"] = "2.1.3 [C:\\Program Files\\dotnet\\sdks]\n2.1.4 [C:\\Program Files\\dotnet\\sdks]";
984-
actions.RunProcess[@"cmd.exe /C powershell -NoProfile -ExecutionPolicy unrestricted -file C:\Project\install-dotnet.ps1 -Version 2.1.3 -InstallDir C:\Project\.dotnet"] = 0;
985-
actions.RunProcess[@"cmd.exe /C del C:\Project\install-dotnet.ps1"] = 0;
983+
action();
986984
actions.RunProcess[@"cmd.exe /C C:\Project\.dotnet\dotnet --info"] = 0;
987985
actions.RunProcess[@"cmd.exe /C C:\Project\.dotnet\dotnet clean C:\Project\test.csproj"] = 0;
988986
actions.RunProcess[@"cmd.exe /C C:\Project\.dotnet\dotnet restore C:\Project\test.csproj"] = 0;
@@ -1005,7 +1003,28 @@ public void TestDotnetVersionWindows()
10051003
actions.LoadXml[@"C:\Project\test.csproj"] = xml;
10061004

10071005
var autobuilder = CreateAutoBuilder(true, dotnetVersion: "2.1.3");
1008-
TestAutobuilderScript(autobuilder, 0, 7);
1006+
TestAutobuilderScript(autobuilder, 0, commandsRun);
1007+
}
1008+
1009+
[Fact]
1010+
public void TestDotnetVersionWindowsWithPwsh()
1011+
{
1012+
TestDotnetVersionWindows(() =>
1013+
{
1014+
actions.RunProcess[@"cmd.exe /C pwsh -NoProfile -ExecutionPolicy unrestricted -Command ""[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; &([scriptblock]::Create((Invoke-WebRequest -UseBasicParsing 'https://dot.net/v1/dotnet-install.ps1'))) -Version 2.1.3 -InstallDir C:\Project\.dotnet"""] = 0;
1015+
},
1016+
6);
1017+
}
1018+
1019+
[Fact]
1020+
public void TestDotnetVersionWindowsWithoutPwsh()
1021+
{
1022+
TestDotnetVersionWindows(() =>
1023+
{
1024+
actions.RunProcess[@"cmd.exe /C pwsh -NoProfile -ExecutionPolicy unrestricted -Command ""[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; &([scriptblock]::Create((Invoke-WebRequest -UseBasicParsing 'https://dot.net/v1/dotnet-install.ps1'))) -Version 2.1.3 -InstallDir C:\Project\.dotnet"""] = 1;
1025+
actions.RunProcess[@"cmd.exe /C powershell -NoProfile -ExecutionPolicy unrestricted -Command ""[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; &([scriptblock]::Create((Invoke-WebRequest -UseBasicParsing 'https://dot.net/v1/dotnet-install.ps1'))) -Version 2.1.3 -InstallDir C:\Project\.dotnet"""] = 0;
1026+
},
1027+
7);
10091028
}
10101029

10111030
[Fact]

csharp/autobuilder/Semmle.Autobuild.CSharp/DotNetRule.cs

Lines changed: 8 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -179,53 +179,20 @@ private static BuildScript DownloadDotNetVersion(Autobuilder builder, string pat
179179

180180
if (builder.Actions.IsWindows())
181181
{
182-
var psScript = @"param([string]$Version, [string]$InstallDir)
183182

184-
add-type @""
185-
using System.Net;
186-
using System.Security.Cryptography.X509Certificates;
187-
public class TrustAllCertsPolicy : ICertificatePolicy
188-
{
189-
public bool CheckValidationResult(ServicePoint srvPoint, X509Certificate certificate, WebRequest request, int certificateProblem)
190-
{
191-
return true;
192-
}
193-
}
194-
""@
195-
$AllProtocols = [System.Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12'
196-
[System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols
197-
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
198-
$Script = Invoke-WebRequest -useb 'https://dot.net/v1/dotnet-install.ps1'
199-
200-
$arguments = @{
201-
Channel = 'release'
202-
Version = $Version
203-
InstallDir = $InstallDir
204-
}
205-
206-
$ScriptBlock = [scriptblock]::create("".{$($Script)} $(&{$args} @arguments)"")
183+
var psCommand = $"[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; &([scriptblock]::Create((Invoke-WebRequest -UseBasicParsing 'https://dot.net/v1/dotnet-install.ps1'))) -Version {version} -InstallDir {path}";
207184

208-
Invoke-Command -ScriptBlock $ScriptBlock";
209-
var psScriptFile = builder.Actions.PathCombine(builder.Options.RootDirectory, "install-dotnet.ps1");
210-
builder.Actions.WriteAllText(psScriptFile, psScript);
211-
212-
var install = new CommandBuilder(builder.Actions).
213-
RunCommand("powershell").
185+
BuildScript GetInstall(string pwsh) =>
186+
new CommandBuilder(builder.Actions).
187+
RunCommand(pwsh).
214188
Argument("-NoProfile").
215189
Argument("-ExecutionPolicy").
216190
Argument("unrestricted").
217-
Argument("-file").
218-
Argument(psScriptFile).
219-
Argument("-Version").
220-
Argument(version).
221-
Argument("-InstallDir").
222-
Argument(path);
223-
224-
var removeScript = new CommandBuilder(builder.Actions).
225-
RunCommand("del").
226-
Argument(psScriptFile);
191+
Argument("-Command").
192+
Argument("\"" + psCommand + "\"").
193+
Script;
227194

228-
return install.Script & BuildScript.Try(removeScript.Script);
195+
return GetInstall("pwsh") | GetInstall("powershell");
229196
}
230197
else
231198
{

0 commit comments

Comments
 (0)