-
Notifications
You must be signed in to change notification settings - Fork 31
/
module.build.ps1
174 lines (143 loc) · 6.27 KB
/
module.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
$script:ModuleName = 'RestPS'
$script:Source = Join-Path $BuildRoot $ModuleName
$script:Output = Join-Path $BuildRoot output
$script:Destination = Join-Path $Output $ModuleName
$script:ModulePath = "$Destination\$ModuleName.psm1"
$script:ManifestPath = "$Destination\$ModuleName.psd1"
$script:Imports = ( 'private', 'public', 'classes' )
$script:TestFile = "$PSScriptRoot\output\TestResults_PS$PSVersion`_$TimeStamp.xml"
# Importing all build settings into the current scope for coveralls integration
. ".\$ModuleName.BuildSettings.ps1"
Task Default Build, Pester, UpdateSource, Publish
Task Build CopyToOutput, BuildPSM1, BuildPSD1
Task Pester Build, ImportModule, UnitTests, FullTests, Publish_Unit_Tests_Coverage
Task Clean {
$null = Remove-Item $Output -Recurse -ErrorAction Ignore
$null = New-Item -Type Directory -Path $Destination
}
Task UnitTests {
$TestResults = Invoke-Pester -Path Tests\*\* -CodeCoverage $ModuleName\*\* -PassThru -Tag Build -ExcludeTag Slow
if ($TestResults.FailedCount -gt 0)
{
Write-Error "Failed [$($TestResults.FailedCount)] Pester tests"
}
}
Task Publish_Unit_Tests_Coverage {
$TestResults = Invoke-Pester -Path Tests\*\* -CodeCoverage $ModuleName\*\* -PassThru -Tag Build -ExcludeTag Slow
#$Coverage = Format-Coverage -PesterResults $TestResults -CoverallsApiToken $ENV:Coveralls_Key -BranchName $ENV:APPVEYOR_REPO_BRANCH
#Publish-Coverage -Coverage $Coverage
}
Task FullTests {
$TestResults = Invoke-Pester -Path Tests -PassThru -OutputFormat NUnitXml -OutputFile $testFile -Tag Build
if ($TestResults.FailedCount -gt 0)
{
Write-Error "Failed [$($TestResults.FailedCount)] Pester tests"
}
}
Task Specification {
$TestResults = Invoke-Gherkin $PSScriptRoot\Spec -PassThru
if ($TestResults.FailedCount -gt 0)
{
Write-Error "[$($TestResults.FailedCount)] specification are incomplete"
}
}
Task CopyToOutput {
Write-Output " Create Directory [$Destination]"
$null = New-Item -Type Directory -Path $Destination -ErrorAction Ignore
Get-ChildItem $source -File |
where name -NotMatch "$ModuleName\.ps[dm]1" |
Copy-Item -Destination $Destination -Force -PassThru |
ForEach-Object { " Create [.{0}]" -f $_.fullname.replace($PSScriptRoot, '') }
Get-ChildItem $source -Directory |
where name -NotIn $imports |
Copy-Item -Destination $Destination -Recurse -Force -PassThru |
ForEach-Object { " Create [.{0}]" -f $_.fullname.replace($PSScriptRoot, '') }
}
Task BuildPSM1 -Inputs (Get-Item "$source\*\*.ps1") -Outputs $ModulePath {
[System.Text.StringBuilder]$stringbuilder = [System.Text.StringBuilder]::new()
foreach ($folder in $imports )
{
[void]$stringbuilder.AppendLine( "Write-Verbose 'Importing from [$Source\$folder]'" )
if (Test-Path "$source\$folder")
{
$fileList = Get-ChildItem "$source\$folder\*.ps1" | Where Name -NotLike '*.Tests.ps1'
foreach ($file in $fileList)
{
$shortName = $file.fullname.replace($PSScriptRoot, '')
Write-Output " Importing [.$shortName]"
[void]$stringbuilder.AppendLine( "# .$shortName" )
[void]$stringbuilder.AppendLine( [System.IO.File]::ReadAllText($file.fullname) )
}
}
}
Write-Output " Creating module [$ModulePath]"
Set-Content -Path $ModulePath -Value $stringbuilder.ToString()
}
Task NextPSGalleryVersion -if (-Not ( Test-Path "$output\version.xml" ) ) -Before BuildPSD1 {
#$galleryVersion = Get-NextPSGalleryVersion -Name $ModuleName
$galleryVersion = Get-NextNugetPackageVersion -Name $ModuleName
$galleryVersion | Export-Clixml -Path "$output\version.xml"
}
Task BuildPSD1 -inputs (Get-ChildItem $Source -Recurse -File) -Outputs $ManifestPath {
Write-Output " Update [$ManifestPath]"
Copy-Item "$source\$ModuleName.psd1" -Destination $ManifestPath
$bumpVersionType = 'Patch'
$functions = Get-ChildItem "$ModuleName\Public\*.ps1" | Where-Object { $_.name -notmatch 'Tests' } | Select-Object -ExpandProperty basename
$oldFunctions = (Get-Metadata -Path $manifestPath -PropertyName 'FunctionsToExport')
$functions | Where { $_ -notin $oldFunctions } | % { $bumpVersionType = 'Minor' }
$oldFunctions | Where { $_ -notin $Functions } | % { $bumpVersionType = 'Major' }
Set-ModuleFunctions -Name $ManifestPath -FunctionsToExport $functions
# Bump the module version
$version = [version] (Get-Metadata -Path $manifestPath -PropertyName 'ModuleVersion')
$galleryVersion = Import-Clixml -Path "$output\version.xml"
if ( $version -lt $galleryVersion )
{
$version = $galleryVersion
}
Write-Output " Stepping [$bumpVersionType] version [$version]"
$version = [version] (Step-Version $version -Type $bumpVersionType)
Write-Output " Using version: $version"
Update-Metadata -Path $ManifestPath -PropertyName ModuleVersion -Value $version
}
Task UpdateSource {
Copy-Item $ManifestPath -Destination "$source\$ModuleName.psd1"
}
Task ImportModule {
if ( -Not ( Test-Path $ManifestPath ) )
{
Write-Output " Module [$ModuleName] is not built, cannot find [$ManifestPath]"
Write-Error "Could not find module manifest [$ManifestPath]. You may need to build the module first"
}
else
{
if (Get-Module $ModuleName)
{
Write-Output " Unloading Module [$ModuleName] from previous import"
Remove-Module $ModuleName
}
Write-Output " Importing Module [$ModuleName] from [$ManifestPath]"
Import-Module $ManifestPath -Force
}
}
Task Publish {
# Gate deployment
if (
$ENV:BHBuildSystem -ne 'Unknown' -and
$ENV:BHBranchName -eq "master" -and
$ENV:BHCommitMessage -match '!deploy'
)
{
$Params = @{
Path = $BuildRoot
Force = $true
}
Invoke-PSDeploy @Verbose @Params
}
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 !deploy (Current: $ENV:BHCommitMessage)"
}
}