Skip to content

Commit dabfcf1

Browse files
committed
Improve add content logic; Improve requirement logic
1 parent 6629672 commit dabfcf1

File tree

9 files changed

+118
-176
lines changed

9 files changed

+118
-176
lines changed

hugoalh.GitHubActionsToolkit/module/artifact.psm1

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,9 @@ Function Export-Artifact {
3939
[Parameter(ValueFromPipelineByPropertyName = $True)][ValidateRange(1, 90)][Alias('RetentionDay')][Byte]$RetentionTime
4040
)
4141
Begin {
42-
[Boolean]$NoOperation = $False# When the requirements are not fulfill, only stop this function but not others.
43-
If (!(Test-GitHubActionsEnvironment -Artifact)) {
42+
[Boolean]$NoOperation = !(Test-GitHubActionsEnvironment -Artifact)# When the requirements are not fulfill, only stop this function but not others.
43+
If ($NoOperation) {
4444
Write-Error -Message 'Unable to get GitHub Actions artifact resources!' -Category 'ResourceUnavailable'
45-
$NoOperation = $True
4645
}
4746
}
4847
Process {
@@ -108,10 +107,9 @@ Function Import-Artifact {
108107
[Parameter(ValueFromPipelineByPropertyName = $True)][Alias('Dest', 'Target')][String]$Destination = $Env:GITHUB_WORKSPACE
109108
)
110109
Begin {
111-
[Boolean]$NoOperation = $False# When the requirements are not fulfill, only stop this function but not others.
112-
If (!(Test-GitHubActionsEnvironment -Artifact)) {
110+
[Boolean]$NoOperation = !(Test-GitHubActionsEnvironment -Artifact)# When the requirements are not fulfill, only stop this function but not others.
111+
If ($NoOperation) {
113112
Write-Error -Message 'Unable to get GitHub Actions artifact resources!' -Category 'ResourceUnavailable'
114-
$NoOperation = $True
115113
}
116114
}
117115
Process {

hugoalh.GitHubActionsToolkit/module/cache.psm1

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,9 @@ Function Restore-Cache {
3939
[Parameter(ValueFromPipelineByPropertyName = $True)][ValidateRange(5, 900)][UInt16]$Timeout
4040
)
4141
Begin {
42-
[Boolean]$NoOperation = $False# When the requirements are not fulfill, only stop this function but not others.
43-
If (!(Test-GitHubActionsEnvironment -Cache)) {
42+
[Boolean]$NoOperation = !(Test-GitHubActionsEnvironment -Cache)# When the requirements are not fulfill, only stop this function but not others.
43+
If ($NoOperation) {
4444
Write-Error -Message 'Unable to get GitHub Actions cache resources!' -Category 'ResourceUnavailable'
45-
$NoOperation = $True
4645
}
4746
}
4847
Process {
@@ -110,10 +109,9 @@ Function Save-Cache {
110109
[Parameter(ValueFromPipelineByPropertyName = $True)][ValidateRange(1, 16)][Alias('Concurrency')][Byte]$UploadConcurrency
111110
)
112111
Begin {
113-
[Boolean]$NoOperation = $False# When the requirements are not fulfill, only stop this function but not others.
114-
If (!(Test-GitHubActionsEnvironment -Cache)) {
112+
[Boolean]$NoOperation = !(Test-GitHubActionsEnvironment -Cache)# When the requirements are not fulfill, only stop this function but not others.
113+
If ($NoOperation) {
115114
Write-Error -Message 'Unable to get GitHub Actions cache resources!' -Category 'ResourceUnavailable'
116-
$NoOperation = $True
117115
}
118116
}
119117
Process {

hugoalh.GitHubActionsToolkit/module/command-base.psm1

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,10 @@ GitHub Actions (Private) - Write File Command
9090
Write file command to communicate with the runner machine.
9191
.PARAMETER LiteralPath
9292
Literal path of the file.
93-
.PARAMETER Table
94-
Table.
93+
.PARAMETER Name
94+
Name.
95+
.PARAMETER Value
96+
Value.
9597
.OUTPUTS
9698
[Void]
9799
#>
@@ -100,25 +102,22 @@ Function Write-FileCommand {
100102
[OutputType([Void])]
101103
Param (
102104
[Parameter(Mandatory = $True, Position = 0, ValueFromPipelineByPropertyName = $True)][String]$LiteralPath,
103-
[Parameter(Mandatory = $True, Position = 1, ValueFromPipelineByPropertyName = $True)][Hashtable]$Table
105+
[Parameter(Mandatory = $True, Position = 1, ValueFromPipelineByPropertyName = $True)][String]$Name,
106+
[Parameter(Mandatory = $True, Position = 2, ValueFromPipelineByPropertyName = $True)][String]$Value
104107
)
105108
Process {
106-
Add-Content -LiteralPath $LiteralPath -Value (
107-
$Table.GetEnumerator() |
108-
ForEach-Object -Process {
109-
If ($_.Value -imatch '^.+$') {
110-
Write-Output -InputObject "$($_.Name)=$($_.Value)"
111-
}
112-
Else {
113-
Do {
114-
[String]$Token = New-GitHubActionsRandomToken -Length 64
115-
}
116-
While ( $Token -iin $GitHubActionsFileCommandTokensUsed )
117-
$Script:GitHubActionsFileCommandTokensUsed += $Token
118-
Write-Output -InputObject "$($_.Name)=<<$Token`n$($_.Value -ireplace '\r?\n', "`n")`n$Token"
119-
}
120-
} |
121-
Join-String -Separator "`n"
109+
Add-Content -LiteralPath $LiteralPath -Value $(
110+
If ($Value -imatch '^.+$') {
111+
Write-Output -InputObject "$($Name)=$($Value)"
112+
}
113+
Else {
114+
Do {
115+
[String]$Token = New-GitHubActionsRandomToken -Length 64
116+
}
117+
While ( $Token -iin $GitHubActionsFileCommandTokensUsed )
118+
$Script:GitHubActionsFileCommandTokensUsed += $Token
119+
Write-Output -InputObject "$Name<<$Token`n$($Value -ireplace '\r?\n', "`n")`n$Token"
120+
}
122121
) -Confirm:$False -Encoding 'UTF8NoBOM'
123122
}
124123
}

hugoalh.GitHubActionsToolkit/module/environment-variable.psm1

Lines changed: 27 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@ Function Add-PATH {
3030
Param (
3131
[Parameter(Mandatory = $True, Position = 0, ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True)][ValidatePattern('^.+$', ErrorMessage = 'Parameter `Path` must be in single line string!')][Alias('Paths')][String[]]$Path,
3232
[Alias('NoValidate', 'SkipValidate', 'SkipValidator')][Switch]$NoValidator,
33-
[GitHubActionsEnvironmentVariableScopes]$Scope = [GitHubActionsEnvironmentVariableScopes]3
33+
[Alias('Scopes')][GitHubActionsEnvironmentVariableScopes]$Scope = [GitHubActionsEnvironmentVariableScopes]3
3434
)
3535
Begin {
36-
[String[]]$Result = @()
36+
[Boolean]$Legacy = [String]::IsNullOrWhiteSpace($Env:GITHUB_PATH)
37+
[String[]]$ScopeArray = $Scope.ToString() -isplit ', '
3738
}
3839
Process {
3940
ForEach ($Item In (
@@ -44,29 +45,20 @@ Function Add-PATH {
4445
Write-Error -Message "``$Item`` is not a valid PATH!" -Category 'SyntaxError'
4546
Continue
4647
}
47-
$Result += $Item
48-
}
49-
}
50-
End {
51-
If ($Result.Count -igt 0) {
52-
Switch -Exact ($Scope.ToString() -isplit ', ') {
48+
Switch -Exact ($ScopeArray) {
5349
'Current' {
5450
[System.Environment]::SetEnvironmentVariable('PATH', (
55-
([System.Environment]::GetEnvironmentVariable('PATH') -isplit [System.IO.Path]::PathSeparator) + $Result |
51+
([System.Environment]::GetEnvironmentVariable('PATH') -isplit [System.IO.Path]::PathSeparator) + $Item |
5652
Join-String -Separator [System.IO.Path]::PathSeparator
5753
)) |
5854
Out-Null
5955
}
6056
'Subsequent' {
61-
If ([String]::IsNullOrWhiteSpace($Env:GITHUB_PATH)) {
62-
$Result |
63-
ForEach-Object -Process { Write-GitHubActionsCommand -Command 'add-path' -Value $_ }
57+
If ($Legacy) {
58+
Write-GitHubActionsCommand -Command 'add-path' -Value $Item
6459
}
6560
Else {
66-
Add-Content -LiteralPath $Env:GITHUB_PATH -Value (
67-
$Result |
68-
Join-String -Separator "`n"
69-
) -Confirm:$False -Encoding 'UTF8NoBOM'
61+
Add-Content -LiteralPath $Env:GITHUB_PATH -Value $Item -Confirm:$False -Encoding 'UTF8NoBOM'
7062
}
7163
}
7264
}
@@ -97,12 +89,13 @@ Function Set-EnvironmentVariable {
9789
Param (
9890
[Parameter(Mandatory = $True, ParameterSetName = 'Multiple', Position = 0, ValueFromPipeline = $True)][Alias('Input', 'Object')][Hashtable]$InputObject,
9991
[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,
100-
[Parameter(Mandatory = $True, ParameterSetName = 'Single', Position = 1, ValueFromPipelineByPropertyName = $True)][ValidatePattern('^.+$', ErrorMessage = 'Parameter `Value` must be in single line string!')][String]$Value,
92+
[Parameter(Mandatory = $True, ParameterSetName = 'Single', Position = 1, ValueFromPipelineByPropertyName = $True)][String]$Value,
10193
[Alias('NoToUppercase')][Switch]$NoToUpper,
102-
[GitHubActionsEnvironmentVariableScopes]$Scope = [GitHubActionsEnvironmentVariableScopes]3
94+
[Alias('Scopes')][GitHubActionsEnvironmentVariableScopes]$Scope = [GitHubActionsEnvironmentVariableScopes]3
10395
)
10496
Begin {
105-
[Hashtable]$Result = @{}
97+
[Boolean]$Legacy = [String]::IsNullOrWhiteSpace($Env:GITHUB_ENV)
98+
[String[]]$ScopeArray = $Scope.ToString() -isplit ', '
10699
}
107100
Process {
108101
Switch ($PSCmdlet.ParameterSetName) {
@@ -120,36 +113,26 @@ Function Set-EnvironmentVariable {
120113
Write-Error -Message 'Parameter `Value` must be type of string!' -Category 'InvalidType'
121114
Continue
122115
}
123-
If ($Item.Value -inotmatch '^.+$') {
124-
Write-Error -Message 'Parameter `Value` must be in single line string!' -Category 'SyntaxError'
125-
Continue
126-
}
127-
$Result[$NoToUpper.IsPresent ? $Item.Name : $Item.Name.ToUpper()] = $Item.Value
116+
[String]$ItemName = $NoToUpper.IsPresent ? $Item.Name : $Item.Name.ToUpper()
117+
[String]$ItemValue = $Item.Value
128118
}
129119
}
130120
'Single' {
131-
$Result[$NoToUpper.IsPresent ? $Name : $Name.ToUpper()] = $Value
121+
[String]$ItemName = $NoToUpper.IsPresent ? $Name : $Name.ToUpper()
122+
[String]$ItemValue = $Value
132123
}
133124
}
134-
}
135-
End {
136-
If ($Result.Count -igt 0) {
137-
Switch -Exact ($Scope.ToString() -isplit ', ') {
138-
'Current' {
139-
$Result.GetEnumerator() |
140-
ForEach-Object -Process {
141-
[System.Environment]::SetEnvironmentVariable($_.Name, $_.Value) |
142-
Out-Null
143-
}
125+
Switch -Exact ($ScopeArray) {
126+
'Current' {
127+
[System.Environment]::SetEnvironmentVariable($ItemName, $ItemValue) |
128+
Out-Null
129+
}
130+
'Subsequent' {
131+
If ($Legacy) {
132+
Write-GitHubActionsCommand -Command 'set-env' -Parameter @{ 'name' = $ItemName } -Value $ItemValue
144133
}
145-
'Subsequent' {
146-
If ([String]::IsNullOrWhiteSpace($Env:GITHUB_ENV)) {
147-
$Result.GetEnumerator() |
148-
ForEach-Object -Process { Write-GitHubActionsCommand -Command 'set-env' -Parameter @{ 'name' = $_.Name } -Value $_.Value }
149-
}
150-
Else {
151-
Write-GitHubActionsFileCommand -LiteralPath $Env:GITHUB_ENV -Table $Result
152-
}
134+
Else {
135+
Write-GitHubActionsFileCommand -LiteralPath $Env:GITHUB_ENV -Name $ItemName -Value $ItemValue
153136
}
154137
}
155138
}
@@ -174,7 +157,7 @@ Function Test-EnvironmentVariableName {
174157
[Parameter(Mandatory = $True, Position = 0, ValueFromPipeline = $True)][Alias('Input', 'Object')][String]$InputObject
175158
)
176159
Process {
177-
$InputObject -imatch '^(?:[\da-z][\da-z_-]*)?[\da-z]$' -and $InputObject -inotmatch '^(?:CI|PATH)$' -and $InputObject -inotmatch '^(?:ACTIONS|GITHUB|RUNNER)_' |
160+
$InputObject -imatch '^(?:[\da-z][\da-z_-]*)?[\da-z]$' -and $InputObject -inotmatch '^(?:CI|PATH)$|^(?:ACTIONS|GITHUB|RUNNER)_' |
178161
Write-Output
179162
}
180163
}

hugoalh.GitHubActionsToolkit/module/internal/token.psm1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#Requires -PSEdition Core
22
#Requires -Version 7.2
3-
[Char[]]$TokenPool = [String[]]@(0..9) + [Char[]]@(97..122)
3+
[Char[]]$Pool = [String[]]@(0..9) + [Char[]]@(97..122)
44
<#
55
.SYNOPSIS
66
GitHub Actions - Internal - New Random Token
@@ -19,7 +19,7 @@ Function New-RandomToken {
1919
)
2020
@(1..$Length) |
2121
ForEach-Object -Process {
22-
$TokenPool |
22+
$Pool |
2323
Get-Random -Count 1
2424
} |
2525
Join-String -Separator '' |

hugoalh.GitHubActionsToolkit/module/open-id-connect.psm1

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,9 @@ Function Get-OpenIdConnectToken {
2828
[Parameter(ValueFromPipelineByPropertyName = $True)][Alias('NodeJs', 'NodeJsWrapper', 'UseNodeJs')][Switch]$UseNodeJsWrapper
2929
)
3030
Begin {
31-
[Boolean]$NoOperation = $False# When the requirements are not fulfill, only stop this function but not others.
32-
If (!(Test-GitHubActionsEnvironment -OpenIDConnect)) {
31+
[Boolean]$NoOperation = !(Test-GitHubActionsEnvironment -OpenIDConnect)# When the requirements are not fulfill, only stop this function but not others.
32+
If ($NoOperation) {
3333
Write-Error -Message 'Unable to get GitHub Actions OpenID Connect (OIDC) resources!' -Category 'ResourceUnavailable'
34-
$NoOperation = $True
3534
}
3635
}
3736
Process {

0 commit comments

Comments
 (0)