Skip to content

Commit dc46a13

Browse files
committed
20220126B
1 parent 311d839 commit dc46a13

File tree

1 file changed

+96
-54
lines changed

1 file changed

+96
-54
lines changed

hugoalh.GitHubActionsToolkit/hugoalh.GitHubActionsToolkit.psm1

Lines changed: 96 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ Void
136136
function Add-GHActionsPATH {
137137
[CmdletBinding()][OutputType([void])]
138138
param(
139-
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)][Alias('Paths')][string[]]$Path
139+
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)][ValidatePattern('^.+$')][Alias('Paths')][string[]]$Path
140140
)
141141
begin {
142142
[string[]]$Result = @()
@@ -167,7 +167,7 @@ Void
167167
function Add-GHActionsProblemMatcher {
168168
[CmdletBinding()][OutputType([void])]
169169
param (
170-
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)][SupportsWildcards()][Alias('File', 'Files', 'Paths', 'PSPath', 'PSPaths')][string[]]$Path
170+
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)][SupportsWildcards()][ValidatePattern('^.+$')][Alias('File', 'Files', 'Paths', 'PSPath', 'PSPaths')][string[]]$Path
171171
)
172172
begin {}
173173
process {
@@ -204,8 +204,8 @@ function Add-GHActionsSecretMask {
204204
process {
205205
Write-GHActionsCommand -Command 'add-mask' -Message $Value
206206
if ($Smart) {
207-
[string[]]$Bin = $Value -split "[\n\r\s\t]+"
208-
$Bin | ForEach-Object -Process {
207+
[string[]]$ValueChunk = $Value -split "[\n\r\s\t]+"
208+
$ValueChunk | ForEach-Object -Process {
209209
if (($_ -ne $Value) -and ($_.Length -ge 2)) {
210210
Write-GHActionsCommand -Command 'add-mask' -Message $_
211211
}
@@ -345,9 +345,8 @@ function Get-GHActionsInput {
345345
switch ($PSCmdlet.ParameterSetName) {
346346
'all' {
347347
$ResultIsHashtable = $true
348-
[string[]]$NameResolve = Get-ChildItem -Path 'Env:\' -Include 'INPUT_*'
349-
$NameResolve | ForEach-Object -Process {
350-
$InputValue = Get-ChildItem -Path "Env:\INPUT_$_"
348+
Get-ChildItem -Path 'Env:\' -Include 'INPUT_*' -Name | ForEach-Object -Process {
349+
$InputValue = Get-ChildItem -Path "Env:\$_"
351350
if ($Trim) {
352351
$Result[$_] = $InputValue.Value.Trim()
353352
} else {
@@ -358,11 +357,10 @@ function Get-GHActionsInput {
358357
}
359358
'select' {
360359
$Name | ForEach-Object -Process {
361-
if ([WildcardPattern]::ContainsWildcardCharacters($Name)) {
362-
$Function:ResultIsHashtable = $true
363-
[string[]]$NameResolve = Get-ChildItem -Path 'Env:\' -Include "INPUT_$Name"
364-
$NameResolve | ForEach-Object -Process {
365-
$InputValue = Get-ChildItem -Path "Env:\INPUT_$_"
360+
if ([WildcardPattern]::ContainsWildcardCharacters($_)) {
361+
$ResultIsHashtable = $true
362+
Get-ChildItem -Path 'Env:\' -Include "INPUT_$_" -Name | ForEach-Object -Process {
363+
$InputValue = Get-ChildItem -Path "Env:$_"
366364
if ($Trim) {
367365
$Result[$_] = $InputValue.Value.Trim()
368366
} else {
@@ -419,30 +417,62 @@ GitHub Actions - Get State
419417
Get state.
420418
.PARAMETER Name
421419
Name of the state.
420+
.PARAMETER All
421+
Get all of the state.
422422
.PARAMETER Trim
423423
Trim the state's value.
424424
.OUTPUTS
425425
Hashtable | String
426426
#>
427427
function Get-GHActionsState {
428-
[CmdletBinding()][OutputType([hashtable], [string])]
428+
[CmdletBinding(DefaultParameterSetName = 'select')][OutputType([hashtable], [string])]
429429
param(
430-
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)][Alias('Key', 'Keys', 'Names')][string[]]$Name,
430+
[Parameter(Mandatory = $true, ParameterSetName = 'select', Position = 0, ValueFromPipeline = $true)][SupportsWildcards()][ValidatePattern('^.+$')][Alias('Key', 'Keys', 'Names')][string[]]$Name,
431+
[Parameter(ParameterSetName = 'all')][switch]$All,
431432
[switch]$Trim
432433
)
433434
begin {
434435
[hashtable]$Result = @{}
436+
[bool]$ResultIsHashtable = $false
435437
}
436438
process {
437-
$Name | ForEach-Object -Process {
438-
$StateValue = Get-ChildItem -Path "Env:\STATE_$($_.ToUpper() -replace '[ \n\r\s\t]+','_')" -ErrorAction SilentlyContinue
439-
if ($null -eq $StateValue) {
440-
$Result[$_] = $StateValue
441-
} else {
442-
if ($Trim) {
443-
$Result[$_] = $StateValue.Value.Trim()
444-
} else {
445-
$Result[$_] = $StateValue.Value
439+
switch ($PSCmdlet.ParameterSetName) {
440+
'all' {
441+
$ResultIsHashtable = $true
442+
Get-ChildItem -Path 'Env:\' -Include 'STATE_*' -Name | ForEach-Object -Process {
443+
$StateValue = Get-ChildItem -Path "Env:\$_"
444+
if ($Trim) {
445+
$Result[$_] = $StateValue.Value.Trim()
446+
} else {
447+
$Result[$_] = $StateValue.Value
448+
}
449+
}
450+
break
451+
}
452+
'select' {
453+
$Name | ForEach-Object -Process {
454+
if ([WildcardPattern]::ContainsWildcardCharacters($_)) {
455+
$ResultIsHashtable = $true
456+
Get-ChildItem -Path 'Env:\' -Include "STATE_$_" -Name | ForEach-Object -Process {
457+
$StateValue = Get-ChildItem -Path "Env:$_"
458+
if ($Trim) {
459+
$Result[$_] = $StateValue.Value.Trim()
460+
} else {
461+
$Result[$_] = $StateValue.Value
462+
}
463+
}
464+
} else {
465+
$StateValue = Get-ChildItem -Path "Env:\STATE_$_" -ErrorAction SilentlyContinue
466+
if ($null -eq $StateValue) {
467+
$Result[$_] = $StateValue
468+
} else {
469+
if ($Trim) {
470+
$Result[$_] = $StateValue.Value.Trim()
471+
} else {
472+
$Result[$_] = $StateValue.Value
473+
}
474+
}
475+
}
446476
}
447477
}
448478
}
@@ -493,7 +523,7 @@ Void
493523
function Remove-GHActionsProblemMatcher {
494524
[CmdletBinding()][OutputType([void])]
495525
param (
496-
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)][Alias('Identifies', 'Identify', 'Identifier', 'Identifiers', 'Key', 'Keys', 'Name', 'Names', 'Owners')][string[]]$Owner
526+
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)][ValidatePattern('^.+$')][Alias('Identifies', 'Identify', 'Identifier', 'Identifiers', 'Key', 'Keys', 'Name', 'Names', 'Owners')][string[]]$Owner
497527
)
498528
begin {}
499529
process {
@@ -516,27 +546,33 @@ Value of the output.
516546
Void
517547
#>
518548
function Set-GHActionsOutput {
519-
[CmdletBinding(DefaultParameterSetName = '1')][OutputType([void])]
549+
[CmdletBinding(DefaultParameterSetName = 'multiple')][OutputType([void])]
520550
param(
521-
[Parameter(Mandatory = $true, ParameterSetName = '1', Position = 0, ValueFromPipeline = $true)][Alias('Input', 'Object')][hashtable]$InputObject,
522-
[Parameter(Mandatory = $true, ParameterSetName = '2', Position = 0)][ValidatePattern('^.+$')][Alias('Key')][string]$Name,
523-
[Parameter(Mandatory = $true, ParameterSetName = '2', Position = 1)][string]$Value
551+
[Parameter(Mandatory = $true, ParameterSetName = 'multiple', Position = 0, ValueFromPipeline = $true)][Alias('Input', 'Object')][hashtable]$InputObject,
552+
[Parameter(Mandatory = $true, ParameterSetName = 'single', Position = 0)][ValidatePattern('^.+$')][Alias('Key')][string]$Name,
553+
[Parameter(Mandatory = $true, ParameterSetName = 'single', Position = 1)][string]$Value
524554
)
525555
begin {}
526556
process {
527557
switch ($PSCmdlet.ParameterSetName) {
528-
'1' { $InputObject.GetEnumerator() | ForEach-Object -Process {
529-
if ($_.Name.GetType().Name -ne 'string') {
530-
Write-Error -Message "Input name `"$($_.Name)`" must be type of string!" -Category InvalidType
531-
} elseif ($_.Name -notmatch '^.+$') {
532-
Write-Error -Message "Input name `"$($_.Name)`" is not match the require pattern!" -Category SyntaxError
533-
} elseif ($_.Value.GetType().Name -ne 'string') {
534-
Write-Error -Message "Input value `"$($_.Value)`" must be type of string!" -Category InvalidType
535-
} else {
536-
Write-GHActionsCommand -Command 'set-output' -Message $_.Value -Property @{ 'name' = $_.Name }
558+
'multiple' {
559+
$InputObject.GetEnumerator() | ForEach-Object -Process {
560+
if ($_.Name.GetType().Name -ne 'string') {
561+
Write-Error -Message "Input name `"$($_.Name)`" must be type of string!" -Category InvalidType
562+
} elseif ($_.Name -notmatch '^.+$') {
563+
Write-Error -Message "Input name `"$($_.Name)`" is not match the require pattern!" -Category SyntaxError
564+
} elseif ($_.Value.GetType().Name -ne 'string') {
565+
Write-Error -Message "Input value `"$($_.Value)`" must be type of string!" -Category InvalidType
566+
} else {
567+
Write-GHActionsCommand -Command 'set-output' -Message $_.Value -Property @{ 'name' = $_.Name }
568+
}
537569
}
538-
}; break }
539-
'2' { Write-GHActionsCommand -Command 'set-output' -Message $Value -Property @{ 'name' = $Name }; break }
570+
break
571+
}
572+
'single' {
573+
Write-GHActionsCommand -Command 'set-output' -Message $Value -Property @{ 'name' = $Name }
574+
break
575+
}
540576
}
541577
}
542578
end {}
@@ -554,27 +590,33 @@ Value of the state.
554590
Void
555591
#>
556592
function Set-GHActionsState {
557-
[CmdletBinding(DefaultParameterSetName = '1')][OutputType([void])]
593+
[CmdletBinding(DefaultParameterSetName = 'multiple')][OutputType([void])]
558594
param(
559-
[Parameter(Mandatory = $true, ParameterSetName = '1', Position = 0, ValueFromPipeline = $true)][Alias('Input', 'Object')][hashtable]$InputObject,
560-
[Parameter(Mandatory = $true, ParameterSetName = '2', Position = 0)][ValidatePattern('^.+$')][Alias('Key')][string]$Name,
561-
[Parameter(Mandatory = $true, ParameterSetName = '2', Position = 1)][string]$Value
595+
[Parameter(Mandatory = $true, ParameterSetName = 'multiple', Position = 0, ValueFromPipeline = $true)][Alias('Input', 'Object')][hashtable]$InputObject,
596+
[Parameter(Mandatory = $true, ParameterSetName = 'single', Position = 0)][ValidatePattern('^.+$')][Alias('Key')][string]$Name,
597+
[Parameter(Mandatory = $true, ParameterSetName = 'single', Position = 1)][string]$Value
562598
)
563599
begin {}
564600
process {
565601
switch ($PSCmdlet.ParameterSetName) {
566-
'1' { $InputObject.GetEnumerator() | ForEach-Object -Process {
567-
if ($_.Name.GetType().Name -ne 'string') {
568-
Write-Error -Message "Input name `"$($_.Name)`" must be type of string!" -Category InvalidType
569-
} elseif ($_.Name -notmatch '^.+$') {
570-
Write-Error -Message "Input name `"$($_.Name)`" is not match the require pattern!" -Category SyntaxError
571-
} elseif ($_.Value.GetType().Name -ne 'string') {
572-
Write-Error -Message "Input value `"$($_.Value)`" must be type of string!" -Category InvalidType
573-
} else {
574-
Write-GHActionsCommand -Command 'save-state' -Message $_.Value -Property @{ 'name' = $_.Name }
602+
'multiple' {
603+
$InputObject.GetEnumerator() | ForEach-Object -Process {
604+
if ($_.Name.GetType().Name -ne 'string') {
605+
Write-Error -Message "Input name `"$($_.Name)`" must be type of string!" -Category InvalidType
606+
} elseif ($_.Name -notmatch '^.+$') {
607+
Write-Error -Message "Input name `"$($_.Name)`" is not match the require pattern!" -Category SyntaxError
608+
} elseif ($_.Value.GetType().Name -ne 'string') {
609+
Write-Error -Message "Input value `"$($_.Value)`" must be type of string!" -Category InvalidType
610+
} else {
611+
Write-GHActionsCommand -Command 'save-state' -Message $_.Value -Property @{ 'name' = $_.Name }
612+
}
575613
}
576-
}; break }
577-
'2' { Write-GHActionsCommand -Command 'save-state' -Message $Value -Property @{ 'name' = $Name }; break }
614+
break
615+
}
616+
'single' {
617+
Write-GHActionsCommand -Command 'save-state' -Message $Value -Property @{ 'name' = $Name }
618+
break
619+
}
578620
}
579621
}
580622
end {}

0 commit comments

Comments
 (0)