/
Helper.psm1
172 lines (144 loc) · 5.04 KB
/
Helper.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
160
161
162
163
164
165
166
167
168
169
170
171
172
Set-StrictMode -Version latest
$ErrorActionPreference = "Stop"
[String]$ScriptDir = Split-Path $script:MyInvocation.MyCommand.Path
# -------------------------------------------------------------------------------------------------------------------------------------
# global configuration (environment variables)
[string] $env:ENVRUN_DATABASE = "$ScriptDir\..\_build\EnvRun.db"
# Tools
[string] $EnvRunToolPath = "$ScriptDir\bin\envrun.exe"
# -------------------------------------------------------------------------------------------------------------------------------------
# ###############################################################################################################################
# Helper functions
# ###############################################################################################################################
$global:EnvRunExecLastError = 0
function Exec
{
[CmdletBinding()]
param(
[Parameter(Position=0,Mandatory=1)][scriptblock] $cmd,
[Parameter()][string] $errorMessage = ("Error executing command {0}" -f $cmd),
[Parameter()][switch] $continueOnError
)
& $cmd
if (!$continueOnError -And $lastexitcode -ne 0) {
throw ("Exec: " + $errorMessage)
}
$global:EnvRunExecLastError = $lastexitcode
}
function EnvRunExec
{
[CmdletBinding()]
param(
[Parameter(Position=0,Mandatory=1)][string[]] $cmd,
[Parameter()][switch] $continueOnError
)
$cmd = '''' + ($cmd -join ''', ''') + ''''
$scriptCode = "& ""$EnvRunToolPath"" ($cmd)"
$script = [scriptblock]::Create($scriptCode)
# echo "SCRIPT: $script"
if ($continueOnError) {
Exec $script -continueOnError
} else {
Exec $script
}
# read environment variables captured by envrun to make them available in powershell
LoadCapturedEnvRunVariables
}
function LoadCapturedEnvRunVariables
{
# read captured environment variables and make them available in powershell
# (otherwise only new processes within envrun can consume them)
$regex = "^(?<name>\w+) = '(?<value>.*)'$"
foreach($line in Get-Content "$env:ENVRUN_DATABASE") {
if($line -match $regex){
[Environment]::SetEnvironmentVariable($Matches.name, $Matches.value, "Process")
}
}
}
function ExecBlock
{
[CmdletBinding()]
param(
[Parameter(Position=0,Mandatory=1)][scriptblock] $cmd,
[Parameter()][string] $errorMessage = ("Error executing command {0}" -f $cmd)
)
Try
{
& $cmd
}
Catch [Exception]
{
echo $_.Exception|format-list -force
exit(-1)
}
}
# -------------------------------------------------------------------------------------------------------------------------------------
# load envrun.db, if available to make captured environment variables available
if (Test-Path "$env:ENVRUN_DATABASE")
{
LoadCapturedEnvRunVariables
}
# -------------------------------------------------------------------------------------------------------------------------------------
function DownloadBuildTool_Internal
{
Param
(
[Parameter()][string] $ToolName,
[Parameter()][string] $ToolDownloadUrl,
[Parameter()][string] $ToolVersionHash
)
$7ZipToolPath = "$ScriptDir\bin\7zip\7z.exe"
$ToolArchive = "$ScriptDir\_tools\$ToolVersionHash.zip"
$ToolDirectory = "$ScriptDir\_tools\$ToolVersionHash"
Write-Host -ForegroundColor "Green" "Check if $ToolName in version '$ToolVersionHash' already exists..."
if (!(Test-Path $ToolDirectory -PathType Container))
{
Write-Host -ForegroundColor "Green" "$ToolName is missing and is now downloaded from '$ToolDownloadUrl'..."
$TempFile = [System.IO.Path]::GetTempFileName()
Try
{
[Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12;
$wc = [System.Net.WebClient]::new()
$wc.DownloadFile($ToolDownloadUrl, $TempFile)
$wc.Dispose()
# check hash
Write-Host -ForegroundColor "Green" "Checking hash of '$TempFile'..."
$FileHash = $(Get-FileHash -Algorithm SHA256 $TempFile).Hash
Write-Host -ForegroundColor "Green" "Hash is '$FileHash'."
# interrupt processing if hash values do not match
if ($FileHash -ne $ToolVersionHash)
{
Write-Host -ForegroundColor "Red" "Hash of downloaded '$TempFile' does not match. Stopping further processing..."
throw "Processing aborted. Please see log for further details..."
}
Try
{
# extract zip archive and store path to the executable
Write-Host -ForegroundColor "Green" "Extracting zip archive of $ToolName..."
EnvRunExec ("$7ZipToolPath", "x", "$TempFile", "-o$ToolDirectory", "-y")
Write-Host -ForegroundColor "Green" "Extracting zip archive succeeded."
}
Catch
{
Remove-Item -Recurse $ToolDirectory
throw
}
}
Finally
{
Remove-Item -Force $TempFile
}
}
Write-Output "" # ensures that DownloadBuildTool() works with its [-1]
Write-Output "$ToolDirectory"
}
function DownloadBuildTool
{
Param
(
[Parameter()][string] $ToolName,
[Parameter()][string] $ToolDownloadUrl,
[Parameter()][string] $ToolVersionHash
)
$(DownloadBuildTool_Internal $ToolName $ToolDownloadUrl $ToolVersionHash)[-1]
}