-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVimTabCompletion.Build.ps1
302 lines (259 loc) · 10.2 KB
/
VimTabCompletion.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
Param(
$VersionIncrement = 'Patch'
)
Task Default Build, Test, Distribute
Task Build CopyOutput, GetReleasedModuleInfo, BuildPSM1, BuildPSD1
function ReadPreviousRelease {
Param (
[Parameter(Mandatory)]
[string]
$Name,
[Parameter(Mandatory)]
[string]
$Repository,
[Parameter(Mandatory)]
[string]
$Path
)
$ModuleReader = {
Param (
[string]
$Name,
[string]
$Repository,
[string]
$Path
)
try {
Save-Module -Name $Name -Path $Path -Repository $Repository -ErrorAction Stop
Import-Module -Name "$Path\$Name" -PassThru -ErrorAction Stop
}
catch {
if ($_ -match "No match was found for the specified search criteria") {
@()
}
else {
$_
}
}
}
$parameters = @{
Name = $Name;
Repository = $Repository;
Path = $Path;
}
# Runspace is used to avoid importing old versions of the module in the current session
$PowerShellRunspace = [powershell]::Create()
$null = $PowerShellRunspace.AddScript($ModuleReader).AddParameters($parameters)
return $PowerShellRunspace.Invoke()
}
function GetPublicFunctionInterfaces {
Param (
[System.Management.Automation.FunctionInfo[]]
$FunctionList
)
$functionInterfaces = New-Object -TypeName System.Collections.ArrayList
foreach ($function in $FunctionList) {
foreach ($parameter in $function.Parameters.Keys) {
Write-Verbose "$($function.Name)"
Write-Verbose "$($function.Parameters[$parameter].Name)"
$toAdd = "{0}:{1}" -f $function.Name, $function.Parameters[$parameter].Name
$functionInterfaces.Add($toAdd)
foreach ($alias in $function.Parameters[$parameter].Aliases) {
Write-Verbose "$($function.Name)"
Write-Verbose "$($alias)"
$toAdd = "{0}:{1}" -f $function.Name, $alias
$functionInterfaces.Add($toAdd)
}
}
}
return $functionInterfaces
}
function PublishTestResults {
Param (
[string]
$Path
)
if ($Env:BHBuildSystem -eq 'Unknown')
{
Write-Warning "Build system unknown; skipping test results publishing."
return
}
Write-Output "Publishing test results data file..."
switch ($Env:BHBuildSystem)
{
'AppVeyor'
{
(New-Object 'System.Net.WebClient').UploadFile(
"https://ci.appveyor.com/api/testresults/nunit/$($env:APPVEYOR_JOB_ID)",
$Path)
}
Default
{
Write-Warning "Publish test result not implemented for build system '$($Env:BHBuildSystem)'."
}
}
}
Enter-Build {
$Script:publishToRepo = 'PSGallery'
if (!(Get-Item 'Env:\BH*')) {
Set-BuildEnvironment
Set-Item -Path 'Env:\PublishToRepo' -Value $Script:publishToRepo
}
$Script:ModuleName = $Env:BHProjectName
$Script:Source = Join-Path -Path $BuildRoot -ChildPath $Script:ModuleName
$Script:Output = Join-Path -Path $BuildRoot -ChildPath 'output'
$Script:Destination = Join-Path -Path $Script:Output -ChildPath $Script:ModuleName
$Script:ModulePath = Join-Path -Path $Script:Destination -ChildPath "$Script:ModuleName.psm1"
$Script:ManifestPath = Join-Path -Path $Script:Destination -ChildPath "$Script:ModuleName.psd1"
$Script:Imports = ('public', 'private')
$Script:TestFile = "$PSScriptRoot\output\TestResults_PS$PSVersion.xml"
$Global:TestThisModule = $Script:ManifestPath
}
Task Clean {
Remove-Item -Path $Output -Recurse -ErrorAction Ignore | Out-Null
}
Task CopyOutput {
Write-Output " Creating directory [$Script:Destination]"
New-Item -Type Directory -Path $Script:Destination -ErrorAction Ignore | Out-Null
Write-Output " Files and directories to be copied from source [$Script:Source]"
Get-ChildItem -Path $Script:Source -File | `
Where-Object -Property Name -NotMatch "$Script:ModuleName\.ps[md]1" | `
Copy-Item -Destination $Script:Destination -Force -PassThru | `
ForEach-Object {" Creating file [{0}]" -f $_.fullname.replace($PSScriptRoot, '')}
Get-ChildItem -Path $Script:Source -Directory | `
Copy-Item -Destination $Script:Destination -Recurse -Force -PassThru | `
ForEach-Object {" Creating directory (recursive) [{0}]" -f $_.fullname.replace($PSScriptRoot, '')}
}
Task GetReleasedModuleInfo {
$downloadPath = "$Script:Output\releasedModule"
if (!(Test-Path $downloadPath)) {
$null = New-Item -Path $downloadPath -ItemType Directory
}
$releasedModule = ReadPreviousRelease -Name $Script:ModuleName -Repository $Script:publishToRepo -Path $downloadPath
if (($releasedModule -ne $null) -and ($releasedModule.GetType() -eq [System.Management.Automation.ErrorRecord])) {
Write-Error $releasedModule
return
}
$moduleInfo = $null
if ($releasedModule -eq $null) {
$moduleInfo = [PSCustomObject] @{
Version = [Version]::New(0, 0, 1)
FunctionInterfaces = New-Object -TypeName System.Collections.ArrayList
}
}
else {
$moduleInfo = [PSCustomObject] @{
Version = $releasedModule.Version
FunctionInterfaces = GetPublicFunctionInterfaces -FunctionList $releasedModule.ExportedFunctions.Values
}
}
$moduleInfo | Export-Clixml -Path "$Script:Output\released-module-info.xml"
}
Task BuildPSM1 {
[System.Text.StringBuilder]$StringBuilder = [System.Text.StringBuilder]::new()
foreach ($folder in $Script:Imports)
{
[void]$StringBuilder.AppendLine("Write-Verbose `"Importing from [`$PSScriptRoot\$folder]`"")
if (Test-Path "$Script:Source\$folder")
{
$fileList = Get-ChildItem "$Script:Source\$folder" -Filter '*.ps1'
foreach ($file in $fileList)
{
$importName = "$folder\$($file.Name)"
Write-Output " Found $importName"
[void]$StringBuilder.AppendLine( ". `"`$PSScriptRoot\$importName`"")
}
}
}
[void]$StringBuilder.AppendLine("`$publicFunctions = (Get-ChildItem -Path `"`$PSScriptRoot\public`" -Filter '*.ps1').BaseName")
[void]$StringBuilder.AppendLine("Export-ModuleMember -Function `$publicFunctions")
Write-Output " Creating module [$Script:ModulePath]"
Set-Content -Path $Script:ModulePath -Value $stringbuilder.ToString()
}
Task BuildPSD1 {
Write-Output " Updating [$Script:ManifestPath]"
Copy-Item "$Script:Source\$ModuleName.psd1" -Destination $Script:ManifestPath
$moduleFunctions = Get-ChildItem -Path "$Script:Source\public" -Filter '*.ps1' | `
Select-Object -ExpandProperty BaseName
Set-ModuleFunctions -Name $Script:ManifestPath -FunctionsToExport $moduleFunctions
Set-ModuleAliases -Name $Script:ManifestPath
$releasedModuleInfo = Import-Clixml -Path "$Script:Output\released-module-info.xml"
Get-Module -Name $Script:ModuleName -All | Remove-Module -Force -ErrorAction 'Ignore'
$newFunctionList = (Import-Module -Name "$Script:ModulePath" -PassThru).ExportedFunctions.Values
Get-Module -Name $Script:ModuleName -All | Remove-Module -Force -ErrorAction 'Ignore'
$newFunctionInterfaces = GetPublicFunctionInterfaces -FunctionList $newFunctionList
$oldFunctionInterfaces = $releasedModuleInfo.FunctionInterfaces
Write-Output " Detecting new features"
foreach ($interface in $newFunctionInterfaces) {
if ($interface -notin $oldFunctionInterfaces) {
$VersionIncrement = 'Minor'
Write-Output " $interface"
}
}
Write-Output " Detecting lost features (breaking changes)"
foreach ($interface in $oldFunctionInterfaces) {
if ($interface -notin $newFunctionInterfaces) {
$VersionIncrement = 'Major'
Write-Output " $interface"
}
}
$version = [Version](Get-Metadata -Path $Script:ManifestPath -PropertyName "ModuleVersion")
# Don't bump major version if in pre-release
if ($version -lt ([Version]"1.0.0")) {
if ($VersionIncrement -eq 'Major') {
$VersionIncrement = 'Minor'
}
else {
$VersionIncrement = 'Patch'
}
}
$releasedVersion = $releasedModuleInfo.Version
if ($version -lt $releasedVersion) {
$version = $releasedVersion
}
if ($version -eq $releasedVersion) {
$version = [Version](Step-Version -Version $releasedVersion -By $VersionIncrement)
Write-Output " Stepping module from released version [$releasedVersion] to new version [$version] by [$VersionIncrement]"
Update-Metadata -Path $Script:ManifestPath -PropertyName 'ModuleVersion' -Value $version
}
else {
Write-Output " Using version from $Script:ModuleName.psd1: $version"
}
}
Task Test {
Get-Module -All -Name $Script:ModuleName | Remove-Module -Force -ErrorAction 'Ignore'
Import-Module -Name $Global:TestThisModule
$origConfirmPreference = $Global:ConfirmPreference
$Global:ConfirmPreference = 'None'
$pesterParams = @{
PassThru = $true;
Strict = $true;
OutputFormat = 'NUnitXml';
OutputFile = $Script:TestFile;
Path = "$PSScriptRoot\tests";
}
$testResults = Invoke-Pester @pesterParams
$Global:ConfirmPreference = $origConfirmPreference
PublishTestResults -Path $Script:TestFile
assert ($testResults.FailedCount -eq 0) "There was [$($testResults.FailedCount)] failed test(s)."
}
Task Distribute {
if (
$Env:BHBuildSystem -ne 'Unknown' -and
$Env:BHBranchName -eq 'Master' -and
$Env:BHCommitMessage -match '!deploy'
) {
$DeployParams = @{
Path = $BuildRoot;
Force = $true;
}
Invoke-PSDeploy @DeployParams
}
else {
Write-Output " Skipping deployment:"
Write-Output " Build system: $Env:BHBuildSystem"
Write-Output " Current branch (should be master): $Env:BHBranchName"
Write-Output " Commit message (should include '!deploy'): $Env:BHCommitMessage"
}
}