Skip to content

Commit aa17470

Browse files
committed
20211222A
1 parent 2fe8362 commit aa17470

File tree

2 files changed

+140
-106
lines changed

2 files changed

+140
-106
lines changed

hugoalh.GitHubActionsToolkit.psd1

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@
6262
# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
6363
FunctionsToExport = @(
6464
'Add-GHActionsEnvironmentVariable',
65-
'Add-GHActionsEnvironmentVariables',
66-
'Add-GHActionsPATHs',
65+
'Add-GHActionsPATH',
6766
'Add-GHActionsSecretMask',
6867
'Disable-GHActionsCommandEcho',
6968
'Disable-GHActionsProcessingCommand',

hugoalh.GitHubActionsToolkit.psm1

Lines changed: 139 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,26 @@
11
<#
22
.SYNOPSIS
3-
GitHub Actions - Internal - Escape Command Properties Characters
3+
GitHub Actions - Internal - Escape Characters
44
.DESCRIPTION
5-
An internal function to escape command properties characters that could cause issues.
6-
.PARAMETER InputObject
7-
#>
8-
function Format-GHActionsEscapeCommandPropertiesCharacters {
9-
[CmdletBinding()]
10-
param(
11-
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)][AllowEmptyString()][string]$InputObject
12-
)
13-
begin {}
14-
process {
15-
return $InputObject -replace '%', '%25' -replace "`n", '%0A' -replace "`r", '%0D' -replace ',', '%2C' -replace ':', '%3A'
16-
}
17-
end {}
18-
}
19-
<#
20-
.SYNOPSIS
21-
GitHub Actions - Internal - Escape New Line Characters
22-
.DESCRIPTION
23-
An internal function to escape new line characters that could cause issues.
5+
An internal function to escape characters that could cause issues.
246
.PARAMETER InputObject
7+
String that need to escape characters.
8+
.PARAMETER Command
9+
Also escape command properties characters.
2510
#>
26-
function Format-GHActionsEscapeDataCharacters {
11+
function Format-GHActionsEscapeCharacters {
2712
[CmdletBinding()]
2813
param(
29-
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)][AllowEmptyString()][string]$InputObject
14+
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)][AllowEmptyString()][string]$InputObject,
15+
[switch]$Command
3016
)
3117
begin {}
3218
process {
33-
return $InputObject -replace '%', '%25' -replace "`n", '%0A' -replace "`r", '%0D'
19+
$Result = $InputObject -replace '%', '%25' -replace "`n", '%0A' -replace "`r", '%0D'
20+
if ($Command) {
21+
$Result = $Result -replace ',', '%2C' -replace ':', '%3A'
22+
}
23+
return $Result
3424
}
3525
end {}
3626
}
@@ -56,78 +46,78 @@ function Write-GHActionsCommand {
5646
$Result = "::$Command"
5747
if ($Properties.Count -gt 0) {
5848
$Result += " $($($Properties.GetEnumerator() | ForEach-Object -Process {
59-
"$($_.Name)=$(Format-GHActionsEscapeCommandPropertiesCharacters -InputObject $_.Value)"
49+
"$($_.Name)=$(Format-GHActionsEscapeCharacters -InputObject $_.Value -Command)"
6050
}) -join ',')"
6151
}
62-
$Result += "::$(Format-GHActionsEscapeDataCharacters -InputObject $Message)"
52+
$Result += "::$(Format-GHActionsEscapeCharacters -InputObject $Message)"
6353
Write-Host -Object $Result
6454
}
55+
function Test-GHActionsEnvironmentVariable {
56+
[CmdletBinding()]
57+
param (
58+
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)][string]$InputObject
59+
)
60+
if (($InputObject -match '^[\da-z_]+=.+$') -and (($InputObject -split '=').Count -eq 2)) {
61+
return $true
62+
}
63+
Write-Error -Message "Input `"$InputObject`" is not match the require environment variable pattern." -Category SyntaxError
64+
}
6565
<#
6666
.SYNOPSIS
6767
GitHub Actions - Add Environment Variable
6868
.DESCRIPTION
69-
Add an environment variable to the system environment variables and automatically makes it available to all subsequent actions in the current job; The currently running action cannot access the updated environment variables.
69+
Add environment variable to the system environment variables and automatically makes it available to all subsequent actions in the current job; The currently running action cannot access the updated environment variables.
70+
.PARAMETER InputObject
71+
Environment variables.
7072
.PARAMETER Name
7173
Environment variable name.
7274
.PARAMETER Value
7375
Environment variable value.
7476
#>
7577
function Add-GHActionsEnvironmentVariable {
76-
[CmdletBinding()]
78+
[CmdletBinding(DefaultParameterSetName = 'single')]
7779
param(
78-
[Parameter(Mandatory = $true, Position = 0)][ValidatePattern('^.+$')][string]$Name,
79-
[Parameter(Mandatory = $true, Position = 1)][ValidatePattern('^.+$')][string]$Value
80-
)
81-
Add-Content -Encoding utf8NoBOM -Path $env:GITHUB_ENV -Value "$Name=$Value"
82-
}
83-
<#
84-
.SYNOPSIS
85-
GitHub Actions - Add Environment Variables
86-
.DESCRIPTION
87-
Add environment variables to the system environment variables and automatically makes these available to all subsequent actions in the current job; The currently running action cannot access the updated environment variables.
88-
.PARAMETER InputObject
89-
Environment variables.
90-
#>
91-
function Add-GHActionsEnvironmentVariables {
92-
[CmdletBinding()]
93-
param(
94-
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]$InputObject
80+
[Parameter(Mandatory = $true, ParameterSetName = 'multiple', Position = 0, ValueFromPipeline = $true)]$InputObject,
81+
[Parameter(Mandatory = $true, ParameterSetName = 'single', Position = 0)][ValidatePattern('^[\da-z_]+$')][string]$Name,
82+
[Parameter(Mandatory = $true, ParameterSetName = 'single', Position = 1)][ValidatePattern('^.+$')][string]$Value
9583
)
9684
begin {
9785
$Result = @{}
9886
}
9987
process {
100-
switch ($InputObject.GetType().FullName) {
101-
'System.Collections.Hashtable' {
102-
$InputObject.GetEnumerator() | ForEach-Object -Process {
103-
if (($_.Name -match '^.+$') -and ($_.Value -match '^.+$')) {
104-
$Result[$_.Name] = $_.Value
105-
} else {
106-
Write-Error -Message "Input `"$($_.Name)=$($_.Value)`" is not match the require environment variable pattern." -Category SyntaxError
88+
switch ($PSCmdlet.ParameterSetName) {
89+
'multiple' {
90+
switch ($InputObject.GetType().Name) {
91+
'Hashtable' {
92+
$InputObject.GetEnumerator() | ForEach-Object -Process {
93+
if (Test-GHActionsEnvironmentVariable -InputObject "$($_.Name)=$($_.Value)") {
94+
$Result[$_.Name] = $_.Value
95+
}
96+
}
10797
}
108-
}
109-
}
110-
'System.Management.Automation.PSCustomObject' {
111-
$InputObject.PSObject.Properties | ForEach-Object -Process {
112-
if (($_.Name -match '^.+$') -and ($_.Value -match '^.+$')) {
113-
$Result[$_.Name] = $_.Value
114-
} else {
115-
Write-Error -Message "Input `"$($_.Name)=$($_.Value)`" is not match the require environment variable pattern." -Category SyntaxError
98+
{$_ -in @('PSObject', 'PSCustomObject')} {
99+
$InputObject.PSObject.Properties | ForEach-Object -Process {
100+
if (Test-GHActionsEnvironmentVariable -InputObject "$($_.Name)=$($_.Value)") {
101+
$Result[$_.Name] = $_.Value
102+
}
103+
}
104+
}
105+
'String' {
106+
if (Test-GHActionsEnvironmentVariable -InputObject $InputObject) {
107+
$InputObjectSplit = $InputObject.Split('=')
108+
$Result[$InputObjectSplit[0]] = $InputObjectSplit[1]
109+
}
110+
}
111+
default {
112+
Write-Error -Message 'Parameter `InputObject` must be hashtable, object, or string!' -Category InvalidType
116113
}
117114
}
118115
}
119-
'System.String' {
120-
if (($InputObject -match '^.+=.+$') -and (($InputObject -split '=').Count -eq 2)) {
121-
$InputObjectSplit = $InputObject.Split('=')
122-
$Result[$InputObjectSplit[0]] = $InputObjectSplit[1]
123-
} else {
124-
Write-Error -Message "Input `"$InputObject`" is not match the require environment variable pattern." -Category SyntaxError
125-
}
126-
}
127-
default {
128-
Write-Error -Message 'Parameter `InputObject` must be custom object, hashtable, or string!' -Category InvalidType
116+
'single' {
117+
$Result[$Name] = $Value
129118
}
130119
}
120+
131121
}
132122
end {
133123
Add-Content -Encoding utf8NoBOM -Path $env:GITHUB_ENV -Value "$($($Result.GetEnumerator() | ForEach-Object -Process {
@@ -137,24 +127,28 @@ function Add-GHActionsEnvironmentVariables {
137127
}
138128
<#
139129
.SYNOPSIS
140-
GitHub Actions - Add PATHs
130+
GitHub Actions - Add PATH
141131
.DESCRIPTION
142-
Add directories to the system `PATH` variable and automatically makes these available to all subsequent actions in the current job; The currently running action cannot access the updated path variable.
132+
Add directory to the system `PATH` variable and automatically makes it available to all subsequent actions in the current job; The currently running action cannot access the updated path variable.
143133
.PARAMETER Path
144134
System path.
145-
.EXAMPLE
146-
Add-GHActionsPATH -Path "$($env:HOME)/.local/bin"
147135
#>
148-
function Add-GHActionsPATHs {
136+
function Add-GHActionsPATH {
149137
[CmdletBinding()]
150138
param(
151-
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)][string]$Path
139+
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)][string[]]$Path
152140
)
153141
begin {
154142
$Result = @()
155143
}
156144
process {
157-
$Result += $Path
145+
$Path.GetEnumerator() | ForEach-Object -Process {
146+
if (Test-Path -Path $_ -IsValid) {
147+
$Result += $_
148+
} else {
149+
Write-Error -Message "Input `"$_`" is not match the require path pattern." -Category SyntaxError
150+
}
151+
}
158152
}
159153
end {
160154
Add-Content -Encoding utf8NoBOM -Path $env:GITHUB_PATH -Value "$($Result -join "`n")"
@@ -197,7 +191,7 @@ function Get-GHActionsIsDebug {
197191
.SYNOPSIS
198192
GitHub Actions - Get Input
199193
.DESCRIPTION
200-
Get an input.
194+
Get input.
201195
.PARAMETER Name
202196
Name of the input.
203197
.PARAMETER Required
@@ -208,33 +202,42 @@ Trim the input's value.
208202
function Get-GHActionsInput {
209203
[CmdletBinding()]
210204
param(
211-
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)][string]$Name,
205+
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)][string[]]$Name,
212206
[switch]$Required,
213207
[switch]$Trim
214208
)
215-
begin {}
209+
begin {
210+
$Result = @{}
211+
}
216212
process {
217-
$Result = Get-ChildItem -Path "Env:\INPUT_$($Name.ToUpper() -replace ' ','_')" -ErrorAction SilentlyContinue
218-
$Value = $null
219-
if ($Result -eq $null) {
220-
if ($Required -eq $true) {
221-
throw "Input ``$Name`` is not defined!"
213+
$Name.GetEnumerator() | ForEach-Object -Process {
214+
$InputValue = Get-ChildItem -Path "Env:\INPUT_$($_.ToUpper() -replace '[ \n\r]','_')" -ErrorAction SilentlyContinue
215+
if ($InputValue -eq $null) {
216+
if ($Required) {
217+
throw "Input ``$_`` is not defined!"
218+
}
219+
$Result[$_] = $InputValue
220+
} else {
221+
if ($Trim) {
222+
$Result[$_] = $InputValue.Value.Trim()
223+
} else {
224+
$Result[$_] = $InputValue.Value
225+
}
222226
}
223-
return $Result
224227
}
225-
$Value = $Result.Value
226-
if ($Trim -eq $true) {
227-
return $Value.Trim()
228+
}
229+
end {
230+
if ($Result.Count -eq 1) {
231+
return $Result.Values[0]
228232
}
229-
return $Value
233+
return $Result
230234
}
231-
end {}
232235
}
233236
<#
234237
.SYNOPSIS
235-
Get a state.
238+
GitHub Actions - Get State
236239
.DESCRIPTION
237-
Get a state.
240+
Get state.
238241
.PARAMETER Name
239242
Name of the state.
240243
.PARAMETER Trim
@@ -243,26 +246,37 @@ Trim the state's value.
243246
function Get-GHActionsState {
244247
[CmdletBinding()]
245248
param(
246-
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)][ValidatePattern('^.+$')][string]$Name,
249+
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)][string[]]$Name,
247250
[switch]$Trim
248251
)
249-
begin {}
252+
begin {
253+
$Result = @{}
254+
}
250255
process {
251-
$Result = Get-ChildItem -Path "Env:\STATE_$($Name.ToUpper() -replace ' ','_')" -ErrorAction SilentlyContinue
252-
$Value = $null
253-
if ($Result -eq $null) {
254-
return $Result
256+
$Name.GetEnumerator() | ForEach-Object -Process {
257+
$StateValue = Get-ChildItem -Path "Env:\STATE_$($_.ToUpper() -replace '[ \n\r]','_')" -ErrorAction SilentlyContinue
258+
if ($StateValue -eq $null) {
259+
$Result[$_] = $StateValue
260+
} else {
261+
if ($Trim) {
262+
$Result[$_] = $StateValue.Value.Trim()
263+
} else {
264+
$Result[$_] = $StateValue.Value
265+
}
266+
}
255267
}
256-
$Value = $Result.Value
257-
if ($Trim -eq $true) {
258-
return $Value.Trim()
268+
}
269+
end {
270+
if ($Result.Count -eq 1) {
271+
return $Result.Values[0]
259272
}
260-
return $Value
273+
return $Result
261274
}
262-
end {}
263275
}
264276
<#
265277
.SYNOPSIS
278+
GitHub Actions - Set Output
279+
.DESCRIPTION
266280
Set an output.
267281
.PARAMETER Name
268282
Name of the output.
@@ -277,6 +291,27 @@ function Set-GHActionsOutput {
277291
)
278292
Write-GHActionsCommand -Command 'set-output' -Message $Value -Properties @{'name' = $Name }
279293
}
294+
<#
295+
.SYNOPSIS
296+
GitHub Actions - Set Outputs
297+
.DESCRIPTION
298+
Set outputs.
299+
.PARAMETER InputObject
300+
Outputs.
301+
#>
302+
function Set-GHActionsOutputs {
303+
[CmdletBinding()]
304+
param(
305+
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)][hashtable]$InputObject
306+
)
307+
begin {}
308+
process {
309+
$InputObject.GetEnumerator() | ForEach-Object -Process {
310+
Set-GHActionsOutput -Name $_.Name -Value $_.Value
311+
}
312+
}
313+
end {}
314+
}
280315
function Save-GHActionsState {
281316
[CmdletBinding()]
282317
param(
@@ -482,4 +517,4 @@ function Write-GHActionsWarning {
482517
}
483518
end {}
484519
}
485-
Export-ModuleMember -Function Add-GHActionsEnvironmentVariable, Add-GHActionsEnvironmentVariables, Add-GHActionsPATHs, Add-GHActionsSecretMask, Disable-GHActionsCommandEcho, Disable-GHActionsProcessingCommand, Enable-GHActionsCommandEcho, Enable-GHActionsProcessingCommand, Enter-GHActionsLogGroup, Exit-GHActionsLogGroup, Get-GHActionsInput, Get-GHActionsIsDebug, Get-GHActionsState, Invoke-GHActionsScriptGroup, Set-GHActionsOutput, Write-GHActionsDebug, Write-GHActionsError, Write-GHActionsFail, Write-GHActionsNotice, Write-GHActionsWarning
520+
Export-ModuleMember -Function Add-GHActionsEnvironmentVariable, Add-GHActionsPATH, Add-GHActionsSecretMask, Disable-GHActionsCommandEcho, Disable-GHActionsProcessingCommand, Enable-GHActionsCommandEcho, Enable-GHActionsProcessingCommand, Enter-GHActionsLogGroup, Exit-GHActionsLogGroup, Get-GHActionsInput, Get-GHActionsIsDebug, Get-GHActionsState, Invoke-GHActionsScriptGroup, Set-GHActionsOutput, Write-GHActionsDebug, Write-GHActionsError, Write-GHActionsFail, Write-GHActionsNotice, Write-GHActionsWarning

0 commit comments

Comments
 (0)