-
Notifications
You must be signed in to change notification settings - Fork 2
/
common.ps1
263 lines (227 loc) · 6.62 KB
/
common.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
param()
function Resolve-Shell-Dependency {
param(
[Parameter(Position = 0, ValueFromPipeline = $true)]
[string] $Command
)
if ($null -eq (Get-Command $Command -ErrorAction SilentlyContinue)) {
Write-Error "Unable to find executable in environment path: $Command"
}
}
function Resolve-Windows {
param(
[Parameter(Position = 0, ValueFromPipeline = $true)]
[string] $OSPlatform
)
if ($OSPlatform -ne "Windows") {
Write-Error "Unable to continue because OS version is not Windows but $OSVersion"
}
}
function Resolve-Unix {
param(
[Parameter(Position = 0, ValueFromPipeline = $true)]
[string] $OSPlatform
)
if ($OSPlatform -ne "Unix") {
Write-Error "Unable to continue because OS version is not Unix but $OSVersion"
}
}
function Write-Output-Colored {
param(
[Parameter(Position = 0, ValueFromPipeline = $true)]
[string] $Message,
[Parameter(Position = 1, ValueFromPipeline = $true)]
[string] $ForegroundColor = "Green"
)
$fc = $host.UI.RawUI.ForegroundColor
$host.UI.RawUI.ForegroundColor = $ForegroundColor
Write-Output $Message
$host.UI.RawUI.ForegroundColor = $fc
}
function Write-Output-Header {
param(
[Parameter(Position = 0, Mandatory = $true, ValueFromPipeline = $true)]
[string] $Message
)
Write-Host
Write-Output-Colored -Message $Message -ForegroundColor Green
Write-Host
}
function Write-Output-Header-Warn {
param(
[Parameter(Position = 0, Mandatory = $true, ValueFromPipeline = $true)]
[string] $Message
)
Write-Host
Write-Output-Colored -Message $Message -ForegroundColor Yellow
Write-Host
}
function Invoke-Command-Clean-Dotnet-Directory {
param(
[Parameter(Position = 0, Mandatory = $true, ValueFromPipeline = $true)]
[string] $Directory
)
if($false -eq (Test-Path $Directory))
{
return
}
$Bin = Join-Path $Directory bin
if(Test-Path $Bin)
{
Write-Output "Removing directory: $Bin"
Get-ChildItem -Path $Bin -Recurse | Remove-Item -Force -Recurse
}
$Obj = Join-Path $Directory obj
if(Test-Path $Obj)
{
Write-Output "Removing directory: $Obj"
Get-ChildItem -Path $Obj -Recurse | Remove-Item -Force -Recurse
}
}
function Invoke-Command-Colored {
param(
[Parameter(Position = 0, Mandatory = $true, ValueFromPipeline = $true)]
[string] $Filename,
[Parameter(Position = 1, ValueFromPipeline = $true)]
[string[]] $Arguments
)
$CommandStr = $Filename
$DashsesRepeatCount = $Filename.Length
if($Arguments.Length -gt 0)
{
$ArgumentsStr = $Arguments -join " "
$CommandStr = "$Filename $ArgumentsStr"
$DashsesRepeatCount = $CommandStr.Length
}
try {
# NB! Accessing this property during a CI build will throw.
# Ref issue: https://github.com/Microsoft/azure-pipelines-tasks/issues/9719
if([console]::BufferWidth -gt 0)
{
$DashsesRepeatCount = [console]::BufferWidth
}
} catch {
$DashsesRepeatCount = 80
}
$DashesStr = "-" * $DashsesRepeatCount
Write-Output-Colored -Message $DashesStr -ForegroundColor White
Write-Output-Colored -Message $CommandStr -ForegroundColor Green
Write-Output-Colored -Message $DashesStr -ForegroundColor White
try {
Invoke-Expression $CommandStr
} finally {
if($LASTEXITCODE -ne 0) {
exit 1
}
}
}
function Get-Msvs-Toolchain-Instance
{
param(
[Parameter(Position = 0, Mandatory = $true, ValueFromPipeline = $true)]
[ValidateSet(16)]
[int] $VisualStudioVersion
)
$Ids = 'Community', 'Professional', 'Enterprise', 'BuildTools' | ForEach-Object { 'Microsoft.VisualStudio.Product.' + $_ }
$Instance = & $CommandVsWhere -version $VisualStudioVersion -products $ids -requires 'Microsoft.Component.MSBuild' -format json `
| convertfrom-json `
| select-object -first 1
return $Instance
}
function Get-Is-String-True
{
param(
[Parameter(Position = 0, ValueFromPipeline = $true)]
$Value
)
if($Value -ieq "true" -or ($Value -ieq "$true"))
{
return $true
}
if($Value -eq "1")
{
return $true
}
return $false
}
function Get-Is-String-False
{
param(
[Parameter(Position = 0, ValueFromPipeline = $true)]
$Value
)
if($Value -ieq "false" -or ($Value -ieq "$false"))
{
return $true
}
if($Value -eq "0")
{
return $true
}
return $false
}
function Use-Msvs-Toolchain
{
param(
[Parameter(Position = 0, Mandatory = $true, ValueFromPipeline = $true)]
[ValidateSet(15, 16)]
[int] $VisualStudioVersion
)
Write-Output-Header "Configuring msvs toolchain"
$script:CommandMsBuild = $null
$Instance = Get-Msvs-Toolchain-Instance $VisualStudioVersion
if ($null -eq $Instance) {
if($VisualStudioVersion -eq 16)
{
Write-Error "Visual Studio 2019 was not found on this computer"
exit 1
} elseif($VisualStudioVersion -eq 15)
{
Write-Error "Visual Studio 2017 was not found on this computer"
exit 1
}
} else {
if($VisualStudioVersion -eq 16)
{
Write-Output "Using Visual Studio 2019 msvs toolset"
} elseif($VisualStudioVersion -eq 15) {
Write-Output "Using Visual Studio 2017 msvs toolset"
} else {
Write-Error "Unknown Visual Studio version: $VisualStudioVersion"
exit 1
}
}
}
# Build targets
function Invoke-Google-Tests
{
param(
[Parameter(Position = 0, Mandatory = $true, ValueFromPipeline = $true)]
[string] $GTestsDirectory,
[Parameter(Position = 1, Mandatory = $true, ValueFromPipeline = $true)]
[string] $GTestsExe,
[Parameter(Position = 2, ValueFromPipeline = $true)]
[string[]] $GTestsArguments
)
try
{
if($null -eq $GTestsArguments) {
$GTestsArguments = @()
}
Push-Location $GTestsDirectory
$GTestsExe = Join-Path $GTestsDirectory $GTestsExe
$GTestsArguments += "--gtest_output=""xml:./googletestsummary.xml"""
Invoke-Command-Colored $GTestsExe $GTestsArguments
} finally {
Pop-Location
}
}
function Convert-Boolean-MSBuild {
param(
[boolean] $Value
)
if ($true -eq $Value) {
return "true"
}
return "false"
}