-
Notifications
You must be signed in to change notification settings - Fork 70
/
Invoke-CommandAs.build.ps1
129 lines (101 loc) · 4.5 KB
/
Invoke-CommandAs.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
<#
.Synopsis
Build script (https://github.com/nightroman/Invoke-Build)
#>
param ($Configuration = 'Development')
#region Set-BuildEnvironment
Try { Set-BuildEnvironment -ErrorAction SilentlyContinue -Force } Catch { }
#endregion
#region use the most strict mode
Set-StrictMode -Version Latest
#endregion
#region Task to run all Pester tests in folder .\tests
task Test {
$OutputPath = New-Item -Path '.\TestResults' -ItemType Directory -Force -Verbose
$PesterParams = @{
Script = '.\Tests'
OutputFile = "${OutputPath}\TestResults.xml"
CodeCoverage = 'Invoke-CommandAs\*\*.ps1'
CodeCoverageOutputFile = "${OutputPath}\CodeCoverage.xml"
CodeCoverageOutputFileFormat = 'JaCoCo'
}
$Result = Invoke-Pester @PesterParams -PassThru
if ($Result.FailedCount -gt 0) {
throw 'Pester tests failed'
}
}
#endregion
#region Task to create a single Script file
task CreateScriptFile {
$ScriptFile = New-Item "${env:BHProjectPath}\Scripts\Invoke-CommandAs.ps1" -ItemType File -Force
$ModuleManifest = Test-ModuleManifest -Path $env:BHPSModuleManifest
[System.Version]$ManifestVersion = $ModuleManifest.Version
'#####################################################################' | Set-Content -Path $ScriptFile
'# Name : {0}' -f $ModuleManifest.Name | Add-Content -Path $ScriptFile
'# Version : {0}' -f $ManifestVersion | Add-Content -Path $ScriptFile
'# Description : {0}' -f $ModuleManifest.Description | Add-Content -Path $ScriptFile
'# ProjectUri : {0}' -f $ModuleManifest.ProjectUri | Add-Content -Path $ScriptFile
'# Author : {0}' -f $ModuleManifest.Author | Add-Content -Path $ScriptFile
'#####################################################################' | Add-Content -Path $ScriptFile
'' | Add-Content -Path $ScriptFile
Get-ChildItem "${env:BHModulePath}\*\*.ps1" -Recurse | Get-Content | Add-Content -Path $ScriptFile
}
#endregion
#region Task to update the Module Manifest file with info from the Changelog in Readme.
task UpdateManifest {
# Import PlatyPS. Needed for parsing README for Change Log versions
#Import-Module -Name PlatyPS
$ModuleManifest = Test-ModuleManifest -Path $env:BHPSModuleManifest
[System.Version]$ManifestVersion = $ModuleManifest.Version
Write-Output -InputObject ('Manifest Version : {0}' -f $ManifestVersion)
Try {
$PSGalleryModule = Find-Module -Name $env:BHProjectName -Repository PSGallery
[System.Version]$PSGalleryVersion = $PSGalleryModule.Version
} Catch {
[System.Version]$PSGalleryVersion = '0.0.0'
}
Write-Output -InputObject ('PSGallery Version : {0}' -f $PSGalleryVersion)
If ($PSGalleryVersion -ge $ManifestVersion) {
[System.Version]$Version = New-Object -TypeName System.Version -ArgumentList ($PSGalleryVersion.Major, $PSGalleryVersion.Minor, ($PSGalleryVersion.Build + 1))
Write-Output -InputObject ('Updated Version : {0}' -f $Version)
Update-ModuleManifest -ModuleVersion $Version -Path .\Invoke-CommandAs\Invoke-CommandAs.psd1 # -ReleaseNotes $ReleaseNotes
}
}
#endregion
#region Task to Publish Module to PowerShell Gallery
task PublishModule -If ($Configuration -eq 'Production') {
Try {
# Publish to gallery with a few restrictions
if(
$env:BHModulePath -and
$env:BHBuildSystem -ne 'Unknown' -and
$env:BHBranchName -eq "master" -and
$env:BHCommitMessage -match '!publish'
)
{
# Build a splat containing the required details and make sure to Stop for errors which will trigger the catch
$params = @{
Path = "${env:BHModulePath}"
NuGetApiKey = "${env:NUGETAPIKEY}"
ErrorAction = 'Stop'
}
Publish-Module @params
Write-Output -InputObject ('Invoke-CommandAs PowerShell Module version published to the PowerShell Gallery')
}
else
{
"Skipping deployment: To deploy, ensure that...`n" +
"`t* You are in a known build system (Current: $ENV:BHBuildSystem)`n" +
"`t* You are committing to the master branch (Current: $ENV:BHBranchName) `n" +
"`t* Your commit message includes !publish (Current: $ENV:BHCommitMessage)" |
Write-Host
}
}
Catch {
throw $_
}
}
#endregion
#region Default Task. Runs Test, UpdateManifest, PublishModule Tasks
task . Test, UpdateManifest, PublishModule
#endregion