src/formatters.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 |
using namespace System.Collections.Generic [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingWriteHost", "")] Param() $ErrorActionPreference = "Stop" ."$PSScriptRoot\types.ps1" function writePending($timestamp, $requirement) { $symbol = " " $color = "Yellow" $message = "$symbol $timestamp $requirement" Write-Host $message -ForegroundColor $color -NoNewline } function writeSuccess($timestamp, $requirement, $clearString) { $symbol = [char]8730 $color = "Green" $message = "$symbol $timestamp $requirement" Write-Host "`r$clearString" -NoNewline Write-Host "`r$message" -ForegroundColor $color } function writeFail($timestamp, $requirement, $clearString) { $symbol = "X" $color = "Red" $message = "$symbol $timestamp $requirement" Write-Host "`r$clearString" -NoNewline Write-Host "`n$message`n" -ForegroundColor $color } $fsm = @{ "Test Test Start $false" = { writePending @args @{ "Test Test Stop $true" = { writeSuccess @args $fsm } "Test Test Stop $false" = { writeFail @args } } } "Set Set Start *" = { writePending @args @{ "Set Set Stop *" = { writeSuccess @args $fsm } } } "TestSet Test Start $false" = { writePending @args @{ "TestSet Test Stop $true" = { writeSuccess @args $fsm } "TestSet Test Stop $false" = { @{ "TestSet Set Start *" = { @{ "TestSet Set Stop *" = { @{ "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 .NOTES Uses Write-Host #> function Format-Checklist { [CmdletBinding()] Param( # Logged Requirement lifecycle events [Parameter(Mandatory, ValueFromPipeline)] [Alias("Event")] [RequirementEvent[]]$RequirementEvent ) begin { $previousRequirement = $null $nextFsm = $fsm } process { $requirement = $_.Requirement # build state vector $requirementType = ("Test", "Set" | ? { $requirement.$_ }) -join "" $method = $_.Method $lifecycleState = $_.State $successResult = if ($method -eq "Set") { "*" } else { [bool]$_.Result } $stateVector = "$requirementType $method $lifecycleState $successResult" # build transition arguments $timestamp = Get-Date -Date $_.Date -Format "hh:mm:ss" $clearString = ' ' * "? ??:??:?? $previousRequirement".Length $transitionArgs = @($timestamp, $requirement, $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 } } function Format-Verbose { [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingWriteHost", "")] [CmdletBinding()] [OutputType([string])] Param( # Logged Requirement lifecycle events [Parameter(Mandatory, ValueFromPipeline)] [Alias("Event")] [RequirementEvent[]]$RequirementEvent ) process { $timestamp = Get-Date -Date $_.Date -Format 'yyyy-MM-dd HH:mm:ss' "{0} {1,-8} {2,-5} {3}" -f $timestamp, $_.Method, $_.State, $_.Requirement } } |