@@ -81,22 +81,46 @@ function AddLocalDotnetDirPath {
81
81
}
82
82
}
83
83
84
- function Find-Dotnet
84
+ function Find-DotnetVersionsToInstall
85
85
{
86
86
AddLocalDotnetDirPath
87
87
$listSdksOutput = dotnet -- list- sdks
88
88
$installedDotnetSdks = $listSdksOutput | ForEach-Object { $_.Split (" " )[0 ] }
89
89
Write-Host " Detected dotnet SDKs: $ ( $installedDotnetSdks -join ' , ' ) "
90
+ $missingVersions = [System.Collections.Generic.List [string ]]::new()
90
91
foreach ($majorMinorVersion in $DotnetSDKVersionRequirements.Keys ) {
91
92
$minimalVersion = " $majorMinorVersion .$ ( $DotnetSDKVersionRequirements [$majorMinorVersion ].MinimalPatch) "
92
93
$firstAcceptable = $installedDotnetSdks |
93
94
Where-Object { $_.StartsWith (" $majorMinorVersion ." ) } |
94
95
Where-Object { [System.Management.Automation.SemanticVersion ]::new($_ ) -ge [System.Management.Automation.SemanticVersion ]::new($minimalVersion ) } |
95
96
Select-Object - First 1
96
- if (-not $firstAcceptable ) {
97
- throw " Cannot find the dotnet SDK for .NET Core $majorMinorVersion . Version $minimalVersion or higher is required. Please specify '-Bootstrap' to install build dependencies."
97
+ if ($firstAcceptable ) {
98
+ Write-Host " Found dotnet SDK $firstAcceptable for .NET Core $majorMinorVersion "
99
+ }
100
+ else {
101
+ Write-Host " Cannot find the dotnet SDK for .NET Core $majorMinorVersion . Version $minimalVersion or higher is required."
102
+ $missingVersions.Add (" $majorMinorVersion .$ ( $DotnetSDKVersionRequirements [$majorMinorVersion ].DefaultPatch) " )
98
103
}
99
104
}
105
+ return $missingVersions
106
+ }
107
+
108
+
109
+ $installScript = if ($IsWindows ) { " dotnet-install.ps1" } else { " dotnet-install.sh" }
110
+ $obtainUrl = " https://raw.githubusercontent.com/dotnet/cli/master/scripts/obtain"
111
+
112
+ function Install-DotnetVersion ($Version , $Channel ) {
113
+ if ((Test-Path $installScript ) -ne $True ) {
114
+ Write-Host " Downloading dotnet-install script"
115
+ Invoke-WebRequest - Uri $obtainUrl / $installScript - OutFile $installScript
116
+ }
117
+
118
+ Write-Host " Installing dotnet SDK version $Version "
119
+ if ($IsWindows ) {
120
+ & .\$installScript - InstallDir " $env: ProgramFiles /dotnet" - Channel $Channel - Version $Version
121
+ } else {
122
+ bash ./ $installScript -- install-dir / usr/ share/ dotnet - c $Channel - v $Version
123
+ }
100
124
}
101
125
102
126
function Install-Dotnet {
@@ -105,25 +129,18 @@ function Install-Dotnet {
105
129
[string ]$Channel = ' release'
106
130
)
107
131
try {
108
- Find-Dotnet
109
- return # Simply return if we find dotnet SDk with the correct version
110
- } catch { }
111
- $obtainUrl = " https://raw.githubusercontent.com/dotnet/cli/master/scripts/obtain"
112
- try {
113
- $installScript = if ($IsWindows ) { " dotnet-install.ps1" } else { " dotnet-install.sh" }
114
- Invoke-WebRequest - Uri $obtainUrl / $installScript - OutFile $installScript
115
- foreach ($majorMinorVersion in $DotnetSDKVersionRequirements.Keys ) {
116
- $version = " $majorMinorVersion .$ ( $DotnetSDKVersionRequirements [$majorMinorVersion ].DefaultPatch) "
117
- Write-Host " Installing dotnet SDK version $version "
118
- if ($IsWindows ) {
119
- & .\$installScript - InstallDir " $env: ProgramFiles /dotnet" - Channel $Channel - Version $Version
120
- } else {
121
- bash ./ $installScript -- install-dir / usr/ share/ dotnet - c $Channel - v $Version
122
- }
132
+ $missingVersions = Find-DotnetVersionsToInstall
133
+ if ($missingVersions.Count -eq 0 ) {
134
+ return
135
+ }
136
+ foreach ($missingMajorMinorVersion in $missingVersions ) {
137
+ Install-DotnetVersion - Version $missingMajorMinorVersion - Channel $Channel
123
138
}
124
139
AddLocalDotnetDirPath
125
- }
140
+ }
126
141
finally {
127
- Remove-Item $installScript - Force - ErrorAction SilentlyContinue
142
+ if (Test-Path $installScript ) {
143
+ Remove-Item $installScript - Force - ErrorAction SilentlyContinue
144
+ }
128
145
}
129
146
}
0 commit comments