-
Notifications
You must be signed in to change notification settings - Fork 24
/
pipeline.build.ps1
262 lines (214 loc) · 8.43 KB
/
pipeline.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
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
[CmdletBinding()]
param (
[Parameter(Mandatory = $False)]
[String]$Build = '0.0.1',
[Parameter(Mandatory = $False)]
[String]$Rev = '0',
[Parameter(Mandatory = $False)]
[String]$ExtensionTag = '',
[Parameter(Mandatory = $False)]
[String]$Configuration = 'Debug'
)
Write-Host -Object "[Pipeline] -- PWD: $PWD" -ForegroundColor Green;
Write-Host -Object "[Pipeline] -- BuildNumber: $($Env:BUILD_BUILDNUMBER)" -ForegroundColor Green;
Write-Host -Object "[Pipeline] -- SourceBranch: $($Env:BUILD_SOURCEBRANCH)" -ForegroundColor Green;
Write-Host -Object "[Pipeline] -- SourceBranchName: $($Env:BUILD_SOURCEBRANCHNAME)" -ForegroundColor Green;
if ($Env:SYSTEM_DEBUG -eq 'true') {
$VerbosePreference = 'Continue';
}
$version = $Build;
if ($Env:BUILD_SOURCEBRANCH -like '*/tags/*' -and $Env:BUILD_SOURCEBRANCHNAME -like 'v2.*') {
$version = $Env:BUILD_SOURCEBRANCHNAME.Substring(1);
}
$versionSuffix = [String]::Empty;
if ($version -like '*-*') {
[String[]]$versionParts = $version.Split('-', [System.StringSplitOptions]::RemoveEmptyEntries);
$version = $versionParts[0];
if ($versionParts.Length -eq 2) {
$versionSuffix = $versionParts[1];
}
}
Write-Host -Object "[Pipeline] -- Using version: $version" -ForegroundColor Green;
Write-Host -Object "[Pipeline] -- Using versionSuffix: $versionSuffix" -ForegroundColor Green;
# A list of tasks included in the extension
$tasks = @((Get-ChildItem -Path tasks/ -Directory).Name)
# Copy the extension files to the destination path
function CopyExtensionFiles {
[CmdletBinding()]
param (
[Parameter(Mandatory = $True)]
[String]$Path,
[Parameter(Mandatory = $True)]
[String]$DestinationPath
)
process {
$sourcePath = Resolve-Path -Path $Path;
Get-ChildItem -Path $sourcePath -File -Include *.ps1,*.json,*.png -Recurse | Where-Object {
($_.FullName -notmatch '(\\|\/)(node_modules)') -and
($_.FullName -notcontains 'package.json')
} | ForEach-Object {
$filePath = $_.FullName.Replace($sourcePath, $DestinationPath);
$parentPath = Split-Path -Path $filePath -Parent;
if (!(Test-Path -Path $parentPath)) {
$Null = New-Item -Path $parentPath -ItemType Directory -Force;
}
Copy-Item -Path $_.FullName -Destination $filePath -Force;
};
}
}
function UpdateTaskVersion {
[CmdletBinding()]
param (
[Parameter(Mandatory = $True)]
[String]$Path
)
process {
$v = $Build.Split('.', [System.StringSplitOptions]::RemoveEmptyEntries);
$taskVersion = [int]::Parse($v[2]);
Write-Host "Using task version: $taskVersion"
Get-ChildItem -Path $Path -Filter task.json -Recurse | ForEach-Object {
$filePath = $_.FullName;
$taskContent = Get-Content -Raw -Path $filePath | ConvertFrom-Json;
$taskContent.version.Patch = $taskVersion;
$taskContent | ConvertTo-Json -Depth 100 | Set-Content -Path $filePath -Force;
}
}
}
# Synopsis: Install NuGet provider
task NuGet {
if ($Null -eq (Get-PackageProvider -Name NuGet -ErrorAction SilentlyContinue)) {
Install-PackageProvider -Name NuGet -Force -Scope CurrentUser;
}
}
task Dependencies NuGet, {
Import-Module $PWD/scripts/dependencies.psm1;
Install-Dependencies -Path $PWD/modules.json;
}
task SaveDependencies NuGet, {
if (!(Test-Path -Path out/dist/ps_modules)) {
$Null = New-Item -Path out/dist/ps_modules -ItemType Directory -Force;
}
Import-Module $PWD/scripts/dependencies.psm1;
Save-Dependencies -Path $PWD/modules.json -OutputPath out/dist/ps_modules;
}
# Synopsis: Remove temp files.
task Clean {
Remove-Item -Path out,reports -Recurse -Force -ErrorAction SilentlyContinue;
}
task CopyExtension {
Write-Host '> Copy extension' -ForegroundColor Green;
foreach ($task in $tasks) {
$taskRoot = $task.Split('V')[0];
CopyExtensionFiles -Path "tasks/$task" -DestinationPath "out/dist/$taskRoot/$task/";
Copy-Item -Path images/icon128.png -Destination "out/dist/$taskRoot/$task/icon.png" -Force;
Push-Location "out/dist/$taskRoot/$task/";
exec { & npm init --force }
exec { & npm install shelljs }
Pop-Location;
}
# Copy manifests
Copy-Item -Path modules.json -Destination out/dist/;
# Copy icon
if (!(Test-Path -Path out/dist/images)) {
$Null = New-Item -Path out/dist/images -ItemType Directory -Force;
}
Copy-Item -Path images/icon128.png -Destination out/dist/images/ -Force;
# Copy repo files
Copy-Item -Path extension.md -Destination out/dist/;
Copy-Item -Path CHANGELOG.md -Destination out/dist/;
Copy-Item -Path LICENSE -Destination out/dist/;
}
task BuildExtension CopyExtension, VersionExtension, SaveDependencies, {
Write-Host '> Building extension' -ForegroundColor Green;
exec { & npm run build }
exec { & npm run -S "package" -- version=$Build }
exec { & npm run -S "package" -- version=$Build official=true }
}
task VersionExtension {
Write-Host '> Version extension' -ForegroundColor Green;
# Update module version
if (![String]::IsNullOrEmpty($version)) {
Write-Verbose -Message "[VersionExtension] -- Updating extension manifest version";
# Write version info
if (!(Test-Path -Path out/dist)) {
$Null = New-Item -Path out/dist -ItemType Directory -Force;
}
$versionInfo = Join-Path -Path out/dist/ -ChildPath 'version.json';
@{ version = $version } | ConvertTo-Json | Set-Content -Path $versionInfo;
}
UpdateTaskVersion -Path out/dist/;
}
# Synopsis: This task reads version info if set and configures a build variable
task GetVersionInfo {
$baseVersion = dotnet-gitversion /showvariable MajorMinorPatch /nofetch;
if (![String]::IsNullOrEmpty($ExtensionTag)) {
$commits = dotnet-gitversion /showvariable CommitsSinceVersionSource /nofetch;
$baseVersion = (Get-Date).ToString("yyyy.M.$commits");
}
Write-Host "[Pipeline] Using EXTENSION_VERSION: $baseVersion";
Write-Host "`#`#vso[task.setvariable variable=EXTENSION_VERSION;]$baseVersion";
if ($Rev -ne '0') {
$taskVersionParts = (dotnet-gitversion /showvariable MajorMinorPatch /nofetch).Split('.', [System.StringSplitOptions]::RemoveEmptyEntries);
$taskVersion = "$($taskVersionParts[0]).$($taskVersionParts[1]).$Rev";
Write-Host "`#`#vso[build.updatebuildnumber]$taskVersion";
}
}
# Synopsis: Run validation
task Rules Dependencies, {
$assertParams = @{
OutputFormat = 'NUnit3'
ErrorAction = 'Stop'
Format = 'File'
InputPath = '.'
}
Assert-PSRule @assertParams -OutputPath reports/ps-rule-file.xml;
}
task TestModule Dependencies, {
# Run Pester tests
$pesterOptions = @{
Run = @{
Path = (Join-Path -Path $PWD -ChildPath tests/);
PassThru = $True;
};
TestResult = @{
Enabled = $True;
OutputFormat = 'NUnitXml';
OutputPath = 'reports/pester-unit.xml';
};
};
if ($CodeCoverage) {
$codeCoverageOptions = @{
Enabled = $True;
OutputPath = (Join-Path -Path $PWD -ChildPath 'reports/pester-coverage.xml');
Path = (Join-Path -Path $PWD -ChildPath 'tasks/**/*.ps1');
};
$pesterOptions.Add('CodeCoverage', $codeCoverageOptions);
}
if (!(Test-Path -Path reports)) {
$Null = New-Item -Path reports -ItemType Directory -Force;
}
if ($Null -ne $TestGroup) {
$pesterOptions.Add('Filter', @{ Tag = $TestGroup });
}
# https://pester.dev/docs/commands/New-PesterConfiguration
$pesterConfiguration = New-PesterConfiguration -Hashtable $pesterOptions;
$results = Invoke-Pester -Configuration $pesterConfiguration;
# Throw an error if pester tests failed
if ($Null -eq $results) {
throw 'Failed to get Pester test results.';
}
elseif ($results.FailedCount -gt 0) {
throw "$($results.FailedCount) tests failed.";
}
}
# Synopsis: Restore NPM packages
task PackageRestore {
exec { & npm install --no-save }
}
# Synopsis: Build and clean.
task . Build, Rules
# Synopsis: Build the project
task Build Clean, PackageRestore, BuildExtension
task Test Build, TestModule