Skip to content

Commit 8ae52d2

Browse files
committed
Add support for file commands GITHUB_OUTPUT and GITHUB_STATE
1 parent 4af728b commit 8ae52d2

File tree

3 files changed

+91
-10
lines changed

3 files changed

+91
-10
lines changed

hugoalh.GitHubActionsToolkit/module/command-base.psm1

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
#Requires -PSEdition Core
22
#Requires -Version 7.2
3+
Import-Module -Name (
4+
@(
5+
'internal\token.psm1'
6+
) |
7+
ForEach-Object -Process { Join-Path -Path $PSScriptRoot -ChildPath $_ }
8+
) -Prefix 'GitHubActions' -Scope 'Local'
9+
[String[]]$GitHubActionsFileCommandTokensUsed = @()
310
<#
411
.SYNOPSIS
512
GitHub Actions (Private) - Format Command Parameter Value
@@ -76,6 +83,46 @@ Function Write-Command {
7683
)" : '')::$(Format-CommandValue -InputObject $Value)"
7784
}
7885
}
86+
<#
87+
.SYNOPSIS
88+
GitHub Actions (Private) - Write File Command
89+
.DESCRIPTION
90+
Write file command to communicate with the runner machine.
91+
.PARAMETER LiteralPath
92+
Literal path of the file.
93+
.PARAMETER Table
94+
Table.
95+
.OUTPUTS
96+
[Void]
97+
#>
98+
Function Write-FileCommand {
99+
[CmdletBinding()]
100+
[OutputType([Void])]
101+
Param (
102+
[Parameter(Mandatory = $True, Position = 0, ValueFromPipelineByPropertyName = $True)][String]$LiteralPath,
103+
[Parameter(Mandatory = $True, Position = 1, ValueFromPipelineByPropertyName = $True)][Hashtable]$Table
104+
)
105+
Process {
106+
Add-Content -LiteralPath $LiteralPath -Value (
107+
$Table.GetEnumerator() |
108+
ForEach-Object -Process {
109+
If ($_.Value -imatch '^.+$') {
110+
Write-Output -InputObject "$($_.Name)=$($_.Value)"
111+
}
112+
Else {
113+
Do {
114+
[String]$Token = New-GitHubActionsRandomToken -Length 64
115+
}
116+
While ( $Token -iin $GitHubActionsFileCommandTokensUsed )
117+
$Script:GitHubActionsFileCommandTokensUsed += $Token
118+
Write-Output -InputObject "$($_.Name)=<<$Token`n$($_.Value -ireplace '\r?\n', "`n")`n$Token"
119+
}
120+
} |
121+
Join-String -Separator "`n"
122+
) -Confirm:$False -Encoding 'UTF8NoBOM'
123+
}
124+
}
79125
Export-ModuleMember -Function @(
80-
'Write-Command'
126+
'Write-Command',
127+
'Write-FileCommand'
81128
)

hugoalh.GitHubActionsToolkit/module/environment-variable.psm1

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,7 @@ Function Set-EnvironmentVariable {
148148
ForEach-Object -Process { Write-GitHubActionsCommand -Command 'set-env' -Parameter @{ 'name' = $_.Name } -Value $_.Value }
149149
}
150150
Else {
151-
Add-Content -LiteralPath $Env:GITHUB_ENV -Value (
152-
$Result.GetEnumerator() |
153-
ForEach-Object -Process { "$($_.Name)=$($_.Value)" } |
154-
Join-String -Separator "`n"
155-
) -Confirm:$False -Encoding 'UTF8NoBOM'
151+
Write-GitHubActionsFileCommand -LiteralPath $Env:GITHUB_ENV -Table $Result
156152
}
157153
}
158154
}

