-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpsakeBuild.ps1
159 lines (132 loc) · 6.03 KB
/
psakeBuild.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
properties {
# Name of the module
$moduleName = 'PoshHubot'
# Path for unit tests
$unitTestsPath = "$($PSScriptRoot)\Tests\unit"
# Artifact Root Path
$artifactRootPath = "$($PSScriptRoot)\Artifact"
# Artifact Module Path
$artifactModulePath = "$($artifactRootPath)\$($moduleName)"
# Path for manifests file
$manifestPath = Join-Path -Path $artifactModulePath -ChildPath "$($moduleName).psd1"
# List of the PowerShell scripts to test
$filesToTest = Get-ChildItem *.psm1,*.psd1,*.ps1 -Recurse -Exclude *build.ps1,*.pester.ps1,*Tests.ps1
# Module version to replace on build
$originalModuleVersion = '1.0.2'
# Files To exclude from packaging
$filesToExclude = @('.gitignore', 'build.ps1', 'psakeBuild.ps1', '*.yml', 'PesterResults*.xml', 'TestResults*.xml')
# Directories to exclude from packaging
$directoriesToExclude = @('Artifact', '.kitchen', '.vagrant', '.git')
# PSGallery API Key
$PSGalleryAPIKey = $env:PSGalleryKey
}
task default -depends Analyze, Test, BuildArtifact, UploadArtifact
task TestProperties {
Assert ($build_version -ne $null) "build_version should not be null"
}
task Analyze {
ForEach ($testPath in $filesToTest)
{
try
{
Write-Output "Running ScriptAnalyzer on $($testPath)"
if ($env:APPVEYOR)
{
Add-AppveyorTest -Name "PsScriptAnalyzer" -Outcome Running
$timer = [System.Diagnostics.Stopwatch]::StartNew()
}
$saResults = Invoke-ScriptAnalyzer -Path $testPath -Verbose:$false
if ($saResults) {
$saResults | Format-Table
$saResultsString = $saResults | Out-String
if ($saResults.Severity -contains 'Error' -or $saResults.Severity -contains 'Warning')
{
if ($env:APPVEYOR)
{
Add-AppveyorMessage -Message "PSScriptAnalyzer output contained one or more result(s) with 'Error or Warning' severity.`
Check the 'Tests' tab of this build for more details." -Category Error
Update-AppveyorTest -Name "PsScriptAnalyzer" -Outcome Failed -ErrorMessage $saResultsString
}
Write-Error -Message "One or more Script Analyzer errors/warnings where found in $($testPath). Build cannot continue!"
}
else
{
Write-Output "All ScriptAnalyzer tests passed"
if ($env:APPVEYOR)
{
Update-AppveyorTest -Name "PsScriptAnalyzer" -Outcome Passed -StdOut $saResultsString -Duration $timer.ElapsedMilliseconds
}
}
}
}
catch
{
$ErrorMessage = $_.Exception.Message
$FailedItem = $_.Exception.ItemName
Write-Output $ErrorMessage
Write-Output $FailedItem
Write-Error "The build failed when working with $($testPath)."
}
}
}
task Test {
$testResults = .\Tests\appveyor.pester.ps1 -Test -TestPath $unitTestsPath
# $testResults = Invoke-Pester -Path $unitTestsPath -PassThru
if ($testResults.FailedCount -gt 0) {
$testResults | Format-List
Write-Error -Message 'One or more Pester unit tests failed. Build cannot continue!'
}
}
task BuildArtifact -depends Analyze, Test {
# join the arrys for using within robocopy
$filesToExclude = $filesToExclude -join ' '
$directoriesToExclude = $directoriesToExclude -join ' '
New-Item -Path $artifactRootPath -ItemType Directory -Force
# prepare artifacts
Start-Process -FilePath 'robocopy.exe' -ArgumentList "`"$($PSScriptRoot)`" `"$($artifactModulePath)`" /S /R:1 /W:1 /XD $directoriesToExclude /XF $filesToExclude" -Wait -NoNewWindow
# Only want proper releases when tagged
if ($env:APPVEYOR_REPO_TAG_NAME -and ($env:APPVEYOR_REPO_BRANCH -eq 'master'))
{
Write-Output "Changing module version to Github tag version $($env:APPVEYOR_REPO_TAG_NAME)"
(Get-Content $manifestPath -Raw).Replace($originalModuleVersion, $env:APPVEYOR_REPO_TAG_NAME) | Out-File $manifestPath
Compress-Archive -Path $artifactModulePath -DestinationPath "$($artifactModulePath)\$($env:APPVEYOR_REPO_TAG_NAME).zip" -Force
}
# Artifiacts are built every time but not published unless tagged. This is for local testing
else
{
Write-Output "Not a tagged release, only building a CI Artifact"
(Get-Content $manifestPath -Raw).Replace($originalModuleVersion, $build_version) | Out-File $manifestPath
Compress-Archive -Path $artifactModulePath -DestinationPath "$($artifactModulePath)-CI-$($build_version).zip" -Force
}
}
task UploadArtifact -depends Analyze, Test, BuildArtifact {
# Get Zips
$zips = Get-ChildItem -Path "$($artifactRootPath)\*.zip"
# Upload Zipped Artifacts
ForEach ($zip in $zips)
{
Write-Output "Found zip: $($zip.FullName)"
# only upload artifacts on master branch
if ($env:APPVEYOR -and ($env:APPVEYOR_REPO_BRANCH -eq 'master'))
{
Write-Output "Pushing $($zip.Fullname) to AppveyorArtifacts"
Push-AppveyorArtifact $zip.FullName -FileName $zip.Name
}
else
{
Write-Output "If this was Appveyor AND master branch, I would have pushed $($zip.Fullname) to AppveyorArtifacts"
}
}
# Publish Module
# Upload artifiact only on tagging
if ($env:APPVEYOR_REPO_TAG_NAME -and ($env:APPVEYOR_REPO_BRANCH -eq 'master'))
{
Write-Output "Publishing Module Located In $($artifactModulePath) to the PSGallery"
Publish-Module -Path $artifactModulePath -NuGetApiKey $PSGalleryAPIKey
}
else
{
Write-Output "If this was Appveyor AND master branch, $($artifactModulePath) would be published to PSGallery."
Get-ChildItem $artifactModulePath | Remove-Item -Force -Recurse
}
}