diff --git a/src/public/Commands/Set-LogGroup.ps1 b/src/public/Commands/Set-LogGroup.ps1 new file mode 100644 index 000000000..feb266b4a --- /dev/null +++ b/src/public/Commands/Set-LogGroup.ps1 @@ -0,0 +1,46 @@ +function Set-LogGroup { + <# + .SYNOPSIS + Encapsulates commands with a log group in GitHub Actions + + .DESCRIPTION + DSL approach for GitHub Action commands. + Allows for colapsing of code in IDE for code that belong together. + + .EXAMPLE + Set-LogGroup -Name 'MyGroup' -ScriptBlock { + Write-Host 'Hello, World!' + } + + Creates a new log group named 'MyGroup' and writes 'Hello, World!' to the output. + + .EXAMPLE + LogGroup 'MyGroup' { + Write-Host 'Hello, World!' + } + + Uses the alias 'LogGroup' to create a new log group named 'MyGroup' and writes 'Hello, World!' to the output. + + .NOTES + [GitHub - Grouping log lines](https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#grouping-log-lines) + #> + [Alias('LogGroup')] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute( + 'PSUseShouldProcessForStateChangingFunctions', '', Scope = 'Function', + Justification = 'Does not change state' + )] + [CmdletBinding()] + param( + # The name of the log group + [Parameter(Mandatory)] + [string] $Name, + + # The script block to execute + [Parameter(Mandatory)] + [scriptblock] $ScriptBlock + ) + + Start-LogGroup -Name $Name + . $ScriptBlock + Stop-LogGroup +} diff --git a/src/public/Commands/Start-LogGroup.ps1 b/src/public/Commands/Start-LogGroup.ps1 new file mode 100644 index 000000000..96ba0fe74 --- /dev/null +++ b/src/public/Commands/Start-LogGroup.ps1 @@ -0,0 +1,31 @@ +function Start-LogGroup { + <# + .SYNOPSIS + Starts a log group in GitHub Actions + + .EXAMPLE + New-LogGroup 'MyGroup' + + Starts a new log group named 'MyGroup' + + .NOTES + [GitHub - Grouping log lines](https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#grouping-log-lines) + #> + [Alias('New-LogGroup')] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute( + 'PSUseShouldProcessForStateChangingFunctions', '', Scope = 'Function', + Justification = 'Does not change state' + )] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute( + 'PSAvoidUsingWriteHost', '', Scope = 'Function', + Justification = 'Intended for logging in Github Runners which does support Write-Host' + )] + [CmdletBinding()] + param( + # The name of the log group + [Parameter(Mandatory)] + [string] $Name + ) + + Write-Host "::group::$Name" +} diff --git a/src/public/Commands/Stop-LogGroup.ps1 b/src/public/Commands/Stop-LogGroup.ps1 new file mode 100644 index 000000000..4bb022070 --- /dev/null +++ b/src/public/Commands/Stop-LogGroup.ps1 @@ -0,0 +1,27 @@ +function Stop-LogGroup { + <# + .SYNOPSIS + Stops the current log group in GitHub Actions + + .EXAMPLE + Stop-LogGroup + + Starts a new log group named 'MyGroup' + + .NOTES + [GitHub - Grouping log lines](https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#grouping-log-lines) + #> + [Alias('End-LogGroup')] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute( + 'PSUseShouldProcessForStateChangingFunctions', '', Scope = 'Function', + Justification = 'Does not change state' + )] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute( + 'PSAvoidUsingWriteHost', '', Scope = 'Function', + Justification = 'Intended for logging in Github Runners' + )] + [CmdletBinding()] + param() + + Write-Host '::endgroup::' +} diff --git a/tests/Commands.Tests.ps1 b/tests/Commands.Tests.ps1 new file mode 100644 index 000000000..3f77dfa7d --- /dev/null +++ b/tests/Commands.Tests.ps1 @@ -0,0 +1,33 @@ +[CmdletBinding()] +Param( + # Path to the module to test. + [Parameter()] + [string] $Path +) + +Write-Verbose "Path to the module: [$Path]" -Verbose + +Describe 'Commands' { + It "Start-LogGroup 'MyGroup' should not throw" { + { + Start-LogGroup 'MyGroup' + } | Should -Not -Throw + } + + It 'Stop-LogGroup should not throw' { + { + Stop-LogGroup + } | Should -Not -Throw + } + + It "LogGroup 'MyGroup' should not throw" { + { + LogGroup 'MyGroup' { + Get-ChildItem env: | Select-Object Name, Value | Format-Table -AutoSize + } + } | Should -Not -Throw + } +} + + +