Skip to content

Commit 311d839

Browse files
committed
20220126A
1 parent 971b530 commit 311d839

File tree

1 file changed

+75
-50
lines changed

1 file changed

+75
-50
lines changed

hugoalh.GitHubActionsToolkit/hugoalh.GitHubActionsToolkit.psm1

Lines changed: 75 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,6 @@ function Format-GHActionsCommand {
4040
}
4141
<#
4242
.SYNOPSIS
43-
GitHub Actions - Internal - Get All Environment Variable
44-
.DESCRIPTION
45-
An internal function to get all environment variable.
46-
.OUTPUTS
47-
Hashtable
48-
#>
49-
function Get-AllEnvironmentVariable {
50-
[CmdletBinding()][OutputType([hashtable])]
51-
param ()
52-
[hashtable]$Result = @{}
53-
Get-ChildItem -Path 'Env:\' | ForEach-Object -Process {
54-
$Result[$_.Name] = $_.Value
55-
}
56-
return $Result
57-
}
58-
<#
59-
.SYNOPSIS
6043
GitHub Actions - Internal - Write Workflow Command
6144
.DESCRIPTION
6245
An internal function to write workflow command.
@@ -100,31 +83,37 @@ Environment variable value.
10083
Void
10184
#>
10285
function Add-GHActionsEnvironmentVariable {
103-
[CmdletBinding(DefaultParameterSetName = '1')][OutputType([void])]
86+
[CmdletBinding(DefaultParameterSetName = 'multiple')][OutputType([void])]
10487
param(
105-
[Parameter(Mandatory = $true, ParameterSetName = '1', Position = 0, ValueFromPipeline = $true)][Alias('Input', 'Object')][hashtable]$InputObject,
106-
[Parameter(Mandatory = $true, ParameterSetName = '2', Position = 0)][ValidatePattern('^[\da-z_]+$')][Alias('Key')][string]$Name,
107-
[Parameter(Mandatory = $true, ParameterSetName = '2', Position = 1)][ValidatePattern('^.+$')][string]$Value
88+
[Parameter(Mandatory = $true, ParameterSetName = 'multiple', Position = 0, ValueFromPipeline = $true)][Alias('Input', 'Object')][hashtable]$InputObject,
89+
[Parameter(Mandatory = $true, ParameterSetName = 'single', Position = 0)][ValidatePattern('^[\da-z_]+$')][Alias('Key')][string]$Name,
90+
[Parameter(Mandatory = $true, ParameterSetName = 'single', Position = 1)][ValidatePattern('^.+$')][string]$Value
10891
)
10992
begin {
11093
[hashtable]$Result = @{}
11194
}
11295
process {
11396
switch ($PSCmdlet.ParameterSetName) {
114-
'1' { $InputObject.GetEnumerator() | ForEach-Object -Process {
115-
if ($_.Name.GetType().Name -ne 'string') {
116-
Write-Error -Message "Input name `"$($_.Name)`" must be type of string!" -Category InvalidType
117-
} elseif ($_.Name -notmatch '^[\da-z_]+$') {
118-
Write-Error -Message "Input name `"$($_.Name)`" is not match the require pattern!" -Category SyntaxError
119-
} elseif ($_.Value.GetType().Name -ne 'string') {
120-
Write-Error -Message "Input value `"$($_.Value)`" must be type of string!" -Category InvalidType
121-
} elseif ($_.Value -notmatch '^.+$') {
122-
Write-Error -Message "Input value `"$($_.Value)`" is not match the require pattern!" -Category SyntaxError
123-
} else {
124-
$Result[$_.Name] = $_.Value
97+
'multiple' {
98+
$InputObject.GetEnumerator() | ForEach-Object -Process {
99+
if ($_.Name.GetType().Name -ne 'string') {
100+
Write-Error -Message "Input name `"$($_.Name)`" must be type of string!" -Category InvalidType
101+
} elseif ($_.Name -notmatch '^[\da-z_]+$') {
102+
Write-Error -Message "Input name `"$($_.Name)`" is not match the require pattern!" -Category SyntaxError
103+
} elseif ($_.Value.GetType().Name -ne 'string') {
104+
Write-Error -Message "Input value `"$($_.Value)`" must be type of string!" -Category InvalidType
105+
} elseif ($_.Value -notmatch '^.+$') {
106+
Write-Error -Message "Input value `"$($_.Value)`" is not match the require pattern!" -Category SyntaxError
107+
} else {
108+
$Result[$_.Name] = $_.Value
109+
}
125110
}
126-
}; break }
127-
'2' { $Result[$Name] = $Value; break }
111+
break
112+
}
113+
'single' {
114+
$Result[$Name] = $Value
115+
break
116+
}
128117
}
129118
}
130119
end {
@@ -182,7 +171,8 @@ function Add-GHActionsProblemMatcher {
182171
)
183172
begin {}
184173
process {
185-
Resolve-Path -Path $Path -Relative | ForEach-Object -Process {
174+
[string[]]$PathResolve = Resolve-Path -Path $Path -Relative
175+
$PathResolve | ForEach-Object -Process {
186176
if ((Test-Path -Path $_ -PathType Leaf) -and ((Split-Path -Path $_ -Extension) -eq '.json')) {
187177
Write-GHActionsCommand -Command 'add-matcher' -Message ($_ -replace '^\.\\', '' -replace '\\', '/')
188178
} else {
@@ -332,40 +322,75 @@ Get input.
332322
Name of the input.
333323
.PARAMETER Require
334324
Whether the input is require. If required and not present, will throw an error.
325+
.PARAMETER All
326+
Get all of the input.
335327
.PARAMETER Trim
336328
Trim the input's value.
337329
.OUTPUTS
338330
Hashtable | String
339331
#>
340332
function Get-GHActionsInput {
341-
[CmdletBinding()][OutputType([hashtable], [string])]
333+
[CmdletBinding(DefaultParameterSetName = 'select')][OutputType([hashtable], [string])]
342334
param(
343-
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)][Alias('Key', 'Keys', 'Names')][string[]]$Name,
344-
[Alias('Required')][switch]$Require,
335+
[Parameter(Mandatory = $true, ParameterSetName = 'select', Position = 0, ValueFromPipeline = $true)][SupportsWildcards()][ValidatePattern('^.+$')][Alias('Key', 'Keys', 'Names')][string[]]$Name,
336+
[Parameter(ParameterSetName = 'select')][Alias('Required')][switch]$Require,
337+
[Parameter(ParameterSetName = 'all')][switch]$All,
345338
[switch]$Trim
346339
)
347340
begin {
348341
[hashtable]$Result = @{}
342+
[bool]$ResultIsHashtable = $false
349343
}
350344
process {
351-
$Name | ForEach-Object -Process {
352-
$InputValue = Get-ChildItem -Path "Env:\INPUT_$($_.ToUpper() -replace '[ \n\r\s\t]+','_')" -ErrorAction SilentlyContinue
353-
if ($null -eq $InputValue) {
354-
if ($Require) {
355-
throw "Input ``$_`` is not defined!"
345+
switch ($PSCmdlet.ParameterSetName) {
346+
'all' {
347+
$ResultIsHashtable = $true
348+
[string[]]$NameResolve = Get-ChildItem -Path 'Env:\' -Include 'INPUT_*'
349+
$NameResolve | ForEach-Object -Process {
350+
$InputValue = Get-ChildItem -Path "Env:\INPUT_$_"
351+
if ($Trim) {
352+
$Result[$_] = $InputValue.Value.Trim()
353+
} else {
354+
$Result[$_] = $InputValue.Value
355+
}
356356
}
357-
$Result[$_] = $InputValue
358-
} else {
359-
if ($Trim) {
360-
$Result[$_] = $InputValue.Value.Trim()
361-
} else {
362-
$Result[$_] = $InputValue.Value
357+
break
358+
}
359+
'select' {
360+
$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_$_"
366+
if ($Trim) {
367+
$Result[$_] = $InputValue.Value.Trim()
368+
} else {
369+
$Result[$_] = $InputValue.Value
370+
}
371+
}
372+
} else {
373+
$InputValue = Get-ChildItem -Path "Env:\INPUT_$_" -ErrorAction SilentlyContinue
374+
if ($null -eq $InputValue) {
375+
if ($Require) {
376+
throw "Input ``$_`` is not defined!"
377+
}
378+
$Result[$_] = $InputValue
379+
} else {
380+
if ($Trim) {
381+
$Result[$_] = $InputValue.Value.Trim()
382+
} else {
383+
$Result[$_] = $InputValue.Value
384+
}
385+
}
386+
}
363387
}
388+
break
364389
}
365390
}
366391
}
367392
end {
368-
if ($Result.Count -eq 1) {
393+
if (($ResultIsHashtable -eq $false) -and ($Result.Count -eq 1)) {
369394
return $Result.Values[0]
370395
}
371396
return $Result

0 commit comments

Comments
 (0)