-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpsakeFile.ps1
111 lines (89 loc) · 4.2 KB
/
psakeFile.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
# Based on the script from https://github.com/poshbotio/PoshBot
properties {
$projectRoot = $ENV:BHProjectPath
if (-not $projectRoot) {
$projectRoot = $PSScriptRoot
}
$sut = $env:BHModulePath
$tests = Join-Path -Path $projectRoot -ChildPath 'Tests'
$outputDir = Join-Path -Path $projectRoot -ChildPath 'out'
$outputModDir = Join-Path -Path $outputDir -ChildPath $env:BHProjectName
$manifest = Import-PowerShellDataFile -Path $env:BHPSModuleManifest
$outputModVerDir = Join-Path -Path $outputModDir -ChildPath $manifest.ModuleVersion
$psVersion = $PSVersionTable.PSVersion.Major
$pathSeperator = [IO.Path]::PathSeparator
}
task OutputDir {
$outputModVerDir
}
task default -depends Test
task Init {
"`nSTATUS: Testing with PowerShell $psVersion"
"Build System Details:"
Get-Item ENV:BH*
"`n"
} -description 'Initialize build environment'
task Test -Depends Init, Analyze, Pester -description 'Run test suite'
task Analyze -Depends Build {
$analysis = Invoke-ScriptAnalyzer -Path $outputModVerDir -Settings ./ScriptAnalyzerSettings.psd1 -Recurse -Verbose:$false
$errors = $analysis | Where-Object { $_.Severity -eq 'Error' }
$warnings = $analysis | Where-Object { $_.Severity -eq 'Warning' }
$informational = $analysis | Where-Object { $_.Severity -eq 'Information' }
if (($errors.Count -eq 0) -and ($warnings.Count -eq 0)) {
' PSScriptAnalyzer passed without errors or warnings'
}
if (@($errors).Count -gt 0) {
$errors | Format-Table -AutoSize
Write-Error -Message 'One or more Script Analyzer errors were found. Build cannot continue!'
}
if (@($warnings).Count -gt 0) {
$warnings | Format-Table -AutoSize
Write-Warning -Message 'One or more Script Analyzer warnings were found. These should be corrected.'
}
if (@($informational).Count -gt 0) {
$informational | Format-Table -AutoSize
Write-Warning -Message 'One or more Script Analyzer informationals were found. These could be corrected.'
}
} -description 'Run PSScriptAnalyzer'
task Pester -Depends Build {
Push-Location
Set-Location -PassThru $outputModDir
if (-not $ENV:BHProjectPath) {
Set-BuildEnvironment -Path $PSScriptRoot\..
}
$origModulePath = $env:PSModulePath
if ( $env:PSModulePath.split($pathSeperator) -notcontains $outputDir ) {
$env:PSModulePath = ($outputDir + $pathSeperator + $origModulePath)
}
Remove-Module $ENV:BHProjectName -ErrorAction SilentlyContinue -Verbose:$false
Import-Module -Name $sut -Force -Verbose:$false
$testResultsXml = Join-Path -Path $outputDir -ChildPath 'testResults.xml'
$codeCoverageXml = Join-Path -Path $outputDir -ChildPath 'jacoco.xml'
$sourceFiles = Get-ChildItem $sut -Recurse | Where-Object { $_.FullName -match 'ps1$' } | Select-Object -ExpandProperty FullName
$testResults = Invoke-Pester -Path $tests -PassThru -OutputFile $testResultsXml -OutputFormat NUnitXml -CodeCoverage $sourceFiles -CodeCoverageOutputFile $codeCoverageXml
if ($testResults.FailedCount -gt 0) {
$testResults | Format-List
Write-Error -Message 'One or more Pester tests failed. Build cannot continue!'
}
Pop-Location
$env:PSModulePath = $origModulePath
} -description 'Run Pester tests'
Task Publish -Depends Test {
" Publishing version [$($manifest.ModuleVersion)] to PSGallery..."
Publish-Module -Path $outputModVerDir -NuGetApiKey $env:PSGALLERY_API_KEY -Repository PSGallery
}
task Clean -depends Init {
Remove-Module -Name $env:BHProjectName -Force -ErrorAction SilentlyContinue
if (Test-Path -Path $outputModVerDir) {
Remove-Item -Path $outputModVerDir -Recurse -Force > $null
}
" Cleaned previous output directory [$outputModVerDir]"
} -description 'Cleans module output directory'
task Build -depends Clean {
if (-not (Test-Path -Path $outputDir)) {
New-Item -Path $outputDir -ItemType Directory > $null
}
New-Item -Path $outputModVerDir -ItemType Directory > $null
Copy-Item -Path (Join-Path -Path $sut -ChildPath *) -Destination $outputModVerDir -Recurse
" Created build module at [$outputModVerDir]"
} -description 'Builds module from source'