1
1
<#
2
2
. SYNOPSIS
3
- GitHub Actions - Internal - Escape Command Properties Characters
3
+ GitHub Actions - Internal - Escape Characters
4
4
. 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.
24
6
. PARAMETER InputObject
7
+ String that need to escape characters.
8
+ . PARAMETER Command
9
+ Also escape command properties characters.
25
10
#>
26
- function Format-GHActionsEscapeDataCharacters {
11
+ function Format-GHActionsEscapeCharacters {
27
12
[CmdletBinding ()]
28
13
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
30
16
)
31
17
begin {}
32
18
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
34
24
}
35
25
end {}
36
26
}
@@ -56,78 +46,78 @@ function Write-GHActionsCommand {
56
46
$Result = " ::$Command "
57
47
if ($Properties.Count -gt 0 ) {
58
48
$Result += " $ ( $ ($Properties.GetEnumerator () | ForEach-Object - Process {
59
- " $ ( $_.Name ) =$ ( Format-GHActionsEscapeCommandPropertiesCharacters - InputObject $_.Value ) "
49
+ " $ ( $_.Name ) =$ ( Format-GHActionsEscapeCharacters - InputObject $_.Value - Command ) "
60
50
}) -join ' ,' ) "
61
51
}
62
- $Result += " ::$ ( Format-GHActionsEscapeDataCharacters - InputObject $Message ) "
52
+ $Result += " ::$ ( Format-GHActionsEscapeCharacters - InputObject $Message ) "
63
53
Write-Host - Object $Result
64
54
}
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
+ }
65
65
<#
66
66
. SYNOPSIS
67
67
GitHub Actions - Add Environment Variable
68
68
. 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.
70
72
. PARAMETER Name
71
73
Environment variable name.
72
74
. PARAMETER Value
73
75
Environment variable value.
74
76
#>
75
77
function Add-GHActionsEnvironmentVariable {
76
- [CmdletBinding ()]
78
+ [CmdletBinding (DefaultParameterSetName = ' single ' )]
77
79
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
95
83
)
96
84
begin {
97
85
$Result = @ {}
98
86
}
99
87
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
+ }
107
97
}
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
116
113
}
117
114
}
118
115
}
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
129
118
}
130
119
}
120
+
131
121
}
132
122
end {
133
123
Add-Content - Encoding utf8NoBOM - Path $env: GITHUB_ENV - Value " $ ( $ ($Result.GetEnumerator () | ForEach-Object - Process {
@@ -137,24 +127,28 @@ function Add-GHActionsEnvironmentVariables {
137
127
}
138
128
<#
139
129
. SYNOPSIS
140
- GitHub Actions - Add PATHs
130
+ GitHub Actions - Add PATH
141
131
. 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.
143
133
. PARAMETER Path
144
134
System path.
145
- . EXAMPLE
146
- Add-GHActionsPATH -Path "$($env:HOME)/.local/bin"
147
135
#>
148
- function Add-GHActionsPATHs {
136
+ function Add-GHActionsPATH {
149
137
[CmdletBinding ()]
150
138
param (
151
- [Parameter (Mandatory = $true , Position = 0 , ValueFromPipeline = $true )][string ]$Path
139
+ [Parameter (Mandatory = $true , Position = 0 , ValueFromPipeline = $true , ValueFromPipelineByPropertyName = $true )][string [] ]$Path
152
140
)
153
141
begin {
154
142
$Result = @ ()
155
143
}
156
144
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
+ }
158
152
}
159
153
end {
160
154
Add-Content - Encoding utf8NoBOM - Path $env: GITHUB_PATH - Value " $ ( $Result -join " `n " ) "
@@ -197,7 +191,7 @@ function Get-GHActionsIsDebug {
197
191
. SYNOPSIS
198
192
GitHub Actions - Get Input
199
193
. DESCRIPTION
200
- Get an input.
194
+ Get input.
201
195
. PARAMETER Name
202
196
Name of the input.
203
197
. PARAMETER Required
@@ -208,33 +202,42 @@ Trim the input's value.
208
202
function Get-GHActionsInput {
209
203
[CmdletBinding ()]
210
204
param (
211
- [Parameter (Mandatory = $true , Position = 0 , ValueFromPipeline = $true )][string ]$Name ,
205
+ [Parameter (Mandatory = $true , Position = 0 , ValueFromPipeline = $true )][string [] ]$Name ,
212
206
[switch ]$Required ,
213
207
[switch ]$Trim
214
208
)
215
- begin {}
209
+ begin {
210
+ $Result = @ {}
211
+ }
216
212
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
+ }
222
226
}
223
- return $Result
224
227
}
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 ]
228
232
}
229
- return $Value
233
+ return $Result
230
234
}
231
- end {}
232
235
}
233
236
<#
234
237
. SYNOPSIS
235
- Get a state.
238
+ GitHub Actions - Get State
236
239
. DESCRIPTION
237
- Get a state.
240
+ Get state.
238
241
. PARAMETER Name
239
242
Name of the state.
240
243
. PARAMETER Trim
@@ -243,26 +246,37 @@ Trim the state's value.
243
246
function Get-GHActionsState {
244
247
[CmdletBinding ()]
245
248
param (
246
- [Parameter (Mandatory = $true , Position = 0 , ValueFromPipeline = $true )][ValidatePattern ( ' ^.+$ ' )][ string ]$Name ,
249
+ [Parameter (Mandatory = $true , Position = 0 , ValueFromPipeline = $true )][string [] ]$Name ,
247
250
[switch ]$Trim
248
251
)
249
- begin {}
252
+ begin {
253
+ $Result = @ {}
254
+ }
250
255
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
+ }
255
267
}
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 ]
259
272
}
260
- return $Value
273
+ return $Result
261
274
}
262
- end {}
263
275
}
264
276
<#
265
277
. SYNOPSIS
278
+ GitHub Actions - Set Output
279
+ . DESCRIPTION
266
280
Set an output.
267
281
. PARAMETER Name
268
282
Name of the output.
@@ -277,6 +291,27 @@ function Set-GHActionsOutput {
277
291
)
278
292
Write-GHActionsCommand - Command ' set-output' - Message $Value - Properties @ {' name' = $Name }
279
293
}
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
+ }
280
315
function Save-GHActionsState {
281
316
[CmdletBinding ()]
282
317
param (
@@ -482,4 +517,4 @@ function Write-GHActionsWarning {
482
517
}
483
518
end {}
484
519
}
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