-
Notifications
You must be signed in to change notification settings - Fork 5
/
helpers.ps1
114 lines (98 loc) · 3.22 KB
/
helpers.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
function Write-Success {
param (
$Text
)
Write-Host -ForegroundColor DarkGreen -NoNewline "✓ "
Write-Host $Text
}
# https://github.com/psake/psake/blob/master/src/public/Exec.ps1
function Exec {
<#
.SYNOPSIS
Helper function for executing command-line programs.
.DESCRIPTION
This is a helper function that runs a scriptblock and checks the PS variable $lastexitcode to see if an error occcured.
If an error is detected then an exception is thrown.
This function allows you to run command-line programs without having to explicitly check fthe $lastexitcode variable.
.PARAMETER cmd
The scriptblock to execute. This scriptblock will typically contain the command-line invocation.
.PARAMETER errorMessage
The error message to display if the external command returned a non-zero exit code.
.PARAMETER maxRetries
The maximum number of times to retry the command before failing.
.PARAMETER retryTriggerErrorPattern
If the external command raises an exception, match the exception against this regex to determine if the command can be retried.
If a match is found, the command will be retried provided [maxRetries] has not been reached.
.PARAMETER workingDirectory
The working directory to set before running the external command.
.EXAMPLE
exec { svn info $repository_trunk } "Error executing SVN. Please verify SVN command-line client is installed"
This example calls the svn command-line client.
.LINK
Assert
.LINK
FormatTaskName
.LINK
Framework
.LINK
Get-PSakeScriptTasks
.LINK
Include
.LINK
Invoke-psake
.LINK
Properties
.LINK
Task
.LINK
TaskSetup
.LINK
TaskTearDown
.LINK
Properties
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[scriptblock]$cmd,
[string]$errorMessage = ($msgs.error_bad_command -f $cmd),
[int]$maxRetries = 0,
[string]$retryTriggerErrorPattern = $null,
[Alias("wd")]
[string]$workingDirectory = $null
)
$tryCount = 1
do {
try {
if ($workingDirectory) {
Push-Location -Path $workingDirectory
}
$global:lastexitcode = 0
& $cmd
if ($global:lastexitcode -ne 0) {
throw "Exec: $errorMessage"
}
break
}
catch [Exception] {
if ($tryCount -gt $maxRetries) {
throw $_
}
if ($retryTriggerErrorPattern -ne $null) {
$isMatch = [regex]::IsMatch($_.Exception.Message, $retryTriggerErrorPattern)
if ($isMatch -eq $false) {
throw $_
}
}
"Try $tryCount failed, retrying again in 1 second..."
$tryCount++
[System.Threading.Thread]::Sleep([System.TimeSpan]::FromSeconds(1))
}
finally {
if ($workingDirectory) {
Pop-Location
}
}
}
while ($true)
}