-
Notifications
You must be signed in to change notification settings - Fork 437
/
pipelineUtilities.psm1
159 lines (135 loc) · 5.55 KB
/
pipelineUtilities.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#
# Copyright (c) Microsoft. All rights reserved.
# Licensed under the MIT license. See LICENSE file in the project root for full license information.
#
#Requires -Version 6.0
using namespace System.Runtime.InteropServices
$DLL_NAME = "Microsoft.ManifestTool.dll"
$MANIFESTOOLNAME = "ManifestTool"
$MANIFESTOOL_DIRECTORY = Join-Path $PSScriptRoot $MANIFESTOOLNAME
$MANIFEST_TOOL_PATH = "$MANIFESTOOL_DIRECTORY/$DLL_NAME"
function Get-ManifestToolPath
{
if (Test-Path $MANIFEST_TOOL_PATH)
{
return $MANIFEST_TOOL_PATH
}
throw "The SBOM Manifest Tool is not installed. Please run Install-SBOMUtil -SBOMUtilSASUrl <SASUrl>"
}
function Install-SBOMUtil
{
param(
[string]
$SBOMUtilSASUrl
)
if ([string]::IsNullOrEmpty($SBOMUtilSASUrl))
{
throw "The `$SBOMUtilSASUrl parameter cannot be null or empty when specifying `$(addSBOM)"
}
Write-Host "Installing $MANIFESTOOLNAME..."
Remove-Item -Recurse -Force $MANIFESTOOL_DIRECTORY -ErrorAction Ignore
Invoke-RestMethod -Uri $SBOMUtilSASUrl -OutFile "$MANIFESTOOL_DIRECTORY.zip"
Expand-Archive "$MANIFESTOOL_DIRECTORY.zip" -DestinationPath $MANIFESTOOL_DIRECTORY
if (-not (Test-Path $MANIFEST_TOOL_PATH))
{
throw "$MANIFESTOOL_DIRECTORY does not contain '$DLL_NAME'"
}
Write-Host 'Done.'
return $MANIFEST_TOOL_PATH
}
$DotnetSDKVersionRequirements = @{
# .NET SDK 3.1 is required by the Microsoft.ManifestTool.dll tool
'2.1' = @{
MinimalPatch = '818'
DefaultPatch = '818'
}
# .NET SDK 3.1 is required by the Microsoft.ManifestTool.dll tool
'3.1' = @{
MinimalPatch = '415'
DefaultPatch = '415'
}
'6.0' = @{
MinimalPatch = '417'
DefaultPatch = '417'
}
'8.0' = @{
MinimalPatch = '204'
DefaultPatch = '204'
}
# Update .NET 9 patch once .NET 9 has been released out of preview
'9.0' = @{
MinimalPatch = '100-rc.1.24452.12'
DefaultPatch = '100-rc.1.24452.12'
}
}
function AddLocalDotnetDirPath {
$LocalDotnetDirPath = if ($IsWindows) { "$env:ProgramFiles/dotnet" } else { "/usr/share/dotnet" }
if (($env:PATH -split [IO.Path]::PathSeparator) -notcontains $LocalDotnetDirPath) {
$env:PATH = $LocalDotnetDirPath + [IO.Path]::PathSeparator + $env:PATH
}
}
function Find-DotnetVersionsToInstall
{
AddLocalDotnetDirPath
$listSdksOutput = dotnet --list-sdks
$installedDotnetSdks = $listSdksOutput | ForEach-Object { $_.Split(" ")[0] }
Write-Host "Detected dotnet SDKs: $($installedDotnetSdks -join ', ')"
$missingVersions = [System.Collections.Generic.List[string]]::new()
foreach ($majorMinorVersion in $DotnetSDKVersionRequirements.Keys) {
$minimalVersion = "$majorMinorVersion.$($DotnetSDKVersionRequirements[$majorMinorVersion].MinimalPatch)"
$firstAcceptable = $installedDotnetSdks |
Where-Object { $_.StartsWith("$majorMinorVersion.") } |
Where-Object { [System.Management.Automation.SemanticVersion]::new($_) -ge [System.Management.Automation.SemanticVersion]::new($minimalVersion) } |
Select-Object -First 1
if ($firstAcceptable) {
Write-Host "Found dotnet SDK $firstAcceptable for .NET Core $majorMinorVersion."
}
else {
Write-Host "Cannot find the dotnet SDK for .NET Core $majorMinorVersion. Version $minimalVersion or higher is required."
$missingVersions.Add("$majorMinorVersion.$($DotnetSDKVersionRequirements[$majorMinorVersion].DefaultPatch)")
}
}
return $missingVersions
}
$installScript = if ($IsWindows) { "dotnet-install.ps1" } else { "dotnet-install.sh" }
$obtainUrl = "https://raw.githubusercontent.com/dotnet/cli/master/scripts/obtain"
function Install-DotnetVersion($Version,$Channel) {
if ((Test-Path $installScript) -ne $True) {
Write-Host "Downloading dotnet-install script"
Invoke-WebRequest -Uri $obtainUrl/$installScript -OutFile $installScript
}
Write-Host "Installing dotnet SDK version $Version"
if ($IsWindows) {
& .\$installScript -InstallDir "$env:ProgramFiles/dotnet" -Channel $Channel -Version $Version
# Installing .NET into x86 directory since the E2E App runs the tests on x86 and looks for the specified framework there
& .\$installScript -InstallDir "$env:ProgramFiles (x86)/dotnet" -Channel $Channel -Version $Version -Architecture x86
} else {
bash ./$installScript --install-dir /usr/share/dotnet -c $Channel -v $Version
}
}
function Install-Dotnet {
[CmdletBinding()]
param(
[string]$Channel = 'release'
)
try {
$versionsToInstall = Find-DotnetVersionsToInstall
if ($versionsToInstall.Count -eq 0) {
return
}
foreach ($version in $versionsToInstall) {
Install-DotnetVersion -Version $version -Channel $Channel
}
$listSdksOutput = dotnet --list-sdks
$installedDotnetSdks = $listSdksOutput | ForEach-Object { $_.Split(" ")[0] }
Write-Host "Detected dotnet SDKs: $($installedDotnetSdks -join ', ')"
$listRuntimesOutput = dotnet --list-runtimes
$installedDotnetRuntimes = $listRuntimesOutput | ForEach-Object { $_.Split(" ")[1] }
Write-Host "Detected dotnet Runtimes: $($installedDotnetRuntimes -join ', ')"
}
finally {
if (Test-Path $installScript) {
Remove-Item $installScript -Force -ErrorAction SilentlyContinue
}
}
}