1
+ # PSake makes variables declared here available in other scriptblocks
2
+ Properties {
3
+ # Find the build folder based on build system
4
+ $ProjectRoot = Resolve-Path $ENV: BHProjectPath
5
+ if (-not $ProjectRoot )
6
+ {
7
+ $ProjectRoot = Resolve-Path " $PSScriptRoot \.."
8
+ }
9
+
10
+ $Timestamp = Get-date - uformat " %Y%m%d-%H%M%S"
11
+ $PSVersion = $PSVersionTable.PSVersion.Major
12
+ $TestFile = " TestResults_PS$PSVersion `_$TimeStamp .xml"
13
+ $lines = ' ----------------------------------------------------------------------'
14
+
15
+ $Verbose = @ {}
16
+ if ($ENV: BHCommitMessage -match " !verbose" )
17
+ {
18
+ $Verbose = @ {Verbose = $True }
19
+ }
20
+ }
21
+
22
+ Task Default - Depends Deploy
23
+
24
+ # Init some things
25
+ Task Init {
26
+ $lines
27
+ Set-Location $ProjectRoot
28
+ " Build System Details:"
29
+ Get-Item ENV:BH* | Format-List
30
+ " `n "
31
+ }
32
+
33
+
34
+ Task Analyze - Depends Init {
35
+ $saResults = Invoke-ScriptAnalyzer - Path $ENV: BHModulePath - Severity @ (' Error' , ' Warning' ) - Recurse - Verbose:$false
36
+ if ($saResults ) {
37
+ $saResults | Format-Table
38
+ # Write-Error -Message 'One or more Script Analyzer errors/warnings where found. Build cannot continue!'
39
+ }
40
+ }
41
+
42
+
43
+ Task UnitTests - Depends Init {
44
+ $lines
45
+ ' Running quick unit tests to fail early if there is an error'
46
+ $TestResults = Invoke-Pester - Path $ProjectRoot \Tests\* unit* - PassThru - Tag Build
47
+
48
+ if ($TestResults.FailedCount -gt 0 )
49
+ {
50
+ Write-Error " Failed '$ ( $TestResults.FailedCount ) ' tests, build failed"
51
+ }
52
+ " `n "
53
+ }
54
+
55
+ Task Test - Depends UnitTests {
56
+ $lines
57
+ " `n STATUS: Testing with PowerShell $PSVersion "
58
+
59
+ # Gather test results. Store them in a variable and file
60
+ $TestFilePath = Join-Path $ProjectRoot $TestFile
61
+ $CodeFiles = Get-ChildItem $ENV: BHModulePath - Recurse - Include " *.psm1" , " *.ps1"
62
+ $CodeCoverage = New-Object System.Collections.ArrayList
63
+ $CodeCoverage.AddRange ($CodeFiles.FullName )
64
+ $Script :TestResults = Invoke-Pester - Path $ProjectRoot \Tests - CodeCoverage $CodeCoverage - PassThru - OutputFormat NUnitXml - OutputFile $TestFilePath
65
+
66
+ # In Appveyor? Upload our tests! #Abstract this into a function?
67
+ If ($ENV: BHBuildSystem -eq ' AppVeyor' )
68
+ {
69
+ [xml ]$content = Get-Content $TestFilePath
70
+ $content .' test-results' .' test-suite' .type = " Powershell"
71
+ $content.Save ($TestFilePath )
72
+
73
+ " Uploading $ProjectRoot \$TestFile to AppVeyor"
74
+ " JobID: $env: APPVEYOR_JOB_ID "
75
+ (New-Object ' System.Net.WebClient' ).UploadFile(" https://ci.appveyor.com/api/testresults/nunit/$ ( $env: APPVEYOR_JOB_ID ) " , (Resolve-Path $TestFilePath ))
76
+ }
77
+
78
+ Remove-Item $TestFilePath - Force - ErrorAction SilentlyContinue
79
+
80
+ # Failed tests?
81
+ # Need to tell psake or it will proceed to the deployment. Danger!
82
+ if ($TestResults.FailedCount -gt 0 )
83
+ {
84
+ Write-Error " Failed '$ ( $TestResults.FailedCount ) ' tests, build failed"
85
+ }
86
+ " `n "
87
+ }
88
+
89
+ Task Build - Depends Test {
90
+ $lines
91
+
92
+ $functions = Get-ChildItem " $ENV: BHModulePath \Public\*.ps1" |
93
+ Where-Object { $_.name -notmatch ' Tests' } |
94
+ Select-Object - ExpandProperty basename
95
+
96
+ # Load the module, read the exported functions, update the psd1 FunctionsToExport
97
+ Set-ModuleFunctions - Name $env: BHPSModuleManifest - FunctionsToExport $functions
98
+
99
+ # Bump the module version
100
+ $version = [version ] (Step-Version (Get-Metadata - Path $env: BHPSModuleManifest ))
101
+ $galleryVersion = Get-NextPSGalleryVersion - Name $env: BHProjectName
102
+ if ($version -lt $galleryVersion )
103
+ {
104
+ $version = $galleryVersion
105
+ }
106
+ $Script :version = [version ]::New($version.Major , $version.Minor , $version.Build )
107
+ Write-Host " Using version: $version "
108
+
109
+ Update-Metadata - Path $env: BHPSModuleManifest - PropertyName ModuleVersion - Value $version
110
+
111
+ # Update Code Coverage
112
+ Function Update-CodeCoveragePercent {
113
+ param (
114
+ [int ]$CodeCoverage = 0 ,
115
+ [string ]$TextFilePath = " $Env: BHProjectPath \Readme.md"
116
+ )
117
+
118
+ $BadgeColor = Switch ($CodeCoverage ) {
119
+ 100 {" brightgreen" }
120
+ {95 .. 99 -contains $_ } {" green" }
121
+ {85 .. 94 -contains $_ } {" yellowgreengreen" }
122
+ {75 .. 84 -contains $_ } {" yellow" }
123
+ {65 .. 74 -contains $_ } {" orange" }
124
+ default {" red" }
125
+ }
126
+
127
+ $ReadmeContent = Get-Content $TextFilePath
128
+ $ReadmeContent = $ReadmeContent | ForEach-Object {$_ - replace " !\[Test Coverage\].+\)" , " " }
129
+ Set-Content - Path $TextFilePath - Value $ReadmeContent
130
+ }
131
+
132
+ $CoveragePercent = 100 - (($Script :TestResults.CodeCoverage.NumberOfCommandsMissed / $Script :TestResults.CodeCoverage.NumberOfCommandsAnalyzed )* 100 )
133
+ " Running Update-CodeCoveragePercent with percentage $CoveragePercent "
134
+ Update-CodeCoveragePercent - CodeCoverage $CoveragePercent
135
+ " `n "
136
+ }
137
+
138
+ Task MakePackage - Depends Build, Test {
139
+ $lines
140
+
141
+ function ZipFiles
142
+ {
143
+ param ( $zipfilename , $sourcedir )
144
+ Add-Type - Assembly System.IO.Compression.FileSystem
145
+ $compressionLevel = [System.IO.Compression.CompressionLevel ]::Optimal
146
+ [System.IO.Compression.ZipFile ]::CreateFromDirectory($sourcedir ,
147
+ $zipfilename , $compressionLevel , $true )
148
+ }
149
+
150
+ function New-MakePackage {
151
+ param (
152
+ [string ]$PackageName ,
153
+ [string ]$PackagePath ,
154
+ [string ]$ModuleName
155
+ )
156
+
157
+ $ZipFile = " $PackagePath \$PackageName "
158
+ Remove-Item $ZipFile - Force - ErrorAction SilentlyContinue | Out-Null
159
+ ZipFiles $ZipFile $ModuleName
160
+ }
161
+
162
+ # Update/Create the package
163
+ $PackageName = " $ ( $Env: BHProjectName ) -v$ ( $Script :version ) .zip"
164
+ " Creating package $PackageName "
165
+ New-MakePackage - PackageName $PackageName - PackagePath $ProjectRoot - ModuleName $ENV: BHModulePath
166
+
167
+ " `n "
168
+ }
169
+
170
+ Task Deploy - Depends Build, MakePackage {
171
+ $lines
172
+
173
+ # Gate deployment
174
+ if (
175
+ $ENV: BHBuildSystem -ne ' Unknown' -and
176
+ $ENV: BHBranchName -eq " master" -and
177
+ $ENV: BHCommitMessage -match ' !deploy'
178
+ )
179
+ {
180
+ $Params = @ {
181
+ Path = $ProjectRoot
182
+ Force = $true
183
+ }
184
+
185
+ Invoke-PSDeploy @Verbose @Params
186
+ }
187
+ else
188
+ {
189
+ " Skipping deployment: To deploy, ensure that...`n " +
190
+ " `t * You are in a known build system (Current: $ENV: BHBuildSystem )`n " +
191
+ " `t * You are committing to the master branch (Current: $ENV: BHBranchName ) `n " +
192
+ " `t * Your commit message includes !deploy (Current: $ENV: BHCommitMessage )"
193
+ }
194
+ }
0 commit comments