Skip to content
This repository was archived by the owner on Jun 13, 2024. It is now read-only.

Commit e44ea13

Browse files
committed
Add psake tasks
1 parent 75678b0 commit e44ea13

File tree

4 files changed

+204
-0
lines changed

4 files changed

+204
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
out/*

.vscode/tasks.json

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
// See https://go.microsoft.com/fwlink/?LinkId=733558
3+
// for the documentation about the tasks.json format
4+
"version": "0.1.0",
5+
"showOutput": "always",
6+
7+
// Start PowerShell
8+
"windows": {
9+
"command": "${env:windir}\\sysnative\\WindowsPowerShell\\v1.0\\powershell.exe",
10+
"args": [ "-NoProfile", "-ExecutionPolicy", "Bypass" ]
11+
},
12+
"linux": {
13+
"command": "/usr/bin/powershell",
14+
"args": [ "-NoProfile" ]
15+
},
16+
"osx": {
17+
"command": "/usr/local/bin/powershell",
18+
"args": [ "-NoProfile" ]
19+
},
20+
21+
"tasks": [
22+
{
23+
"taskName": "Test",
24+
"suppressTaskName": true,
25+
"showOutput": "always",
26+
"isTestCommand": true,
27+
"args": [
28+
"./build.ps1 -Task Test"
29+
]
30+
},
31+
{
32+
"taskName": "Lint",
33+
"suppressTaskName": true,
34+
"showOutput": "always",
35+
"isTestCommand": false,
36+
"args":[
37+
"./build.ps1 -Task Analyze"
38+
]
39+
}
40+
]
41+
}

build.ps1

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
2+
[cmdletbinding(DefaultParameterSetName = 'task')]
3+
param(
4+
[parameter(ParameterSetName = 'task', Position = 0)]
5+
[string[]]$Task = 'default',
6+
7+
[parameter(ParameterSetName = 'help')]
8+
[switch]$Help,
9+
10+
[switch]$UpdateModules
11+
)
12+
13+
function Resolve-Module {
14+
[Cmdletbinding()]
15+
param (
16+
[Parameter(Mandatory, ValueFromPipeline)]
17+
[string[]]$Name,
18+
19+
[switch]$UpdateModules
20+
)
21+
22+
begin {
23+
Get-PackageProvider -Name Nuget -ForceBootstrap | Out-Null
24+
Set-PSRepository -Name PSGallery -InstallationPolicy Trusted
25+
26+
$PSDefaultParameterValues = @{
27+
'*-Module:Verbose' = $false
28+
'Install-Module:ErrorAction' = 'Stop'
29+
'Install-Module:Force' = $true
30+
'Install-Module:Scope' = 'CurrentUser'
31+
'Install-Module:Verbose' = $false
32+
'Install-Module:AllowClobber' = $true
33+
'Import-Module:ErrorAction' = 'Stop'
34+
'Import-Module:Verbose' = $false
35+
'Import-Module:Force' = $true
36+
}
37+
}
38+
39+
process {
40+
foreach ($moduleName in $Name) {
41+
$versionToImport = ''
42+
43+
Write-Verbose -Message "Resolving Module [$($moduleName)]"
44+
if ($Module = Get-Module -Name $moduleName -ListAvailable -Verbose:$false) {
45+
# Determine latest version on PSGallery and warn us if we're out of date
46+
$latestLocalVersion = ($Module | Measure-Object -Property Version -Maximum).Maximum
47+
$latestGalleryVersion = (Find-Module -Name $moduleName -Repository PSGallery |
48+
Measure-Object -Property Version -Maximum).Maximum
49+
50+
# Out we out of date?
51+
if ($latestLocalVersion -lt $latestGalleryVersion) {
52+
if ($UpdateModules) {
53+
Write-Verbose -Message "$($moduleName) installed version [$($latestLocalVersion.ToString())] is outdated. Installing gallery version [$($latestGalleryVersion.ToString())]"
54+
if ($UpdateModules) {
55+
Install-Module -Name $moduleName -RequiredVersion $latestGalleryVersion
56+
$versionToImport = $latestGalleryVersion
57+
}
58+
} else {
59+
Write-Warning "$($moduleName) is out of date. Latest version on PSGallery is [$latestGalleryVersion]. To update, use the -UpdateModules switch."
60+
}
61+
} else {
62+
$versionToImport = $latestLocalVersion
63+
}
64+
} else {
65+
Write-Verbose -Message "[$($moduleName)] missing. Installing..."
66+
Install-Module -Name $moduleName -Repository PSGallery
67+
$versionToImport = (Get-Module -Name $moduleName -ListAvailable | Measure-Object -Property Version -Maximum).Maximum
68+
}
69+
70+
Write-Verbose -Message "$($moduleName) installed. Importing..."
71+
if (-not [string]::IsNullOrEmpty($versionToImport)) {
72+
Import-module -Name $moduleName -RequiredVersion $versionToImport
73+
} else {
74+
Import-module -Name $moduleName
75+
}
76+
}
77+
}
78+
}
79+
80+
'BuildHelpers', 'psake' | Resolve-Module -UpdateModules:($PSBoundParameters.ContainsKey('UpdateModules'))
81+
82+
if ($PSBoundParameters.ContainsKey('help')) {
83+
Get-PSakeScriptTasks -buildFile "$PSScriptRoot\psake.ps1" |
84+
Sort-Object -Property Name |
85+
Format-Table -Property Name, Description, Alias, DependsOn
86+
} else {
87+
Set-BuildEnvironment -Force
88+
89+
Invoke-psake -buildFile "$PSScriptRoot\psake.ps1" -taskList $Task -nologo -Verbose:($VerbosePreference -eq 'Continue')
90+
exit ( [int]( -not $psake.build_success ) )
91+
}

psake.ps1

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
2+
properties {
3+
$projectRoot = $ENV:BHProjectPath
4+
if(-not $projectRoot) {
5+
$projectRoot = $PSScriptRoot
6+
}
7+
8+
$sut = $env:BHModulePath
9+
$tests = Join-Path -Path $projectRoot -ChildPath 'Tests'
10+
$outputDir = Join-Path -Path $projectRoot -ChildPath 'out'
11+
$manifest = Import-PowerShellDataFile -Path $env:BHPSModuleManifest
12+
}
13+
14+
task default -depends Test
15+
16+
task Init {
17+
"`nSTATUS: Testing with PowerShell $($PSVersionTable.PSVersion.Major)"
18+
'Build System Details:'
19+
Get-Item ENV:BH*
20+
"`n"
21+
22+
'Pester', 'PSScriptAnalyzer' | Foreach-Object {
23+
if (-not (Get-Module -Name $_ -ListAvailable -Verbose:$false -ErrorAction SilentlyContinue)) {
24+
Install-Module -Name $_ -Repository PSGallery -Scope CurrentUser -AllowClobber -Confirm:$false -ErrorAction Stop
25+
}
26+
Import-Module -Name $_ -Verbose:$false -Force -ErrorAction Stop
27+
}
28+
} -description 'Initialize build environment'
29+
30+
task Test -Depends Init, Analyze, Pester -description 'Run test suite'
31+
32+
task Analyze -Depends Init {
33+
$analysis = Invoke-ScriptAnalyzer -Path $env:BHModulePath -Verbose:$false
34+
$errors = $analysis | Where-Object {$_.Severity -eq 'Error'}
35+
$warnings = $analysis | Where-Object {$_.Severity -eq 'Warning'}
36+
37+
if (($errors.Count -eq 0) -and ($warnings.Count -eq 0)) {
38+
' PSScriptAnalyzer passed without errors or warnings'
39+
}
40+
41+
if (@($errors).Count -gt 0) {
42+
Write-Error -Message 'One or more Script Analyzer errors were found. Build cannot continue!'
43+
$errors | Format-Table
44+
}
45+
46+
if (@($warnings).Count -gt 0) {
47+
Write-Warning -Message 'One or more Script Analyzer warnings were found. These should be corrected.'
48+
$warnings | Format-Table
49+
}
50+
} -description 'Run PSScriptAnalyzer'
51+
52+
task Pester-Meta -depends Init {
53+
$testResultsXml = Join-Path -Path $outputDir -ChildPath 'testResults_meta.xml'
54+
$testResults = Invoke-Pester -Path (Join-Path -Path $tests -ChildPath 'Meta' ) -PassThru -OutputFile $testResultsXml -OutputFormat NUnitXml
55+
if ($testResults.FailedCount -gt 0) {
56+
$testResults | Format-List
57+
Write-Error -Message 'One or more Pester meta tests failed. Build cannot continue!'
58+
}
59+
} -description 'Run Pester meta tests'
60+
61+
task Pester-Unit -depends Init {
62+
$testResultsXml = Join-Path -Path $outputDir -ChildPath 'testResults_unit.xml'
63+
$testResults = Invoke-Pester -Path (Join-Path -Path $tests -ChildPath 'Unit' ) -PassThru -OutputFile $testResultsXml -OutputFormat NUnitXml
64+
if ($testResults.FailedCount -gt 0) {
65+
$testResults | Format-List
66+
Write-Error -Message 'One or more Pester unit tests failed. Build cannot continue!'
67+
}
68+
} -description 'Run Pester unit tests'
69+
70+
task Pester -Depends Pester-Meta, Pester-Unit {
71+
} -description 'Run all Pester tests'

0 commit comments

Comments
 (0)