-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.ps1
165 lines (154 loc) · 5.66 KB
/
utils.ps1
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
160
161
162
163
164
165
<#
.DESCRIPTION
Utility methods for common tasks.
#>
function Invoke-CommandLine {
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingInvokeExpression', '', Justification = 'Usually this statement must be avoided (https://learn.microsoft.com/en-us/powershell/scripting/learn/deep-dives/avoid-using-invoke-expression?view=powershell-7.3), here it is OK as it does not execute unknown code.')]
param (
[Parameter(Mandatory = $true, Position = 0)]
[string]$CommandLine,
[Parameter(Mandatory = $false, Position = 1)]
[bool]$StopAtError = $true,
[Parameter(Mandatory = $false, Position = 2)]
[bool]$PrintCommand = $true,
[Parameter(Mandatory = $false, Position = 3)]
[bool]$Silent = $false
)
if ($PrintCommand) {
Write-Output "Executing: $CommandLine"
}
$global:LASTEXITCODE = 0
if ($Silent) {
# Omit information stream (6) and stdout (1)
Invoke-Expression $CommandLine 6>&1 | Out-Null
}
else {
Invoke-Expression $CommandLine
}
if ($global:LASTEXITCODE -ne 0) {
if ($StopAtError) {
Write-Error "Command line call `"$CommandLine`" failed with exit code $global:LASTEXITCODE"
}
else {
Write-Output "Command line call `"$CommandLine`" failed with exit code $global:LASTEXITCODE, continuing ..."
}
}
}
# Update/Reload current environment variable PATH with settings from registry
function Initialize-EnvPath {
# workaround for system-wide installations (e.g. in GitHub Actions)
if ($Env:USER_PATH_FIRST) {
$Env:Path = [System.Environment]::GetEnvironmentVariable("Path", "User") + ";" + [System.Environment]::GetEnvironmentVariable("Path", "Machine")
}
else {
$Env:Path = [System.Environment]::GetEnvironmentVariable("Path", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path", "User")
}
}
function Remove-Path {
[CmdletBinding(SupportsShouldProcess)]
param (
[Parameter(Mandatory = $true, Position = 0)]
[string]$path
)
if (Test-Path -Path $path -PathType Container) {
Write-Output "Deleting directory '$path' ..."
Remove-Item $path -Force -Recurse
}
elseif (Test-Path -Path $path -PathType Leaf) {
Write-Output "Deleting file '$path' ..."
Remove-Item $path -Force
}
}
function New-Directory {
[CmdletBinding(SupportsShouldProcess)]
param (
[Parameter(Mandatory = $true, Position = 0)]
[string]$dir
)
if (-Not (Test-Path -Path $dir)) {
Write-Output "Creating directory '$dir' ..."
New-Item -ItemType Directory $dir
}
}
function CloneOrPullGitRepo {
param (
[Parameter(Mandatory = $true)]
[string]$RepoUrl,
[Parameter(Mandatory = $true)]
[string]$TargetDirectory,
[Parameter(Mandatory = $false)]
[string]$Tag,
[Parameter(Mandatory = $false)]
[string]$Branch = "develop"
)
$baselineName = "branch"
$baseline = $Branch
if ($Tag) {
$baselineName = "tag"
$baseline = $Tag
}
if (Test-Path -Path "$TargetDirectory\.git" -PathType Container) {
# When the repository directory exists, fetch the latest changes and checkout the baseline
try {
Push-Location $TargetDirectory
Invoke-CommandLine "git fetch --tags" -Silent $true
if ($Tag) {
Invoke-CommandLine "git checkout $Tag --quiet" -Silent $true
}
else {
Invoke-CommandLine "git checkout -B $Branch origin/$Branch --quiet" -Silent $true
}
Invoke-CommandLine "git reset --hard"
return
}
catch {
Write-Output "Failed to checkout $baselineName '$baseline' in repository '$RepoUrl' at directory '$TargetDirectory'."
}
finally {
Pop-Location
}
}
# Repo directory does not exist, remove any possible leftovers and get a fresh clone
try {
Remove-Path $TargetDirectory
New-Directory $TargetDirectory
Push-Location $TargetDirectory
Invoke-CommandLine "git -c advice.detachedHead=false clone --branch $baseline $RepoUrl ." -Silent $true
Invoke-CommandLine "git config pull.rebase true" -Silent $true -PrintCommand $false
Invoke-CommandLine "git log -1 --pretty='format:%h %B'" -PrintCommand $false
}
catch {
Write-Output "Failed to clone repository '$RepoUrl' at directory '$TargetDirectory'."
}
finally {
Pop-Location
}
}
function Test-RunningInCIorTestEnvironment {
return [Boolean]($Env:JENKINS_URL -or $Env:PYTEST_CURRENT_TEST -or $Env:GITHUB_ACTIONS)
}
function Get-UserConfirmation {
param (
[Parameter(Mandatory = $true)]
[string]$message,
# Default value of the confirmation prompt
[Parameter(Mandatory = $false)]
[bool]$defaultValueForUser = $true,
# Value when running in CI or test environment
[Parameter(Mandatory = $false)]
[bool]$valueForCi = $false
)
if (Test-RunningInCIorTestEnvironment) {
return $valueForCi
} else {
$defaultText = if ($defaultValueForUser) { "[Y/n]" } else { "[y/N]" }
$userResponse = Read-Host "$message $defaultText"
if ($userResponse -eq '') {
return $defaultValueForUser
} elseif ($userResponse -match '^[Yy]') {
return $true
} else {
return $false
}
}
}