Skip to content

Commit eec539f

Browse files
committed
20231121B
1 parent 4c34ca9 commit eec539f

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

hugoalh.GitHubActionsToolkit/module/environment-variable.psm1

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ Add PATH for the current step and/or all of the subsequent steps in the current
1515
Absolute paths.
1616
.PARAMETER Scope
1717
Scope of the PATHs.
18+
.PARAMETER Optimize
19+
Whether to have an optimize operation by replace exist command instead of add command directly.
1820
.OUTPUTS
1921
[Void]
2022
#>
@@ -23,7 +25,8 @@ Function Add-PATH {
2325
[OutputType([Void])]
2426
Param (
2527
[Parameter(Mandatory = $True, Position = 0, ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True)][ValidateScript({ [System.IO.Path]::IsPathFullyQualified($_) }, ErrorMessage = '`{0}` is not a valid absolute path!')][Alias('Paths')][String[]]$Path,
26-
[Parameter(ValueFromPipelineByPropertyName = $True)][Alias('Scopes')][GitHubActionsEnvironmentVariableScopes]$Scope = [GitHubActionsEnvironmentVariableScopes]3
28+
[Parameter(ValueFromPipelineByPropertyName = $True)][Alias('Scopes')][GitHubActionsEnvironmentVariableScopes]$Scope = [GitHubActionsEnvironmentVariableScopes]3,
29+
[Parameter(ValueFromPipelineByPropertyName = $True)][Switch]$Optimize
2730
)
2831
Begin {
2932
[Boolean]$ShouldProceed = $True
@@ -56,10 +59,15 @@ Function Add-PATH {
5659
}
5760
If (($Scope -band ([GitHubActionsEnvironmentVariableScopes]::Subsequent)) -ieq ([GitHubActionsEnvironmentVariableScopes]::Subsequent)) {
5861
Try {
59-
(Get-Content -LiteralPath $Env:GITHUB_PATH -Encoding 'UTF8NoBOM') + $Path |
60-
Where-Object -FilterScript { $_.Length -gt 0 } |
61-
Select-Object -Unique |
62-
Set-Content -LiteralPath $Env:GITHUB_PATH -Confirm:$False -Encoding 'UTF8NoBOM'
62+
If ($Optimize.IsPresent) {
63+
(Get-Content -LiteralPath $Env:GITHUB_PATH -Encoding 'UTF8NoBOM') + $Path |
64+
Where-Object -FilterScript { $_.Length -gt 0 } |
65+
Select-Object -Unique |
66+
Set-Content -LiteralPath $Env:GITHUB_PATH -Confirm:$False -Encoding 'UTF8NoBOM'
67+
}
68+
Else {
69+
Add-Content -LiteralPath $Env:GITHUB_PATH -Value $Path -Confirm:$False -Encoding 'UTF8NoBOM'
70+
}
6371
}
6472
Catch {
6573
Write-Error -Message "Unable to add the GitHub Actions PATH: $_" -Category (($_)?.CategoryInfo.Category ?? 'OperationStopped')

0 commit comments

Comments
 (0)