@@ -30,10 +30,11 @@ Function Add-PATH {
30
30
Param (
31
31
[Parameter (Mandatory = $True , Position = 0 , ValueFromPipeline = $True , ValueFromPipelineByPropertyName = $True )][ValidatePattern (' ^.+$' , ErrorMessage = ' Parameter `Path` must be in single line string!' )][Alias (' Paths' )][String []]$Path ,
32
32
[Alias (' NoValidate' , ' SkipValidate' , ' SkipValidator' )][Switch ]$NoValidator ,
33
- [GitHubActionsEnvironmentVariableScopes ]$Scope = [GitHubActionsEnvironmentVariableScopes ]3
33
+ [Alias ( ' Scopes ' )][ GitHubActionsEnvironmentVariableScopes ]$Scope = [GitHubActionsEnvironmentVariableScopes ]3
34
34
)
35
35
Begin {
36
- [String []]$Result = @ ()
36
+ [Boolean ]$Legacy = [String ]::IsNullOrWhiteSpace($Env: GITHUB_PATH )
37
+ [String []]$ScopeArray = $Scope.ToString () - isplit ' , '
37
38
}
38
39
Process {
39
40
ForEach ($Item In (
@@ -44,29 +45,20 @@ Function Add-PATH {
44
45
Write-Error - Message " `` $Item `` is not a valid PATH!" - Category ' SyntaxError'
45
46
Continue
46
47
}
47
- $Result += $Item
48
- }
49
- }
50
- End {
51
- If ($Result.Count -igt 0 ) {
52
- Switch - Exact ($Scope.ToString () - isplit ' , ' ) {
48
+ Switch - Exact ($ScopeArray ) {
53
49
' Current' {
54
50
[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 |
56
52
Join-String - Separator [System.IO.Path ]::PathSeparator
57
53
)) |
58
54
Out-Null
59
55
}
60
56
' 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
64
59
}
65
60
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'
70
62
}
71
63
}
72
64
}
@@ -97,12 +89,13 @@ Function Set-EnvironmentVariable {
97
89
Param (
98
90
[Parameter (Mandatory = $True , ParameterSetName = ' Multiple' , Position = 0 , ValueFromPipeline = $True )][Alias (' Input' , ' Object' )][Hashtable ]$InputObject ,
99
91
[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 ,
101
93
[Alias (' NoToUppercase' )][Switch ]$NoToUpper ,
102
- [GitHubActionsEnvironmentVariableScopes ]$Scope = [GitHubActionsEnvironmentVariableScopes ]3
94
+ [Alias ( ' Scopes ' )][ GitHubActionsEnvironmentVariableScopes ]$Scope = [GitHubActionsEnvironmentVariableScopes ]3
103
95
)
104
96
Begin {
105
- [Hashtable ]$Result = @ {}
97
+ [Boolean ]$Legacy = [String ]::IsNullOrWhiteSpace($Env: GITHUB_ENV )
98
+ [String []]$ScopeArray = $Scope.ToString () - isplit ' , '
106
99
}
107
100
Process {
108
101
Switch ($PSCmdlet.ParameterSetName ) {
@@ -120,36 +113,26 @@ Function Set-EnvironmentVariable {
120
113
Write-Error - Message ' Parameter `Value` must be type of string!' - Category ' InvalidType'
121
114
Continue
122
115
}
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
128
118
}
129
119
}
130
120
' Single' {
131
- $Result [$NoToUpper.IsPresent ? $Name : $Name.ToUpper ()] = $Value
121
+ [String ]$ItemName = $NoToUpper.IsPresent ? $Name : $Name.ToUpper ()
122
+ [String ]$ItemValue = $Value
132
123
}
133
124
}
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
144
133
}
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
153
136
}
154
137
}
155
138
}
@@ -174,7 +157,7 @@ Function Test-EnvironmentVariableName {
174
157
[Parameter (Mandatory = $True , Position = 0 , ValueFromPipeline = $True )][Alias (' Input' , ' Object' )][String ]$InputObject
175
158
)
176
159
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)_' |
178
161
Write-Output
179
162
}
180
163
}
0 commit comments