module/log.psm1
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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 |
#Requires -PSEdition Core -Version 7.2 Import-Module -Name ( @( 'command-base', 'command-control' ) | ForEach-Object -Process { Join-Path -Path $PSScriptRoot -ChildPath "$_.psm1" } ) -Prefix 'GitHubActions' -Scope 'Local' Enum GitHubActionsAnnotationType { Notice = 0 N = 0 Note = 0 Warning = 1 W = 1 Warn = 1 Error = 2 E = 2 } <# .SYNOPSIS GitHub Actions - Enter Log Group .DESCRIPTION Create an expandable group in the log; Anything write to the log are inside this expandable group in the log. .PARAMETER Title Title of the log group. .OUTPUTS [Void] #> Function Enter-LogGroup { [CmdletBinding(HelpUri = 'https://github.com/hugoalh-studio/ghactions-toolkit-powershell/wiki/api_function_entergithubactionsloggroup')] [OutputType([Void])] Param ( [Parameter(Mandatory = $True, Position = 0)][ValidatePattern('^.+$', ErrorMessage = 'Parameter `Title` must be in single line string!')][Alias('Header', 'Summary')][String]$Title ) Write-GitHubActionsStdOutCommand -StdOutCommand 'group' -Value $Title } Set-Alias -Name 'Enter-Group' -Value 'Enter-LogGroup' -Option 'ReadOnly' -Scope 'Local' <# .SYNOPSIS GitHub Actions - Exit Log Group .DESCRIPTION End an expandable group in the log. .OUTPUTS [Void] #> Function Exit-LogGroup { [CmdletBinding(HelpUri = 'https://github.com/hugoalh-studio/ghactions-toolkit-powershell/wiki/api_function_exitgithubactionsloggroup')] [OutputType([Void])] Param () Write-GitHubActionsStdOutCommand -StdOutCommand 'endgroup' } Set-Alias -Name 'Exit-Group' -Value 'Exit-LogGroup' -Option 'ReadOnly' -Scope 'Local' <# .SYNOPSIS GitHub Actions - Write Annotation .DESCRIPTION Print an annotation message to the log. .PARAMETER Type Type of the annotation. .PARAMETER Message Message of the annotation. .PARAMETER File Path of the issue file of the annotation. .PARAMETER Line Line start of the issue file of the annotation. .PARAMETER Column Column start of the issue file of the annotation. .PARAMETER EndLine Line end of the issue file of the annotation. .PARAMETER EndColumn Column end of the issue file of the annotation. .PARAMETER Title Title of the annotation. .OUTPUTS [Void] #> Function Write-Annotation { [CmdletBinding(HelpUri = 'https://github.com/hugoalh-studio/ghactions-toolkit-powershell/wiki/api_function_writegithubactionsannotation')] [OutputType([Void])] Param ( [Parameter(Mandatory = $True, Position = 0, ValueFromPipelineByPropertyName = $True)][GitHubActionsAnnotationType]$Type, [Parameter(Mandatory = $True, Position = 1, ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True)][Alias('Content')][String]$Message, [Parameter(ValueFromPipelineByPropertyName = $True)][ValidatePattern('^.*$', ErrorMessage = 'Parameter `File` must be in single line string!')][Alias('Path')][String]$File, [Parameter(ValueFromPipelineByPropertyName = $True)][Alias('LineStart', 'StartLine')][UInt32]$Line, [Parameter(ValueFromPipelineByPropertyName = $True)][Alias('Col', 'ColStart', 'ColumnStart', 'StartCol', 'StartColumn')][UInt32]$Column, [Parameter(ValueFromPipelineByPropertyName = $True)][Alias('LineEnd')][UInt32]$EndLine, [Parameter(ValueFromPipelineByPropertyName = $True)][Alias('ColEnd', 'ColumnEnd', 'EndCol')][UInt32]$EndColumn, [Parameter(ValueFromPipelineByPropertyName = $True)][ValidatePattern('^.*$', ErrorMessage = 'Parameter `Title` must be in single line string!')][Alias('Header')][String]$Title ) Process { Switch ($Type.GetHashCode()) { 0 { [String]$TypeRaw = 'notice' Break } 1 { [String]$TypeRaw = 'warning' Break } 2 { [String]$TypeRaw = 'error' Break } } [Hashtable]$Property = @{} If ($File.Length -igt 0) { $Property['file'] = $File } If ($Line -igt 0) { $Property['line'] = $Line } If ($Column -igt 0) { $Property['col'] = $Column } If ($EndLine -igt 0) { $Property['endLine'] = $EndLine } If ($EndColumn -igt 0) { $Property['endColumn'] = $EndColumn } If ($Title.Length -igt 0) { $Property['title'] = $Title } Write-GitHubActionsStdOutCommand -StdOutCommand $TypeRaw -Parameter $Property -Value $Message } } <# .SYNOPSIS GitHub Actions - Write Debug .DESCRIPTION Print a debug message to the log. .PARAMETER Message Message that need to log at debug level. .OUTPUTS [Void] #> Function Write-Debug { [CmdletBinding(HelpUri = 'https://github.com/hugoalh-studio/ghactions-toolkit-powershell/wiki/api_function_writegithubactionsdebug')] [OutputType([Void])] Param ( [Parameter(Mandatory = $True, Position = 0, ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True)][Alias('Content')][String]$Message ) Process { Write-GitHubActionsStdOutCommand -StdOutCommand 'debug' -Value $Message } } <# .SYNOPSIS GitHub Actions - Write Error .DESCRIPTION Print an error message to the log. .PARAMETER Message Message that need to log at error level. .PARAMETER File Path of the issue file of the annotation. .PARAMETER Line Line start of the issue file of the annotation. .PARAMETER Column Column start of the issue file of the annotation. .PARAMETER EndLine Line end of the issue file of the annotation. .PARAMETER EndColumn Column end of the issue file of the annotation. .PARAMETER Title Title of the error message. .OUTPUTS [Void] #> Function Write-Error { [CmdletBinding(HelpUri = 'https://github.com/hugoalh-studio/ghactions-toolkit-powershell/wiki/api_function_writegithubactionserror')] [OutputType([Void])] Param ( [Parameter(Mandatory = $True, Position = 0, ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True)][Alias('Content')][String]$Message, [Parameter(ValueFromPipelineByPropertyName = $True)][ValidatePattern('^.*$', ErrorMessage = 'Parameter `File` must be in single line string!')][Alias('Path')][String]$File, [Parameter(ValueFromPipelineByPropertyName = $True)][Alias('LineStart', 'StartLine')][UInt32]$Line, [Parameter(ValueFromPipelineByPropertyName = $True)][Alias('Col', 'ColStart', 'ColumnStart', 'StartCol', 'StartColumn')][UInt32]$Column, [Parameter(ValueFromPipelineByPropertyName = $True)][Alias('LineEnd')][UInt32]$EndLine, [Parameter(ValueFromPipelineByPropertyName = $True)][Alias('ColEnd', 'ColumnEnd', 'EndCol')][UInt32]$EndColumn, [Parameter(ValueFromPipelineByPropertyName = $True)][ValidatePattern('^.*$', ErrorMessage = 'Parameter `Title` must be in single line string!')][Alias('Header')][String]$Title ) Process { Write-Annotation -Type 'Error' -Message $Message -File $File -Line $Line -Column $Column -EndLine $EndLine -EndColumn $EndColumn -Title $Title } } <# .SYNOPSIS GitHub Actions - Write Fail .DESCRIPTION Print an error message to the log and end the process. .PARAMETER Message Message that need to log at error level. .PARAMETER File Path of the issue file of the annotation. .PARAMETER Line Line start of the issue file of the annotation. .PARAMETER Column Column start of the issue file of the annotation. .PARAMETER EndLine Line end of the issue file of the annotation. .PARAMETER EndColumn Column end of the issue file of the annotation. .PARAMETER Title Title of the error message. .PARAMETER Finally A script that invoke before end the process, use to free any resources that are no longer needed. .PARAMETER ExitCode Exit code of the process. .OUTPUTS [Void] #> Function Write-Fail { [CmdletBinding(HelpUri = 'https://github.com/hugoalh-studio/ghactions-toolkit-powershell/wiki/api_function_writegithubactionsfail')] [OutputType([Void])] Param ( [Parameter(Mandatory = $True, Position = 0)][Alias('Content')][String]$Message, [ValidatePattern('^.*$', ErrorMessage = 'Parameter `File` must be in single line string!')][Alias('Path')][String]$File, [Alias('LineStart', 'StartLine')][UInt32]$Line, [Alias('Col', 'ColStart', 'ColumnStart', 'StartCol', 'StartColumn')][UInt32]$Column, [Alias('LineEnd')][UInt32]$EndLine, [Alias('ColEnd', 'ColumnEnd', 'EndCol')][UInt32]$EndColumn, [ValidatePattern('^.*$', ErrorMessage = 'Parameter `Title` must be in single line string!')][Alias('Header')][String]$Title, [ScriptBlock]$Finally = {}, [ValidateRange(1, [Byte]::MaxValue)][Byte]$ExitCode = 1 ) Write-Annotation -Type 'Error' -Message $Message -File $File -Line $Line -Column $Column -EndLine $EndLine -EndColumn $EndColumn -Title $Title Invoke-Command -ScriptBlock $Finally -ErrorAction 'Continue' Exit $ExitCode Exit 1# Fallback for safety. } <# .SYNOPSIS GitHub Actions - Write Notice .DESCRIPTION Print a notice message to the log. .PARAMETER Message Message that need to log at notice level. .PARAMETER File Path of the issue file of the annotation. .PARAMETER Line Line start of the issue file of the annotation. .PARAMETER Column Column start of the issue file of the annotation. .PARAMETER EndLine Line end of the issue file of the annotation. .PARAMETER EndColumn Column end of the issue file of the annotation. .PARAMETER Title Title of the notice message. .OUTPUTS [Void] #> Function Write-Notice { [CmdletBinding(HelpUri = 'https://github.com/hugoalh-studio/ghactions-toolkit-powershell/wiki/api_function_writegithubactionsnotice')] [OutputType([Void])] Param ( [Parameter(Mandatory = $True, Position = 0, ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True)][Alias('Content')][String]$Message, [Parameter(ValueFromPipelineByPropertyName = $True)][ValidatePattern('^.*$', ErrorMessage = 'Parameter `File` must be in single line string!')][Alias('Path')][String]$File, [Parameter(ValueFromPipelineByPropertyName = $True)][Alias('LineStart', 'StartLine')][UInt32]$Line, [Parameter(ValueFromPipelineByPropertyName = $True)][Alias('Col', 'ColStart', 'ColumnStart', 'StartCol', 'StartColumn')][UInt32]$Column, [Parameter(ValueFromPipelineByPropertyName = $True)][Alias('LineEnd')][UInt32]$EndLine, [Parameter(ValueFromPipelineByPropertyName = $True)][Alias('ColEnd', 'ColumnEnd', 'EndCol')][UInt32]$EndColumn, [Parameter(ValueFromPipelineByPropertyName = $True)][ValidatePattern('^.*$', ErrorMessage = 'Parameter `Title` must be in single line string!')][Alias('Header')][String]$Title ) Process { Write-Annotation -Type 'Notice' -Message $Message -File $File -Line $Line -Column $Column -EndLine $EndLine -EndColumn $EndColumn -Title $Title } } Set-Alias -Name 'Write-Note' -Value 'Write-Notice' -Option 'ReadOnly' -Scope 'Local' <# .SYNOPSIS GitHub Actions - Write Raw .DESCRIPTION Print anything to the log without accidentally execute any commands. .PARAMETER InputObject Item that need to log. .PARAMETER GroupTitle Title of the log group; This creates an expandable group in the log, and anything are inside this expandable group in the log. .OUTPUTS [Void] #> Function Write-Raw { [CmdletBinding(HelpUri = 'https://github.com/hugoalh-studio/ghactions-toolkit-powershell/wiki/api_function_writegithubactionsraw')] [OutputType([Void])] Param ( [Parameter(Mandatory = $True, Position = 0, ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True)][AllowEmptyCollection()][AllowEmptyString()][AllowNull()][Alias('Content', 'Input', 'Message', 'Object')]$InputObject, [Alias('GroupHeader', 'Header', 'Title')][String]$GroupTitle ) Begin { If ($GroupTitle.Length -igt 0) { Enter-LogGroup -Title $GroupTitle } [String]$EndToken = Disable-GitHubActionsProcessingCommands } Process { Write-Host -Object $InputObject } End { Enable-GitHubActionsProcessingCommands -EndToken $EndToken If ($GroupTitle.Length -igt 0) { Exit-LogGroup } } } <# .SYNOPSIS GitHub Actions - Write Warning .DESCRIPTION Print a warning message to the log. .PARAMETER Message Message that need to log at warning level. .PARAMETER File Path of the issue file of the annotation. .PARAMETER Line Line start of the issue file of the annotation. .PARAMETER Column Column start of the issue file of the annotation. .PARAMETER EndLine Line end of the issue file of the annotation. .PARAMETER EndColumn Column end of the issue file of the annotation. .PARAMETER Title Title of the warning message. .OUTPUTS [Void] #> Function Write-Warning { [CmdletBinding(HelpUri = 'https://github.com/hugoalh-studio/ghactions-toolkit-powershell/wiki/api_function_writegithubactionswarning')] [OutputType([Void])] Param ( [Parameter(Mandatory = $True, Position = 0, ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True)][Alias('Content')][String]$Message, [Parameter(ValueFromPipelineByPropertyName = $True)][ValidatePattern('^.*$', ErrorMessage = 'Parameter `File` must be in single line string!')][Alias('Path')][String]$File, [Parameter(ValueFromPipelineByPropertyName = $True)][Alias('LineStart', 'StartLine')][UInt32]$Line, [Parameter(ValueFromPipelineByPropertyName = $True)][Alias('Col', 'ColStart', 'ColumnStart', 'StartCol', 'StartColumn')][UInt32]$Column, [Parameter(ValueFromPipelineByPropertyName = $True)][Alias('LineEnd')][UInt32]$EndLine, [Parameter(ValueFromPipelineByPropertyName = $True)][Alias('ColEnd', 'ColumnEnd', 'EndCol')][UInt32]$EndColumn, [Parameter(ValueFromPipelineByPropertyName = $True)][ValidatePattern('^.*$', ErrorMessage = 'Parameter `Title` must be in single line string!')][Alias('Header')][String]$Title ) Process { Write-Annotation -Type 'Warning' -Message $Message -File $File -Line $Line -Column $Column -EndLine $EndLine -EndColumn $EndColumn -Title $Title } } Set-Alias -Name 'Write-Warn' -Value 'Write-Warning' -Option 'ReadOnly' -Scope 'Local' Export-ModuleMember -Function @( 'Enter-LogGroup', 'Exit-LogGroup', 'Write-Annotation', 'Write-Debug', 'Write-Error', 'Write-Fail', 'Write-Notice', 'Write-Raw', 'Write-Warning' ) -Alias @( 'Enter-Group', 'Exit-Group', 'Write-Note', 'Write-Warn' ) |