forked from Azure/azure-functions-core-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipelineUtilities.psm1
124 lines (106 loc) · 4.22 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
#
# 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'
}
}
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-Dotnet
{
AddLocalDotnetDirPath
$listSdksOutput = dotnet --list-sdks
$installedDotnetSdks = $listSdksOutput | ForEach-Object { $_.Split(" ")[0] }
Write-Host "Detected dotnet SDKs: $($installedDotnetSdks -join ', ')"
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 (-not $firstAcceptable) {
throw "Cannot find the dotnet SDK for .NET Core $majorMinorVersion. Version $minimalVersion or higher is required. Please specify '-Bootstrap' to install build dependencies."
}
}
}
function Install-Dotnet {
[CmdletBinding()]
param(
[string]$Channel = 'release'
)
try {
Find-Dotnet
return # Simply return if we find dotnet SDk with the correct version
} catch { }
$obtainUrl = "https://raw.githubusercontent.com/dotnet/cli/master/scripts/obtain"
try {
$installScript = if ($IsWindows) { "dotnet-install.ps1" } else { "dotnet-install.sh" }
Invoke-WebRequest -Uri $obtainUrl/$installScript -OutFile $installScript
foreach ($majorMinorVersion in $DotnetSDKVersionRequirements.Keys) {
$version = "$majorMinorVersion.$($DotnetSDKVersionRequirements[$majorMinorVersion].DefaultPatch)"
Write-Host "Installing dotnet SDK version $version"
if ($IsWindows) {
& .\$installScript -InstallDir "$env:ProgramFiles/dotnet" -Channel $Channel -Version $Version
} else {
bash ./$installScript --install-dir /usr/share/dotnet -c $Channel -v $Version
}
}
AddLocalDotnetDirPath
}
finally {
Remove-Item $installScript -Force -ErrorAction SilentlyContinue
}
}