forked from Azure/azure-iot-sdk-csharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.ps1
352 lines (284 loc) · 10 KB
/
build.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# Copyright (c) Microsoft. All rights reserved.
# Licensed under the MIT license. See LICENSE file in the project root for full license information.
<#
.SYNOPSIS
Microsoft Azure IoT SDK .NET build script.
.DESCRIPTION
Builds Azure IoT SDK binaries.
Parameters:
-clean: Runs dotnet clean. Use `git clean -xdf` if this is not sufficient.
-nobuild: Skips build step (use if re-running tests after a successful build).
-nounittests: Skips Unit Tests
-nopackage: Skips NuGet packaging
-e2etests: Runs E2E tests. Requires prerequisites and environment variables.
-stresstests: Runs Stress tests.
-xamarintests: Runs Xamarin tests. Requires additional SDKs and prerequisite configuration.
-sign: (Internal use, requires signing toolset) Signs the binaries before release.
-publish: (Internal use, requires nuget toolset) Publishes the nuget packages.
-configuration {Debug|Release}
-verbosity: Sets the verbosity level of the command. Allowed values are q[uiet], m[inimal], n[ormal], d[etailed], and diag[nostic].
Build will automatically detect if the machine is Windows vs Unix. On Windows development boxes, additional testing on .NET Framework will be performed.
The following environment variables can tune the build behavior:
- AZURE_IOT_DONOTSIGN: disables delay-signing if set to 'TRUE'
- AZURE_IOT_LOCALPACKAGES: the path to the local nuget source.
Add a new source using: `nuget sources add -name MySource -Source <path>`
Remove a source using: `nuget sources remove -name MySource`
.EXAMPLE
.\build
Builds a Debug version of the SDK.
.EXAMPLE
.\build -config Release
Builds a Release version of the SDK.
.EXAMPLE
.\build -clean -e2etests -xamarintests
Builds and runs all tests (requires prerequisites).
.EXAMPLE
.\build -nobuild -nounittests -nopackage -stresstests
Builds stress tests after a successful build.
.LINK
https://github.com/azure/azure-iot-sdk-csharp
#>
Param(
[switch] $clean,
[switch] $nobuild,
[switch] $nounittests,
[switch] $nopackage,
[switch] $e2etests,
[switch] $stresstests,
[switch] $xamarintests,
[switch] $sign,
[switch] $publish,
[string] $configuration = "Debug",
[string] $verbosity = "q"
)
Function IsWindowsDevelopmentBox()
{
return ([Environment]::OSVersion.Platform -eq [System.PlatformID]::Win32NT)
}
Function CheckSignTools()
{
$commands = $("SignDotNetBinary", "SignBinary", "SignNuGetPackage", "SignMSIPackage")
CheckTools $commands
}
Function CheckPublishTools()
{
$commands = $("PushNuGet")
CheckTools $commands
}
Function CheckTools($commands)
{
foreach($command in $commands)
{
$info = Get-Command $command -ErrorAction SilentlyContinue
if ($info -eq $null)
{
throw "Toolset not found: '$command' is missing."
}
}
}
Function BuildProject($path, $message) {
$label = "BUILD: --- $message $configuration ---"
Write-Host
Write-Host -ForegroundColor Cyan $label
cd (Join-Path $rootDir $path)
if ($clean) {
& dotnet clean --verbosity $verbosity --configuration $configuration
if ($LASTEXITCODE -ne 0) {
throw "Clean failed: $label"
}
}
& dotnet build --verbosity $verbosity --configuration $configuration
if ($LASTEXITCODE -ne 0) {
throw "Build failed: $label"
}
}
Function BuildPackage($path, $message) {
$label = "PACK: --- $message $configuration ---"
Write-Host
Write-Host -ForegroundColor Cyan $label
$projectPath = Join-Path $rootDir $path
cd $projectPath
$projectName = (dir (Join-Path $projectPath *.csproj))[0].BaseName
if ($sign)
{
Write-Host -ForegroundColor Magenta "`tSigning binaries: $projectName"
$filesToSign = dir -Recurse .\bin\Release\$projectName.dll
SignDotNetBinary $filesToSign
}
& dotnet pack --verbosity $verbosity --configuration $configuration --no-build --include-symbols --include-source --output $localPackages
if ($LASTEXITCODE -ne 0) {
throw "Package failed: $label"
}
if ($sign)
{
Write-Host -ForegroundColor Magenta "`tSigning package: $projectName"
$filesToSign = dir (Join-Path $localPackages "$projectName.*.nupkg")
SignNuGetPackage $filesToSign
}
}
Function RunTests($path, $message, $framework="*") {
$label = "TEST: --- $message $configuration ---"
Write-Host
Write-Host -ForegroundColor Cyan $label
cd (Join-Path $rootDir $path)
if ($framework -eq "*")
{
& dotnet test --verbosity $verbosity --configuration $configuration --logger "trx"
}
else
{
& dotnet test --framework $framework --verbosity $verbosity --configuration $configuration --logger "trx"
}
if ($LASTEXITCODE -ne 0) {
throw "Tests failed: $label"
}
}
Function RunApp($path, $message, $framework="netcoreapp2.1") {
$label = "RUN: --- $message $configuration ---"
Write-Host
Write-Host -ForegroundColor Cyan $label
cd (Join-Path $rootDir $path)
& dotnet run --framework $framework --verbosity $verbosity --configuration $configuration --logger "trx"
if ($LASTEXITCODE -ne 0) {
throw "Tests failed: $label"
}
}
$rootDir = (Get-Item -Path ".\" -Verbose).FullName
$localPackages = Join-Path $rootDir "bin\pkg"
$startTime = Get-Date
$buildFailed = $true
$errorMessage = ""
try {
if ($sign)
{
if ([string]::IsNullOrWhiteSpace($env:AZURE_IOT_LOCALPACKAGES))
{
throw "Local NuGet package source path is not set, required when signing packages."
}
CheckSignTools
}
if ($publish)
{
CheckPublishTools
}
if (-not $nobuild)
{
# SDK binaries
BuildProject . "Azure IoT C# SDK Solution"
}
# Unit Tests require InternalsVisibleTo and can only run in Debug builds.
if ((-not $nounittests) -and ($configuration.ToUpperInvariant() -eq "DEBUG"))
{
Write-Host
Write-Host -ForegroundColor Cyan "Unit Test execution"
Write-Host
RunTests iothub\device\tests "IoT Hub DeviceClient Tests"
RunTests iothub\service\tests "IoT Hub ServiceClient Tests"
RunTests provisioning\device\tests "Provisioning Device Client Tests"
RunTests provisioning\transport\amqp\tests "Provisioning Transport for AMQP Tests"
RunTests provisioning\transport\http\tests "Provisioning Transport for HTTP Tests"
RunTests provisioning\transport\mqtt\tests "Provisioning Transport for MQTT Tests"
RunTests security\tpm\tests "SecurityProvider for TPM Tests"
RunTests provisioning\service\tests "Provisioning Service Client Tests"
}
if ((-not $nopackage))
{
BuildPackage shared\src "Shared Assembly"
BuildPackage iothub\device\src "IoT Hub DeviceClient SDK"
BuildPackage iothub\service\src "IoT Hub ServiceClient SDK"
BuildPackage security\tpm\src "SecurityProvider for TPM"
BuildPackage provisioning\device\src "Provisioning Device Client SDK"
BuildPackage provisioning\transport\amqp\src "Provisioning Transport for AMQP"
BuildPackage provisioning\transport\http\src "Provisioning Transport for HTTP"
BuildPackage provisioning\transport\mqtt\src "Provisioning Transport for MQTT"
BuildPackage provisioning\service\src "Provisioning Service Client SDK"
}
if (-not [string]::IsNullOrWhiteSpace($env:AZURE_IOT_LOCALPACKAGES))
{
Write-Host
Write-Host -ForegroundColor Cyan "Preparing local package source"
Write-Host
if (-not (Test-Path $env:AZURE_IOT_LOCALPACKAGES))
{
throw "Local NuGet package source path invalid: $($env:AZURE_IOT_LOCALPACKAGES)"
}
# Clear the NuGet cache and the old packages.
dotnet nuget locals --clear all
Remove-Item $env:AZURE_IOT_LOCALPACKAGES\*.*
# Copy new packages.
copy (Join-Path $rootDir "bin\pkg\*.*") $env:AZURE_IOT_LOCALPACKAGES
}
if ($e2etests)
{
Write-Host
Write-Host -ForegroundColor Cyan "End-to-end Test execution"
Write-Host
# Override verbosity to display individual test execution.
$oldVerbosity = $verbosity
$verbosity = "normal"
if (IsWindowsDevelopmentBox)
{
RunTests e2e\test "End-to-end tests (NetCoreApp2.1, NET47, NET451)"
}
else
{
RunTests e2e\test "End-to-end tests (NetCoreApp2.1)" "netcoreapp2.1"
}
$verbosity = $oldVerbosity
# Samples
BuildProject security\tpm\samples "SecurityProvider for TPM Samples"
# Xamarin samples (require Android, iOS and UWP SDKs and configured iOS remote)
if ($xamarintests)
{
# TODO #335 - create new Xamarin automated samples/tests
}
}
if ($stresstests)
{
Write-Host
Write-Host -ForegroundColor Cyan "Stress Test execution"
Write-Host
RunApp e2e\stress\MemoryLeakTest "MemoryLeakTest test"
}
# Xamarin samples (require Android, iOS and UWP SDKs and configured iOS remote)
if ($xamarintests)
{
# TODO #335 - create new Xamarin automated samples/tests
}
if ($publish)
{
$files = dir $rootDir\bin\pkg\*.nupkg | where {-not ($_.Name -match "symbols")}
$publishResult = PushNuGet $files
foreach( $result in $publishResult)
{
if($result.success)
{
Write-Host -ForegroundColor Green "OK : $($result.file.FullName)"
}
else
{
Write-Host -ForegroundColor Red "FAILED: $($result.file.FullName)"
}
}
}
$buildFailed = $false
}
catch [Exception]{
$buildFailed = $true
$errorMessage = $Error[0]
}
finally {
cd $rootDir
$endTime = Get-Date
}
Write-Host
Write-Host
Write-Host ("Time Elapsed {0:c}" -f ($endTime - $startTime))
if ($buildFailed) {
Write-Host -ForegroundColor Red "Build failed ($errorMessage)"
exit 1
}
else {
Write-Host -ForegroundColor Green "Build succeeded."
exit 0
}