@@ -7,85 +7,69 @@ using namespace System.Runtime.InteropServices
7
7
8
8
$IsWindowsEnv = [RuntimeInformation ]::IsOSPlatform([OSPlatform ]::Windows)
9
9
$RepoRoot = (Resolve-Path " $PSScriptRoot /.." ).Path
10
- $MinimalSDKVersion = ' 2.1.805'
11
- $DefaultSDKVersion = ' 2.1.805'
12
- $LocalDotnetDirPath = if ($IsWindowsEnv ) { " $env: LocalAppData \Microsoft\dotnet" } else { " $env: HOME /.dotnet" }
13
- $GrpcToolsVersion = ' 2.27.0' # grpc.tools
14
- $GoogleProtobufToolsVersion = ' 3.11.4' # google.protobuf.tools
15
10
16
- function Find-Dotnet
17
- {
18
- $dotnetFile = if ($IsWindowsEnv ) { " dotnet.exe" } else { " dotnet" }
19
- $dotnetExePath = Join-Path - Path $LocalDotnetDirPath - ChildPath $dotnetFile
20
-
21
- # If dotnet is already in the PATH, check to see if that version of dotnet can find the required SDK.
22
- # This is "typically" the globally installed dotnet.
23
- $foundDotnetWithRightVersion = $false
24
- $dotnetInPath = Get-Command ' dotnet' - ErrorAction SilentlyContinue
25
- if ($dotnetInPath ) {
26
- $foundDotnetWithRightVersion = Test-DotnetSDK $dotnetInPath.Source
27
- }
11
+ $DotnetSDKVersionRequirements = @ {
28
12
29
- if (-not $foundDotnetWithRightVersion ) {
30
- if (Test-DotnetSDK $dotnetExePath ) {
31
- Write-Warning " Can't find the dotnet SDK version $MinimalSDKVersion or higher, prepending '$LocalDotnetDirPath ' to PATH."
32
- $env: PATH = $LocalDotnetDirPath + [IO.Path ]::PathSeparator + $env: PATH
33
- }
34
- else {
35
- throw " Cannot find the dotnet SDK for .NET Core 2.1. Please specify '-Bootstrap' to install build dependencies."
36
- }
13
+ ' 2.1' = @ {
14
+ MinimalPatch = ' 818'
15
+ DefaultPatch = ' 818'
37
16
}
38
- }
39
17
40
- function Get-VersionCore ($Version ) {
41
- if ($Version -match ' ^\d+\.\d+\.\d+' ) {
42
- $Matches.0
43
- } else {
44
- throw " Unexpected version: '$Version '"
18
+ # .NET SDK 3.1 is required by the Microsoft.ManifestTool.dll tool
19
+ ' 3.1' = @ {
20
+ MinimalPatch = ' 417'
21
+ DefaultPatch = ' 417'
45
22
}
46
23
}
47
24
48
- function Test-DotnetSDK
25
+ $GrpcToolsVersion = ' 2.27.0' # grpc.tools
26
+ $GoogleProtobufToolsVersion = ' 3.11.4' # google.protobuf.tools
27
+
28
+ function Find-Dotnet
49
29
{
50
- param ($dotnetExePath )
30
+ $listSdksOutput = dotnet -- list- sdks
31
+ $installedDotnetSdks = $listSdksOutput | ForEach-Object { $_.Split (" " )[0 ] }
32
+ Write-Log " Detected dotnet SDKs: $ ( $installedDotnetSdks -join ' , ' ) "
51
33
52
- if (Test-Path $dotnetExePath ) {
53
- $installedVersion = Get-VersionCore (& $dotnetExePath -- version)
54
- return [version ]$installedVersion -ge [version ]$MinimalSDKVersion
34
+ foreach ($majorMinorVersion in $DotnetSDKVersionRequirements.Keys ) {
35
+ $minimalVersion = " $majorMinorVersion .$ ( $DotnetSDKVersionRequirements [$majorMinorVersion ].MinimalPatch) "
36
+
37
+ $firstAcceptable = $installedDotnetSdks |
38
+ Where-Object { $_.StartsWith (" $majorMinorVersion ." ) } |
39
+ Where-Object { [System.Management.Automation.SemanticVersion ]::new($_ ) -ge [System.Management.Automation.SemanticVersion ]::new($minimalVersion ) } |
40
+ Select-Object - First 1
41
+
42
+ if (-not $firstAcceptable ) {
43
+ throw " Cannot find the dotnet SDK for .NET Core $majorMinorVersion . Version $minimalVersion or higher is required. Please specify '-Bootstrap' to install build dependencies."
44
+ }
55
45
}
56
- return $false
57
46
}
58
47
59
48
function Install-Dotnet {
60
49
[CmdletBinding ()]
61
50
param (
62
- [string ]$Channel = ' release' ,
63
- [string ]$Version = $DefaultSDKVersion
51
+ [string ]$Channel = ' release'
64
52
)
65
53
66
54
try {
67
55
Find-Dotnet
68
56
return # Simply return if we find dotnet SDk with the correct version
69
57
} catch { }
70
58
71
- $logMsg = if (Get-Command ' dotnet' - ErrorAction SilentlyContinue) {
72
- " dotent SDK is not present. Installing dotnet SDK."
73
- } else {
74
- " dotnet SDK out of date. Require '$MinimalSDKVersion ' but found '$dotnetSDKVersion '. Updating dotnet."
75
- }
76
- Write-Log $logMsg - Warning
77
-
78
- $obtainUrl = " https://raw.githubusercontent.com/dotnet/cli/master/scripts/obtain"
59
+ $obtainUrl = " https://raw.githubusercontent.com/dotnet/install-scripts/main/src"
79
60
80
61
try {
81
- Remove-Item $LocalDotnetDirPath - Recurse - Force - ErrorAction SilentlyContinue
82
62
$installScript = if ($IsWindowsEnv ) { " dotnet-install.ps1" } else { " dotnet-install.sh" }
83
63
Invoke-WebRequest - Uri $obtainUrl / $installScript - OutFile $installScript
84
64
85
- if ($IsWindowsEnv ) {
86
- & .\$installScript - Channel $Channel - Version $Version
87
- } else {
88
- bash ./ $installScript - c $Channel - v $Version
65
+ foreach ($majorMinorVersion in $DotnetSDKVersionRequirements.Keys ) {
66
+ $version = " $majorMinorVersion .$ ( $DotnetSDKVersionRequirements [$majorMinorVersion ].DefaultPatch) "
67
+ Write-Log " Installing dotnet SDK version $version " - Warning
68
+ if ($IsWindowsEnv ) {
69
+ & .\$installScript - Channel $Channel - Version $Version - InstallDir " $env: ProgramFiles /dotnet"
70
+ } else {
71
+ bash ./ $installScript - c $Channel - v $Version -- install-dir / usr/ share/ dotnet
72
+ }
89
73
}
90
74
}
91
75
finally {
@@ -131,7 +115,7 @@ function Resolve-ProtoBufToolPath
131
115
{
132
116
if (-not $Script :protoc_Path ) {
133
117
Write-Log " Resolve the protobuf tools for auto-generating code"
134
- $nugetPath = " ~/.nuget/packages "
118
+ $nugetPath = Get-NugetPackagesPath
135
119
$toolsPath = " $RepoRoot /tools"
136
120
137
121
if (-not (Test-Path " $toolsPath /obj/project.assets.json" )) {
@@ -205,6 +189,23 @@ function Write-Log
205
189
Write-Host - ForegroundColor $foregroundColor " ${indentPrefix}${Message} "
206
190
}
207
191
192
+ function Get-NugetPackagesPath
193
+ {
194
+ if ($env: NUGET_PACKAGES )
195
+ {
196
+ return $env: NUGET_PACKAGES
197
+ }
198
+
199
+ if ($IsWindowsEnv )
200
+ {
201
+ return " ${env: USERPROFILE} \.nuget\packages"
202
+ }
203
+ else
204
+ {
205
+ return " ${env: HOME} /.nuget/packages"
206
+ }
207
+ }
208
+
208
209
# region Start-ResGen
209
210
210
211
$generated_code_template = @'
0 commit comments