hugoalh.GitHubActionsToolkit/module/parameter.psm1

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,10 @@ Function Set-Output {
174174
[Parameter(Mandatory = $True, ParameterSetName = 'Single', Position = 0, ValueFromPipelineByPropertyName = $True)][ValidatePattern('^(?:[\da-z][\da-z_-]*)?[\da-z]$', ErrorMessage = '`{0}` is not a valid GitHub Actions output name!')][Alias('Key')][String]$Name,
175175
[Parameter(Mandatory = $True, ParameterSetName = 'Single', Position = 1, ValueFromPipelineByPropertyName = $True)][AllowEmptyString()][String]$Value
176176
)
177+
Begin {
178+
[Boolean]$FileEditionAvailable = ![String]::IsNullOrWhiteSpace($Env:GITHUB_OUTPUT)
179+
[Hashtable]$Result = @{}
180+
}
177181
Process {
178182
Switch ($PSCmdlet.ParameterSetName) {
179183
'Multiple' {
@@ -191,14 +195,29 @@ Function Set-Output {
191195
Write-Error -Message 'Parameter `Value` must be type of string!' -Category 'InvalidType'
192196
Return
193197
}
194-
Write-GitHubActionsCommand -Command 'set-output' -Parameter @{ 'name' = $_.Name } -Value $_.Value
198+
If ($FileEditionAvailable) {
199+
$Result[$_.Name] = $_.Value
200+
}
201+
Else {
202+
Write-GitHubActionsCommand -Command 'set-output' -Parameter @{ 'name' = $_.Name } -Value $_.Value
203+
}
195204
}
196205
}
197206
'Single' {
198-
Write-GitHubActionsCommand -Command 'set-output' -Parameter @{ 'name' = $Name } -Value $Value
207+
If ($FileEditionAvailable) {
208+
$Result[$Name] = $Value
209+
}
210+
Else {
211+
Write-GitHubActionsCommand -Command 'set-output' -Parameter @{ 'name' = $Name } -Value $Value
212+
}
199213
}
200214
}
201215
}
216+
End {
217+
If ($FileEditionAvailable) {
218+
Write-GitHubActionsFileCommand -LiteralPath $Env:GITHUB_OUTPUT -Table $Result
219+
}
220+
}
202221
}
203222
<#
204223
.SYNOPSIS
@@ -222,6 +241,10 @@ Function Set-State {
222241
[Parameter(Mandatory = $True, ParameterSetName = 'Single', Position = 0, ValueFromPipelineByPropertyName = $True)][ValidatePattern('^(?:[\da-z][\da-z_-]*)?[\da-z]$', ErrorMessage = '`{0}` is not a valid GitHub Actions state name!')][Alias('Key')][String]$Name,
223242
[Parameter(Mandatory = $True, ParameterSetName = 'Single', Position = 1, ValueFromPipelineByPropertyName = $True)][AllowEmptyString()][String]$Value
224243
)
244+
Begin {
245+
[Boolean]$FileEditionAvailable = ![String]::IsNullOrWhiteSpace($Env:GITHUB_STATE)
246+
[Hashtable]$Result = @{}
247+
}
225248
Process {
226249
Switch ($PSCmdlet.ParameterSetName) {
227250
'Multiple' {
@@ -239,14 +262,29 @@ Function Set-State {
239262
Write-Error -Message 'Parameter `Value` must be type of string!' -Category 'InvalidType'
240263
Return
241264
}
242-
Write-GitHubActionsCommand -Command 'save-state' -Parameter @{ 'name' = $_.Name } -Value $_.Value
265+
If ($FileEditionAvailable) {
266+
$Result[$_.Name] = $_.Value
267+
}
268+
Else {
269+
Write-GitHubActionsCommand -Command 'save-state' -Parameter @{ 'name' = $_.Name } -Value $_.Value
270+
}
243271
}
244272
}
245273
'Single' {
246-
Write-GitHubActionsCommand -Command 'save-state' -Parameter @{ 'name' = $Name } -Value $Value
274+
If ($FileEditionAvailable) {
275+
$Result[$Name] = $Value
276+
}
277+
Else {
278+
Write-GitHubActionsCommand -Command 'save-state' -Parameter @{ 'name' = $Name } -Value $Value
279+
}
247280
}
248281
}
249282
}
283+
End {
284+
If ($FileEditionAvailable) {
285+
Write-GitHubActionsFileCommand -LiteralPath $Env:GITHUB_STATE -Table $Result
286+
}
287+
}
250288
}
251289
Set-Alias -Name 'Save-State' -Value 'Set-State' -Option 'ReadOnly' -Scope 'Local'
252290
Export-ModuleMember -Function @(

0 commit comments

Comments
 (0)