-
Notifications
You must be signed in to change notification settings - Fork 20
/
pipeline.build.ps1
221 lines (184 loc) · 7.07 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
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# Invoke-Build
# CI pipeline script for PSRule-vscode
[CmdletBinding()]
param (
[Parameter(Mandatory = $False)]
[String]$Build = '0.0.1',
[Parameter(Mandatory = $False)]
[ValidateSet('preview', 'stable', 'dev')]
[String]$Channel = 'preview',
[Parameter(Mandatory = $False)]
[String]$Configuration = 'Debug',
[Parameter(Mandatory = $False)]
[String]$OutputPath = (Join-Path -Path $PWD -ChildPath out),
[Parameter(Mandatory = $False)]
[String]$ApiKey,
[Parameter(Mandatory = $False)]
[String]$AssertStyle = 'AzurePipelines'
)
$commitId = git log --format="%H" -n 1;
Write-Host -Object "[Pipeline] -- PWD: $PWD" -ForegroundColor Green;
Write-Host -Object "[Pipeline] -- OutputPath: $OutputPath" -ForegroundColor Green;
Write-Host -Object "[Pipeline] -- BuildNumber: $($Env:BUILD_BUILDNUMBER)" -ForegroundColor Green;
Write-Host -Object "[Pipeline] -- CommitId: $($commitId)" -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';
}
if ($Env:BUILD_SOURCEBRANCH -like '*/tags/*' -and $Env:BUILD_SOURCEBRANCHNAME -like 'v2.*') {
$Build = $Env:BUILD_SOURCEBRANCHNAME.Substring(1);
}
$version = $Build;
# Handle channel
if ([String]::IsNullOrEmpty('Channel')) {
$Channel = 'preview';
}
$channelSuffix = '';
$channelDisplayName = 'PSRule';
$channelCommand = '';
switch ($Channel) {
'dev' { $channelSuffix = '-dev'; $channelDisplayName = 'PSRule (Dev)'; }
'stable' { $channelSuffix = ''; $channelDisplayName = 'PSRule'; $channelCommand = ':stable' }
default { $channelSuffix = ''; $channelDisplayName = 'PSRule'; }
}
Write-Host -Object "[Pipeline] -- Using channel: $Channel" -ForegroundColor Green;
Write-Host -Object "[Pipeline] -- Using channelSuffix: $channelSuffix" -ForegroundColor Green;
Write-Host -Object "[Pipeline] -- Using version: $version" -ForegroundColor Green;
$packageRoot = Join-Path -Path $OutputPath -ChildPath 'package';
$packageName = "code$channelSuffix";
$packagePath = Join-Path -Path $packageRoot -ChildPath "$packageName-$version.vsix";
Write-Host -Object "[Pipeline] -- packageRoot: $packageRoot" -ForegroundColor Green;
Write-Host -Object "[Pipeline] -- packageName: $packageName" -ForegroundColor Green;
Write-Host -Object "[Pipeline] -- packagePath: $packagePath" -ForegroundColor Green;
function Get-RepoRuleData {
[CmdletBinding()]
param (
[Parameter(Position = 0, Mandatory = $False)]
[String]$Path = $PWD
)
process {
GetPathInfo -Path $Path -Verbose:$VerbosePreference;
}
}
function GetPathInfo {
[CmdletBinding()]
param (
[Parameter(Mandatory = $True)]
[String]$Path
)
begin {
$items = New-Object -TypeName System.Collections.ArrayList;
}
process {
$Null = $items.Add((Get-Item -Path $Path));
$files = @(Get-ChildItem -Path $Path -File -Recurse -Include *.ps1,*.psm1,*.psd1,*.cs | Where-Object {
!($_.FullName -like "*.Designer.cs") -and
!($_.FullName -like "*/bin/*") -and
!($_.FullName -like "*/obj/*") -and
!($_.FullName -like "*\obj\*") -and
!($_.FullName -like "*\bin\*") -and
!($_.FullName -like "*\out\*") -and
!($_.FullName -like "*/out/*")
});
$Null = $items.AddRange($files);
}
end {
$items;
}
}
task BuildExtension {
Write-Host '> Building extension' -ForegroundColor Green;
exec { & npm run compile }
}
task PackageExtension {
Write-Host '> Packaging PSRule-vscode' -ForegroundColor Green;
if (!(Test-Path -Path $packageRoot)) {
$Null = New-Item -Path $packageRoot -ItemType Directory -Force;
}
exec { & npm run package$channelCommand }
}
# Synopsis: Install the extension in Visual Studio Code
task InstallExtension {
Write-Host '> Installing PSRule-vscode' -ForegroundColor Green;
exec { & code --install-extension $packagePath --force }
}
task VersionExtension {
# Set channel
$package = Get-Content ./package.json -Raw | ConvertFrom-Json;
if ($package.channel -ne $Channel) {
$package.channel = $Channel;
$package | ConvertTo-Json -Depth 99 | Set-Content ./package.json;
}
# Update channel name
$package = Get-Content ./package.json -Raw | ConvertFrom-Json;
if ($package.name -ne $packageName) {
$package.name = $packageName;
$package | ConvertTo-Json -Depth 99 | Set-Content ./package.json;
}
# Update channel flag
# $package = Get-Content ./package.json -Raw | ConvertFrom-Json;
# $previewFlag = $Channel -notin 'stable', 'preview';
# if ($package.preview -ne $previewFlag) {
# $package.preview = $previewFlag;
# $package | ConvertTo-Json -Depth 99 | Set-Content ./package.json;
# }
# Update channel display name
$package = Get-Content ./package.json -Raw | ConvertFrom-Json;
if ($package.displayName -ne $channelDisplayName) {
$package.displayName = $channelDisplayName;
$package | ConvertTo-Json -Depth 99 | Set-Content ./package.json;
}
if (![String]::IsNullOrEmpty($Build)) {
# Update extension version
if (![String]::IsNullOrEmpty($version)) {
Write-Verbose -Message "[VersionExtension] -- Updating extension version";
$package = Get-Content ./package.json -Raw | ConvertFrom-Json;
if ($package.version -ne $version) {
$package.version = $version;
$package | ConvertTo-Json -Depth 99 | Set-Content ./package.json;
}
}
}
}
# Synopsis: Install NuGet provider
task NuGet {
if ($Null -eq (Get-PackageProvider -Name NuGet -ErrorAction Ignore)) {
Install-PackageProvider -Name NuGet -Force -Scope CurrentUser;
}
}
# Synopsis: Install PSRule
task PSRule NuGet, {
if ($Null -eq (Get-InstalledModule -Name PSRule -MinimumVersion 2.9.0 -ErrorAction Ignore)) {
Install-Module -Name PSRule -Repository PSGallery -MinimumVersion 2.9.0 -Scope CurrentUser -Force;
}
Import-Module -Name PSRule -Verbose:$False;
}
# Synopsis: Run validation
task Rules PSRule, {
$assertParams = @{
Path = './.ps-rule/'
Style = $AssertStyle
OutputFormat = 'NUnit3'
ErrorAction = 'Stop'
As = 'Summary'
}
Assert-PSRule @assertParams -InputPath $PWD -Format File -OutputPath reports/ps-rule-file.xml;
}
# Synopsis: Remove temp files
task Clean {
Remove-Item -Path out,reports -Recurse -Force -ErrorAction Ignore;
}
# Synopsis: Restore NPM packages
task PackageRestore {
exec { & npm install --no-save }
}
task ReleaseExtension {
exec { & npm run publish$channelCommand -- --packagePath $packagePath --pat $ApiKey }
}
task Build Clean, PackageRestore, VersionExtension, PackageExtension
task Install Build, InstallExtension
task . Build
task Release VersionExtension, ReleaseExtension