1+ function Invoke-InstallDotNet {
2+ [CmdletBinding ()]
3+ param (
4+ [Parameter (Mandatory )]
5+ [string ]
6+ $Version
7+ )
8+
9+ function Test-Version {
10+ param (
11+ [Parameter (Mandatory )]
12+ [System.Management.Automation.SemanticVersion ]
13+ $Target ,
14+
15+ [Parameter (Mandatory )]
16+ [System.Management.Automation.SemanticVersion ]
17+ $Test
18+ )
19+
20+ # 6.0 vs 7.0
21+ if ($Target.Major -ne $Test.Major -or $Target.Minor -ne $Test.Minor ) {
22+ $false
23+ }
24+ else {
25+ # 6.0.0 vs 6.0.1
26+ # 7.0.100 vs 7.0.100-rc.2.22477.23
27+ $Target.CompareTo ($Test ) -le 0
28+ }
29+ }
30+
31+ if (Get-Command - Name dotnet - ErrorAction SilentlyContinue) {
32+ $versions = dotnet -- list- sdks
33+ foreach ($installedVersion in $versions ) {
34+ # 6.0.401 [C:\Program Files\dotnet\sdk]
35+ $test = ($installedVersion -split " " )[0 ]
36+
37+ if (Test-Version - Target $Version - Test $test ) {
38+ Write-Output " .net sdk $test is alredy installed"
39+ return
40+ }
41+ }
42+ }
43+
44+ $installDir = " C:\Program Files\dotnet"
45+ $installScript = " dotnet-install.ps1"
46+
47+ if ($IsLinux ) {
48+ $installDir = " /usr/share/dotnet"
49+ $installScript = " dotnet-install.sh"
50+ }
51+
52+ $downloadDir = Join-Path ([System.IO.Path ]::GetTempPath()) " install-dotnet"
53+ if (Test-Path $downloadDir ) {
54+ Remove-Item - Path $downloadDir - Recurse - Force
55+ }
56+
57+ New-Item - Path $downloadDir - ItemType Directory | Out-Null
58+
59+ $dotnetInstall = Join-Path $downloadDir $installScript
60+ Invoke-WebRequest - Uri " https://dot.net/v1/$installScript " - OutFile $dotnetInstall
61+
62+ if ($IsLinux ) {
63+ chmod + x $dotnetInstall
64+ }
65+
66+ " $dotnetInstall -Version $Version -InstallDir $installDir "
67+ & $dotnetInstall - Version $Version - InstallDir $installDir
68+ }
0 commit comments