Skip to content

Commit 42b8c18

Browse files
committed
20221015A
1 parent 1946d13 commit 42b8c18

File tree

5 files changed

+83
-84
lines changed

5 files changed

+83
-84
lines changed

hugoalh.GitHubActionsToolkit/module/cache.psm1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ Function Restore-Cache {
4646
If ($NoOperation) {
4747
Write-Error -Message 'Unable to get GitHub Actions cache resources!' -Category 'ResourceUnavailable'
4848
}
49-
$Env:SEGMENT_DOWNLOAD_TIMEOUT_MINS = $SegmentTimeout.ToString()
5049
}
5150
Process {
5251
If ($NoOperation) {
@@ -73,6 +72,8 @@ Function Restore-Cache {
7372
$InputObject.Timeout = $Timeout * 1000
7473
}
7574
}
75+
[System.Environment]::SetEnvironmentVariable('SEGMENT_DOWNLOAD_TIMEOUT_MINS', $SegmentTimeout) |
76+
Out-Null
7677
(Invoke-GitHubActionsNodeJsWrapper -Path 'cache\restore.js' -InputObject ([PSCustomObject]$InputObject))?.CacheKey |
7778
Write-Output
7879
}

hugoalh.GitHubActionsToolkit/module/command-base.psm1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ Function Write-FileCommand {
116116
While ( $ItemRaw -imatch [RegEx]::Escape($Token) )
117117
@(
118118
"$Name<<$Token",
119-
$Value -ireplace '\r?\n', "`n",
119+
($Value -ireplace '\r?\n', "`n"),
120120
$Token
121121
) |
122122
Add-Content -LiteralPath $LiteralPath -Confirm:$False -Encoding 'UTF8NoBOM'

hugoalh.GitHubActionsToolkit/module/environment-variable.psm1

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
#Requires -Version 7.2
33
Import-Module -Name (
44
@(
5-
'command-base.psm1'
5+
'command-base.psm1',
6+
'internal\test-parameter-input-object.psm1'
67
) |
78
ForEach-Object -Process { Join-Path -Path $PSScriptRoot -ChildPath $_ }
89
) -Prefix 'GitHubActions' -Scope 'Local'
@@ -80,10 +81,10 @@ Scope of the environment variables.
8081
[Void]
8182
#>
8283
Function Set-EnvironmentVariable {
83-
[CmdletBinding(DefaultParameterSetName = 'Multiple', HelpUri = 'https://github.com/hugoalh-studio/ghactions-toolkit-powershell/wiki/api_function_set-githubactionsenvironmentvariable#Set-GitHubActionsEnvironmentVariable')]
84+
[CmdletBinding(DefaultParameterSetName = 'Single', HelpUri = 'https://github.com/hugoalh-studio/ghactions-toolkit-powershell/wiki/api_function_set-githubactionsenvironmentvariable#Set-GitHubActionsEnvironmentVariable')]
8485
[OutputType([Void])]
8586
Param (
86-
[Parameter(Mandatory = $True, ParameterSetName = 'Multiple', Position = 0, ValueFromPipeline = $True)][Alias('Input', 'Object')][Hashtable]$InputObject,
87+
[Parameter(Mandatory = $True, ParameterSetName = 'Multiple', Position = 0, ValueFromPipeline = $True)][ValidateScript({ Test-GitHubActionsParameterInputObject -InputObject $_ })][Alias('Input', 'Object')]$InputObject,
8788
[Parameter(Mandatory = $True, ParameterSetName = 'Single', Position = 0, ValueFromPipelineByPropertyName = $True)][ValidateScript({ Test-EnvironmentVariableName -InputObject $_ }, ErrorMessage = '`{0}` is not a valid environment variable name!')][Alias('Key')][String]$Name,
8889
[Parameter(Mandatory = $True, ParameterSetName = 'Single', Position = 1, ValueFromPipelineByPropertyName = $True)][String]$Value,
8990
[Parameter(ValueFromPipelineByPropertyName = $True)][Alias('NoToUpperCase')][Switch]$NoToUpper,
@@ -93,42 +94,30 @@ Function Set-EnvironmentVariable {
9394
[Boolean]$Legacy = [String]::IsNullOrWhiteSpace($Env:GITHUB_ENV)
9495
}
9596
Process {
96-
[String[]]$ScopeArray = $Scope.ToString() -isplit ', '
97-
Switch ($PSCmdlet.ParameterSetName) {
98-
'Multiple' {
99-
ForEach ($Item In $InputObject.GetEnumerator()) {
100-
If ($Item.Name.GetType().Name -ine 'String') {
101-
Write-Error -Message 'Parameter `Name` must be type of string!' -Category 'InvalidType'
102-
Continue
103-
}
104-
If (!(Test-EnvironmentVariableName -InputObject $Item.Name)) {
105-
Write-Error -Message "``$($Item.Name)`` is not a valid environment variable name!" -Category 'SyntaxError'
106-
Continue
107-
}
108-
If ($Item.Value.GetType().Name -ine 'String') {
109-
Write-Error -Message 'Parameter `Value` must be type of string!' -Category 'InvalidType'
110-
Continue
111-
}
112-
[String]$ItemName = $NoToUpper.IsPresent ? $Item.Name : $Item.Name.ToUpper()
113-
[String]$ItemValue = $Item.Value
114-
}
115-
}
116-
'Single' {
117-
[String]$ItemName = $NoToUpper.IsPresent ? $Name : $Name.ToUpper()
118-
[String]$ItemValue = $Value
97+
If ($PSCmdlet.ParameterSetName -ieq 'Multiple') {
98+
If (
99+
$InputObject -is [Hashtable] -or
100+
$InputObject -is [System.Collections.Specialized.OrderedDictionary]
101+
) {
102+
$InputObject.GetEnumerator() |
103+
Set-EnvironmentVariable -NoToUpper:$NoToUpper.IsPresent -Scope $Scope
104+
Return
119105
}
106+
$InputObject |
107+
Set-EnvironmentVariable -NoToUpper:$NoToUpper.IsPresent -Scope $Scope
108+
Return
120109
}
121-
Switch -Exact ($ScopeArray) {
110+
Switch -Exact ($Scope.ToString() -isplit ', ') {
122111
'Current' {
123-
[System.Environment]::SetEnvironmentVariable($ItemName, $ItemValue) |
112+
[System.Environment]::SetEnvironmentVariable($Name, $Value) |
124113
Out-Null
125114
}
126115
'Subsequent' {
127116
If ($Legacy) {
128-
Write-GitHubActionsCommand -Command 'set-env' -Parameter @{ 'name' = $ItemName } -Value $ItemValue
117+
Write-GitHubActionsCommand -Command 'set-env' -Parameter @{ 'name' = $Name } -Value $Value
129118
}
130119
Else {
131-
Write-GitHubActionsFileCommand -LiteralPath $Env:GITHUB_ENV -Name $ItemName -Value $ItemValue
120+
Write-GitHubActionsFileCommand -LiteralPath $Env:GITHUB_ENV -Name $Name -Value $Value
132121
}
133122
}
134123
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#Requires -PSEdition Core
2+
#Requires -Version 7.2
3+
<#
4+
.SYNOPSIS
5+
GitHub Actions - Internal - Test Parameter Input Object
6+
.DESCRIPTION
7+
Test the parameter input object whether is valid.
8+
.PARAMETER InputObject
9+
Parameter input object that need to test.
10+
.OUTPUTS
11+
[Boolean] Test result.
12+
#>
13+
Function Test-ParameterInputObject {
14+
[CmdletBinding()]
15+
[OutputType([Boolean])]
16+
Param (
17+
[Parameter(Mandatory = $True, Position = 0, ValueFromPipeline = $True)][Alias('Input', 'Object')]$InputObject
18+
)
19+
Process {
20+
(
21+
$InputObject -is [Hashtable] -or
22+
$InputObject -is [Object[]] -or
23+
$InputObject -is [System.Collections.Specialized.OrderedDictionary]
24+
) |
25+
Write-Output
26+
}
27+
}
28+
Export-ModuleMember -Function @(
29+
'Test-ParameterInputObject'
30+
)

hugoalh.GitHubActionsToolkit/module/parameter.psm1

Lines changed: 31 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Import-Module -Name (
44
@(
55
'command-base.psm1',
6+
'internal\test-parameter-input-object.psm1',
67
'log.psm1'
78
) |
89
ForEach-Object -Process { Join-Path -Path $PSScriptRoot -ChildPath $_ }
@@ -167,46 +168,35 @@ Value of the output.
167168
[Void]
168169
#>
169170
Function Set-Output {
170-
[CmdletBinding(DefaultParameterSetName = 'Multiple', HelpUri = 'https://github.com/hugoalh-studio/ghactions-toolkit-powershell/wiki/api_function_set-githubactionsoutput#Set-GitHubActionsOutput')]
171+
[CmdletBinding(DefaultParameterSetName = 'Single', HelpUri = 'https://github.com/hugoalh-studio/ghactions-toolkit-powershell/wiki/api_function_set-githubactionsoutput#Set-GitHubActionsOutput')]
171172
[OutputType([Void])]
172173
Param (
173-
[Parameter(Mandatory = $True, ParameterSetName = 'Multiple', Position = 0, ValueFromPipeline = $True)][Alias('Input', 'Object')][Hashtable]$InputObject,
174+
[Parameter(Mandatory = $True, ParameterSetName = 'Multiple', Position = 0, ValueFromPipeline = $True)][ValidateScript({ Test-GitHubActionsParameterInputObject -InputObject $_ })][Alias('Input', 'Object')]$InputObject,
174175
[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,
175176
[Parameter(Mandatory = $True, ParameterSetName = 'Single', Position = 1, ValueFromPipelineByPropertyName = $True)][AllowEmptyString()][String]$Value
176177
)
177178
Begin {
178179
[Boolean]$Legacy = [String]::IsNullOrWhiteSpace($Env:GITHUB_OUTPUT)
179180
}
180181
Process {
181-
Switch ($PSCmdlet.ParameterSetName) {
182-
'Multiple' {
183-
ForEach ($Item In $InputObject.GetEnumerator()) {
184-
If ($Item.Name.GetType().Name -ine 'String') {
185-
Write-Error -Message 'Parameter `Name` must be type of string!' -Category 'InvalidType'
186-
Return
187-
}
188-
If ($Item.Name -inotmatch '^(?:[\da-z][\da-z_-]*)?[\da-z]$') {
189-
Write-Error -Message "``$($Item.Name)`` is not a valid GitHub Actions output name!" -Category 'SyntaxError'
190-
Return
191-
}
192-
If ($Item.Value.GetType().Name -ine 'String') {
193-
Write-Error -Message 'Parameter `Value` must be type of string!' -Category 'InvalidType'
194-
Return
195-
}
196-
[String]$ItemName = $Item.Name
197-
[String]$ItemValue = $Item.Value
198-
}
199-
}
200-
'Single' {
201-
[String]$ItemName = $Name
202-
[String]$ItemValue = $Value
182+
If ($PSCmdlet.ParameterSetName -ieq 'Multiple') {
183+
If (
184+
$InputObject -is [Hashtable] -or
185+
$InputObject -is [System.Collections.Specialized.OrderedDictionary]
186+
) {
187+
$InputObject.GetEnumerator() |
188+
Set-Output
189+
Return
203190
}
191+
$InputObject |
192+
Set-Output
193+
Return
204194
}
205195
If ($Legacy) {
206-
Write-GitHubActionsCommand -Command 'set-output' -Parameter @{ 'name' = $ItemName } -Value $ItemValue
196+
Write-GitHubActionsCommand -Command 'set-output' -Parameter @{ 'name' = $Name } -Value $Value
207197
}
208198
Else {
209-
Write-GitHubActionsFileCommand -LiteralPath $Env:GITHUB_OUTPUT -Name $ItemName -Value $ItemValue
199+
Write-GitHubActionsFileCommand -LiteralPath $Env:GITHUB_OUTPUT -Name $Name -Value $Value
210200
}
211201
}
212202
}
@@ -225,46 +215,35 @@ Value of the state.
225215
[Void]
226216
#>
227217
Function Set-State {
228-
[CmdletBinding(DefaultParameterSetName = 'Multiple', HelpUri = 'https://github.com/hugoalh-studio/ghactions-toolkit-powershell/wiki/api_function_set-githubactionsstate#Set-GitHubActionsState')]
218+
[CmdletBinding(DefaultParameterSetName = 'Single', HelpUri = 'https://github.com/hugoalh-studio/ghactions-toolkit-powershell/wiki/api_function_set-githubactionsstate#Set-GitHubActionsState')]
229219
[OutputType([Void])]
230220
Param (
231-
[Parameter(Mandatory = $True, ParameterSetName = 'Multiple', Position = 0, ValueFromPipeline = $True)][Alias('Input', 'Object')][Hashtable]$InputObject,
221+
[Parameter(Mandatory = $True, ParameterSetName = 'Multiple', Position = 0, ValueFromPipeline = $True)][ValidateScript({ Test-GitHubActionsParameterInputObject -InputObject $_ })][Alias('Input', 'Object')]$InputObject,
232222
[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,
233223
[Parameter(Mandatory = $True, ParameterSetName = 'Single', Position = 1, ValueFromPipelineByPropertyName = $True)][AllowEmptyString()][String]$Value
234224
)
235225
Begin {
236226
[Boolean]$Legacy = [String]::IsNullOrWhiteSpace($Env:GITHUB_STATE)
237227
}
238228
Process {
239-
Switch ($PSCmdlet.ParameterSetName) {
240-
'Multiple' {
241-
ForEach ($Item In $InputObject.GetEnumerator()) {
242-
If ($Item.Name.GetType().Name -ine 'String') {
243-
Write-Error -Message 'Parameter `Name` must be type of string!' -Category 'InvalidType'
244-
Return
245-
}
246-
If ($Item.Name -inotmatch '^(?:[\da-z][\da-z_-]*)?[\da-z]$') {
247-
Write-Error -Message "``$($Item.Name)`` is not a valid GitHub Actions state name!" -Category 'SyntaxError'
248-
Return
249-
}
250-
If ($Item.Value.GetType().Name -ine 'String') {
251-
Write-Error -Message 'Parameter `Value` must be type of string!' -Category 'InvalidType'
252-
Return
253-
}
254-
[String]$ItemName = $Item.Name
255-
[String]$ItemValue = $Item.Value
256-
}
257-
}
258-
'Single' {
259-
[String]$ItemName = $Name
260-
[String]$ItemValue = $Value
229+
If ($PSCmdlet.ParameterSetName -ieq 'Multiple') {
230+
If (
231+
$InputObject -is [Hashtable] -or
232+
$InputObject -is [System.Collections.Specialized.OrderedDictionary]
233+
) {
234+
$InputObject.GetEnumerator() |
235+
Set-State
236+
Return
261237
}
238+
$InputObject |
239+
Set-State
240+
Return
262241
}
263242
If ($Legacy) {
264-
Write-GitHubActionsCommand -Command 'save-state' -Parameter @{ 'name' = $ItemName } -Value $ItemValue
243+
Write-GitHubActionsCommand -Command 'save-state' -Parameter @{ 'name' = $Name } -Value $Value
265244
}
266245
Else {
267-
Write-GitHubActionsFileCommand -LiteralPath $Env:GITHUB_STATE -Name $ItemName -Value $ItemValue
246+
Write-GitHubActionsFileCommand -LiteralPath $Env:GITHUB_STATE -Name $Name -Value $Value
268247
}
269248
}
270249
}

0 commit comments

Comments
 (0)