Format-Checklist rewrite (#28)

* reimplemented Format-Checklist to cover more scenarios

* bumped version number
format-checklist-fix
Chris Kuech 2019-09-05 21:01:33 -04:00 committed by GitHub
parent f0d9af1583
commit d7a5d6eb49
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 214 additions and 150 deletions

View File

@ -1,3 +1,3 @@
{
"powershell.scriptAnalysis.settingsPath": "PSScriptAnalyzerSettings.psd1"
"powershell.scriptAnalysis.settingsPath": "./PSScriptAnalyzerSettings.psd1"
}

View File

@ -4,7 +4,7 @@
RootModule = 'src\module.psm1'
# Version number of this module.
ModuleVersion = '2.2.3'
ModuleVersion = '2.2.4'
# Supported PSEditions
# CompatiblePSEditions = @()

View File

@ -2,8 +2,6 @@
$ErrorActionPreference = "Stop"
Import-Module "$PSScriptRoot\Requirements.psd1" -Force
$requirements = @(
@{
Name = "Resource 1"
@ -17,6 +15,11 @@ $requirements = @(
Test = { $mySystem -contains 2 }
Set = { $mySystem.Add(2) | Out-Null; Start-Sleep 1 }
},
@{
Name = "Checkpoint"
Describe = "Assert before continuing"
Test = { $mySystem }
},
@{
Name = "Resource 3"
Describe = "Resource 3 is present in the system"
@ -29,6 +32,11 @@ $requirements = @(
Test = { $mySystem -contains 4 }
Set = { throw "This should not have been reached!"; Start-Sleep 1 }
},
@{
Name = "Always set"
Describe = "Always run the set command"
Set = { Start-Sleep 1 }
},
@{
Name = "Resource 5"
Describe = "Resource 5 is present in the system"

View File

@ -1,9 +1,92 @@
using namespace System.Collections.Generic
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingWriteHost", "")]
Param()
$ErrorActionPreference = "Stop"
."$PSScriptRoot\types.ps1"
function writePending($timestamp, $description) {
$symbol = " "
$color = "Yellow"
$message = "$timestamp [ $symbol ] $description"
Write-Host $message -ForegroundColor $color -NoNewline
}
function writeSuccess($timestamp, $description, $clearString) {
$symbol = [char]8730
$color = "Green"
$message = "$timestamp [ $symbol ] $description"
Write-Host "`r$clearString" -NoNewline
Write-Host "`r$message" -ForegroundColor $color
}
function writeFail($timestamp, $description, $clearString) {
$symbol = "X"
$color = "Red"
$message = "$timestamp [ $symbol ] $description"
Write-Host "`r$clearString" -NoNewline
Write-Host "`n$message`n" -ForegroundColor $color
exit -1
}
$fsm = @{
"Test Test Start $false" = {
writePending @args
@{
"Test Test Stop $true" = {
writeSuccess @args
$fsm
}
"Test Test Stop $false" = {
writeFail @args
}
}
}
"Set Set Start $false" = {
writePending @args
@{
"Set Set Stop $false" = {
writeSuccess @args
$fsm
}
}
}
"TestSet Test Start $false" = {
writePending @args
@{
"TestSet Test Stop $true" = {
writeSuccess @args
$fsm
}
"TestSet Test Stop $false" = {
@{
"TestSet Set Start $false" = {
@{
"TestSet Set Stop $false" = {
@{
"TestSet Validate Start $false" = {
@{
"TestSet Validate Stop $true" = {
writeSuccess @args
$fsm
}
"TestSet Validate Stop $false" = {
writeFail @args
}
}
}
}
}
}
}
}
}
}
}
}
<#
.SYNOPSIS
Formats Requirement log events as a live-updating checklist
@ -11,7 +94,6 @@ $ErrorActionPreference = "Stop"
Uses Write-Host
#>
function Format-Checklist {
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingWriteHost", "")]
[CmdletBinding()]
Param(
# Logged Requirement lifecycle events
@ -21,62 +103,36 @@ function Format-Checklist {
)
begin {
$lastDescription = ""
$previousRequirement = $null
$nextFsm = $fsm
}
process {
$timestamp = Get-Date -Date $_.Date -Format 'hh:mm:ss'
$description = $_.Requirement.Describe
$method, $state, $result = $_.Method, $_.State, $_.Result
switch ($method) {
"Test" {
switch ($state) {
"Start" {
$symbol = " "
$color = "Yellow"
$message = "$timestamp [ $symbol ] $description"
Write-Host $message -ForegroundColor $color -NoNewline
$lastDescription = $description
}
"Stop" {
switch ($result) {
$true {
$symbol = [char]8730
$color = "Green"
$message = "$timestamp [ $symbol ] $description"
Write-Host "`r$(' ' * $lastDescription.Length)" -NoNewline
Write-Host "`r$message" -ForegroundColor $color
$lastDescription = $description
}
}
}
}
}
"Validate" {
switch ($state) {
"Stop" {
switch ($result) {
$true {
$symbol = [char]8730
$color = "Green"
$message = "$timestamp [ $symbol ] $description"
Write-Host "`r$(' ' * $lastDescription.Length)" -NoNewline
Write-Host "`r$message" -ForegroundColor $color
$lastDescription = $description
}
$false {
$symbol = "X"
$color = "Red"
$message = "$timestamp [ $symbol ] $description"
Write-Host "`n$message`n" -ForegroundColor $color
$lastDescription = $description
exit -1
}
}
}
}
}
$requirement = $_.Requirement
# build state vector
$requirementType = ("Test", "Set" | ? { $requirement.$_ }) -join ""
$method = $_.Method
$lifecycleState = $_.State
$successResult = [bool]$_.Result
$stateVector = "$requirementType $method $lifecycleState $successResult"
# build transition arguments
$timestamp = Get-Date -Date $_.Date -Format "hh:mm:ss"
$description = $requirement.Describe
$clearString = ' ' * "??:??:?? [ ? ] $($previousRequirement.Describe)".Length
$transitionArgs = @($timestamp, $description, $clearString)
# transition FSM
if (-not $nextFsm[$stateVector]) {
throw @"
Format-Checklist has reached an unexpected state '$stateVector'.
If you are piping the output of Invoke-Requirement directly to this
cmdlet, then this is probably a bug in Format-Checklist.
"@
}
$nextFsm = &$nextFsm[$stateVector] @transitionArgs
$previousRequirement = $requirement
}
}