Skip to content

Commit f04b93e

Browse files
committed
20220804B
1 parent 2463fea commit f04b93e

File tree

11 files changed

+95
-102
lines changed

11 files changed

+95
-102
lines changed

hugoalh.GitHubActionsToolkit/_get-package-commands.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ This script is use for debug, and help to copy commands to hugoalh.GitHubActions
66
Import-Module -Name (Join-Path -Path $PSScriptRoot -ChildPath 'hugoalh.GitHubActionsToolkit.psm1') -Scope 'Local'
77
[PSCustomObject[]]$PackageCommands = Get-Command -Module 'hugoalh.GitHubActionsToolkit' -ListImported
88
ForEach ($CommandType In @('Function', 'Alias')) {
9-
Set-Clipboard -Value "'$(($PackageCommands | Where-Object -FilterScript {
9+
Set-Clipboard -Value "$($PackageCommands | Where-Object -FilterScript {
1010
Return ($_.CommandType -ieq $CommandType)
11-
} | Sort-Object -Property 'Name').Name -join "',`n'")'" -Confirm
11+
} | Select-Object -ExpandProperty 'Name' | Sort-Object | Join-String -Separator "`n" -SingleQuote)" -Confirm
1212
}

hugoalh.GitHubActionsToolkit/module/artifact.psm1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Function Export-Artifact {
3333
[Parameter(Mandatory = $True, ParameterSetName = 'LiteralPath', ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True)][Alias('LiteralFile', 'LiteralFiles', 'LiteralPaths', 'LP', 'PSPath', 'PSPaths')][String[]]$LiteralPath,
3434
[Parameter(ValueFromPipelineByPropertyName = $True)][ValidateScript({ Return ([System.IO.Path]::IsPathRooted($_) -and (Test-Path -LiteralPath $_ -PathType 'Container')) }, ErrorMessage = '`{0}` is not an exist and valid GitHub Actions artifact base root!')][Alias('Root')][String]$BaseRoot = $Env:GITHUB_WORKSPACE,
3535
[Parameter(ValueFromPipelineByPropertyName = $True)][Alias('ContinueOnError')][Switch]$ContinueOnIssue,
36-
[Parameter(ValueFromPipelineByPropertyName = $True)][ValidateRange(1, 90)][Alias('RetentionDay')][Byte]$RetentionTime = 0
36+
[Parameter(ValueFromPipelineByPropertyName = $True)][ValidateRange(1, 90)][Alias('RetentionDay')][Byte]$RetentionTime
3737
)
3838
Begin {
3939
[Boolean]$NoOperation = $False# When the requirements are not fulfill, only stop this function but not others.

hugoalh.GitHubActionsToolkit/module/cache.psm1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ Function Restore-Cache {
3232
[Parameter(Mandatory = $True, ParameterSetName = 'Path', Position = 1, ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True)][SupportsWildcards()][Alias('File', 'Files', 'Paths')][String[]]$Path,
3333
[Parameter(Mandatory = $True, ParameterSetName = 'LiteralPath', ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True)][Alias('LiteralFile', 'LiteralFiles', 'LiteralPaths', 'LP', 'PSPath', 'PSPaths')][String[]]$LiteralPath,
3434
[Parameter(ValueFromPipelineByPropertyName = $True)][Alias('NoAzureSdk')][Switch]$NotUseAzureSdk,
35-
[Parameter(ValueFromPipelineByPropertyName = $True)][ValidateRange(1, 16)][Alias('Concurrency')][Byte]$DownloadConcurrency = 0,
36-
[Parameter(ValueFromPipelineByPropertyName = $True)][ValidateRange(5, 900)][UInt16]$Timeout = 0
35+
[Parameter(ValueFromPipelineByPropertyName = $True)][ValidateRange(1, 16)][Alias('Concurrency')][Byte]$DownloadConcurrency,
36+
[Parameter(ValueFromPipelineByPropertyName = $True)][ValidateRange(5, 900)][UInt16]$Timeout
3737
)
3838
Begin {
3939
[Boolean]$NoOperation = $False# When the requirements are not fulfill, only stop this function but not others.
@@ -103,8 +103,8 @@ Function Save-Cache {
103103
[Parameter(Mandatory = $True, Position = 0, ValueFromPipelineByPropertyName = $True)][ValidateScript({ Return (Test-CacheKey -InputObject $_) }, ErrorMessage = '`{0}` is not a valid GitHub Actions cache key, and/or more than 512 characters!')][Alias('Name')][String]$Key,
104104
[Parameter(Mandatory = $True, ParameterSetName = 'Path', Position = 1, ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True)][SupportsWildcards()][Alias('File', 'Files', 'Paths')][String[]]$Path,
105105
[Parameter(Mandatory = $True, ParameterSetName = 'LiteralPath', ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True)][Alias('LiteralFile', 'LiteralFiles', 'LiteralPaths', 'LP', 'PSPath', 'PSPaths')][String[]]$LiteralPath,
106-
[Parameter(ValueFromPipelineByPropertyName = $True)][ValidateRange(1, 1MB)][Alias('ChunkSize', 'ChunkSizes', 'UploadChunkSize')][UInt32]$UploadChunkSizes = 0,
107-
[Parameter(ValueFromPipelineByPropertyName = $True)][ValidateRange(1, 16)][Alias('Concurrency')][Byte]$UploadConcurrency = 0
106+
[Parameter(ValueFromPipelineByPropertyName = $True)][ValidateRange(1, 1MB)][Alias('ChunkSize', 'ChunkSizes', 'UploadChunkSize')][UInt32]$UploadChunkSizes,
107+
[Parameter(ValueFromPipelineByPropertyName = $True)][ValidateRange(1, 16)][Alias('Concurrency')][Byte]$UploadConcurrency
108108
)
109109
Begin {
110110
[Boolean]$NoOperation = $False# When the requirements are not fulfill, only stop this function but not others.

hugoalh.GitHubActionsToolkit/module/command-base.psm1

Lines changed: 18 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,22 @@
11
#Requires -PSEdition Core
22
#Requires -Version 7.2
3-
<#
4-
.SYNOPSIS
5-
GitHub Actions (Internal) - Format Command Parameter Value
6-
.DESCRIPTION
7-
Format command parameter value characters that can cause issues.
8-
.PARAMETER InputObject
9-
String that need to format command parameter value characters.
10-
.OUTPUTS
11-
[String] A string that formatted command parameter value characters.
12-
#>
13-
Function Format-CommandParameterValue {
14-
[CmdletBinding()]
15-
[OutputType([String])]
16-
Param (
17-
[Parameter(Mandatory = $True, Position = 0, ValueFromPipeline = $True)][AllowEmptyString()][Alias('Input', 'Object')][String]$InputObject
18-
)
19-
Begin {}
20-
Process {
21-
Return ((Format-CommandValue -InputObject $InputObject) -ireplace ',', '%2C' -ireplace ':', '%3A')
3+
Class GitHubActionsCommand {
4+
Static [String]EscapeContent([String]$InputObject) {
5+
Return [GitHubActionsCommand]::EscapeValue($InputObject)
226
}
23-
End {}
24-
}
25-
Set-Alias -Name 'Format-CommandPropertyValue' -Value 'Format-CommandParameterValue' -Option 'ReadOnly' -Scope 'Local'
26-
<#
27-
.SYNOPSIS
28-
GitHub Actions (Internal) - Format Command Value
29-
.DESCRIPTION
30-
Format command value characters that can cause issues.
31-
.PARAMETER InputObject
32-
String that need to format command value characters.
33-
.OUTPUTS
34-
[String] A string that formatted command value characters.
35-
#>
36-
Function Format-CommandValue {
37-
[CmdletBinding()]
38-
[OutputType([String])]
39-
Param (
40-
[Parameter(Mandatory = $True, Position = 0, ValueFromPipeline = $True)][AllowEmptyString()][Alias('Input', 'Object')][String]$InputObject
41-
)
42-
Begin {}
43-
Process {
7+
Static [String]EscapeMessage([String]$InputObject) {
8+
Return [GitHubActionsCommand]::EscapeValue($InputObject)
9+
}
10+
Static [String]EscapeParameterValue([String]$InputObject) {
11+
Return ([GitHubActionsCommand]::EscapeValue($InputObject) -ireplace ',', '%2C' -ireplace ':', '%3A')
12+
}
13+
Static [String]EscapePropertyValue([String]$InputObject) {
14+
Return [GitHubActionsCommand]::EscapeParameterValue($InputObject)
15+
}
16+
Static [String]EscapeValue([String]$InputObject) {
4417
Return ($InputObject -ireplace '%', '%25' -ireplace '\n', '%0A' -ireplace '\r', '%0D')
4518
}
46-
End {}
4719
}
48-
Set-Alias -Name 'Format-CommandContent' -Value 'Format-CommandValue' -Option 'ReadOnly' -Scope 'Local'
49-
Set-Alias -Name 'Format-CommandMessage' -Value 'Format-CommandValue' -Option 'ReadOnly' -Scope 'Local'
5020
<#
5121
.SYNOPSIS
5222
GitHub Actions - Write Command
@@ -66,14 +36,14 @@ Function Write-Command {
6636
[OutputType([Void])]
6737
Param (
6838
[Parameter(Mandatory = $True, Position = 0, ValueFromPipelineByPropertyName = $True)][ValidatePattern('^(?:[\da-z][\da-z_-]*)?[\da-z]$', ErrorMessage = '`{0}` is not a valid GitHub Actions command!')][String]$Command,
69-
[Parameter(ValueFromPipelineByPropertyName = $True)][Alias('Parameters', 'Properties', 'Property')][Hashtable]$Parameter = @{},
70-
[Parameter(ValueFromPipelineByPropertyName = $True)][Alias('Content', 'Message')][String]$Value = ''
39+
[Parameter(ValueFromPipelineByPropertyName = $True)][Alias('Parameters', 'Properties', 'Property')][Hashtable]$Parameter,
40+
[Parameter(ValueFromPipelineByPropertyName = $True)][Alias('Content', 'Message')][String]$Value
7141
)
7242
Begin {}
7343
Process {
74-
Write-Host -Object "::$Command$(($Parameter.Count -igt 0) ? " $(($Parameter.GetEnumerator() | Sort-Object -Property 'Name' | ForEach-Object -Process {
75-
Return "$($_.Name)=$(Format-CommandParameterValue -InputObject $_.Value)"
76-
}) -join ',')" : '')::$(Format-CommandValue -InputObject $Value)"
44+
Write-Host -Object "::$Command$(($Parameter.Count -igt 0) ? " $($Parameter.GetEnumerator() | Sort-Object -Property 'Name' | ForEach-Object -Process {
45+
Return "$($_.Name)=$([GitHubActionsCommand]::EscapeParameterValue($_.Value))"
46+
} | Join-String -Separator ',')" : '')::$([GitHubActionsCommand]::EscapeValue($Value))"
7747
}
7848
End {}
7949
}

hugoalh.GitHubActionsToolkit/module/command-control.psm1

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ Import-Module -Name @(
2020
'stop-commands'
2121
'warning'
2222
)
23+
[Char[]]$GitHubActionsCommandsEndTokenPool = [String[]]@(0..9) + [Char[]]@(97..122)
24+
[String[]]$GitHubActionsCommandsEndTokensUsed = @()
2325
<#
2426
.SYNOPSIS
2527
GitHub Actions - Disable Echoing Commands
@@ -63,7 +65,7 @@ Function Disable-ProcessingCommands {
6365
[CmdletBinding(HelpUri = 'https://github.com/hugoalh-studio/ghactions-toolkit-powershell/wiki/api_function_disable-githubactionsprocessingcommands#Disable-GitHubActionsProcessingCommands')]
6466
[OutputType([String])]
6567
Param (
66-
[Parameter(Position = 0)][ValidateScript({ Return (Test-ProcessingCommandsEndToken -InputObject $_) }, ErrorMessage = 'Parameter `EndToken` must be in single line string, more than or equal to 4 characters, not match any GitHub Actions commands, and unique!')][Alias('EndKey', 'EndValue', 'Key', 'Token', 'Value')][String]$EndToken = ((New-Guid).Guid -ireplace '-', '')
68+
[Parameter(Position = 0)][ValidateScript({ Return (Test-ProcessingCommandsEndToken -InputObject $_) }, ErrorMessage = 'Parameter `EndToken` must be in single line string, more than or equal to 4 characters, not match any GitHub Actions commands, and unique!')][Alias('EndKey', 'EndValue', 'Key', 'Token', 'Value')][String]$EndToken = (Get-RandomCommandsEndToken)
6769
)
6870
Write-GitHubActionsCommand -Command 'stop-commands' -Value $EndToken
6971
Return $EndToken
@@ -147,6 +149,27 @@ Set-Alias -Name 'Start-ProcessingCommand' -Value 'Enable-ProcessingCommands' -Op
147149
Set-Alias -Name 'Start-ProcessingCommands' -Value 'Enable-ProcessingCommands' -Option 'ReadOnly' -Scope 'Local'
148150
<#
149151
.SYNOPSIS
152+
GitHub Actions (Internal) - Get Random Commands End Token
153+
.DESCRIPTION
154+
Get random GitHub Actions commands end token.
155+
.OUTPUTS
156+
[String] A random GitHub Actions commands end token.
157+
#>
158+
Function Get-RandomCommandsEndToken {
159+
[CmdletBinding()]
160+
[OutputType([String])]
161+
Param ()
162+
[String]$Result = (@(1..64) | ForEach-Object -Process {
163+
Return ($GitHubActionsCommandsEndTokenPool | Get-Random -Count 1)
164+
} | Join-String -Separator '')
165+
If ($Result -iin $GitHubActionsCommandsEndTokensUsed) {
166+
Return Get-RandomCommandsEndToken
167+
}
168+
$Script:GitHubActionsCommandsEndTokensUsed += $Result
169+
Return $Result
170+
}
171+
<#
172+
.SYNOPSIS
150173
GitHub Actions (Internal) - Test Processing Commands End Token
151174
.DESCRIPTION
152175
Test GitHub Actions processing commands end token whether is valid.

hugoalh.GitHubActionsToolkit/module/environment-variable.psm1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@ Function Add-PATH {
4545
If ($Result.Count -igt 0) {
4646
Switch -Exact ($Scope.ToString() -isplit ', ') {
4747
'Current' {
48-
[System.Environment]::SetEnvironmentVariable('PATH', ((([System.Environment]::GetEnvironmentVariable('PATH') -isplit [System.IO.Path]::PathSeparator) + $Result) -join [System.IO.Path]::PathSeparator)) | Out-Null
48+
[System.Environment]::SetEnvironmentVariable('PATH', ((([System.Environment]::GetEnvironmentVariable('PATH') -isplit [System.IO.Path]::PathSeparator) + $Result) | Join-String -Separator [System.IO.Path]::PathSeparator)) | Out-Null
4949
}
5050
'Subsequent' {
5151
If ([String]::IsNullOrWhiteSpace($Env:GITHUB_PATH)) {
5252
$Result | ForEach-Object -Process {
5353
Write-GitHubActionsCommand -Command 'add-path' -Value $_
5454
}
5555
} Else {
56-
Add-Content -LiteralPath $Env:GITHUB_PATH -Value ($Result -join "`n") -Confirm:$False -Encoding 'UTF8NoBOM'
56+
Add-Content -LiteralPath $Env:GITHUB_PATH -Value ($Result | Join-String -Separator "`n") -Confirm:$False -Encoding 'UTF8NoBOM'
5757
}
5858
}
5959
}
@@ -133,9 +133,9 @@ Function Set-EnvironmentVariable {
133133
Write-GitHubActionsCommand -Command 'set-env' -Parameter @{ 'name' = $_.Name } -Value $_.Value
134134
}
135135
} Else {
136-
Add-Content -LiteralPath $Env:GITHUB_ENV -Value (($Result.GetEnumerator() | ForEach-Object -Process {
136+
Add-Content -LiteralPath $Env:GITHUB_ENV -Value ($Result.GetEnumerator() | ForEach-Object -Process {
137137
Return "$($_.Name)=$($_.Value)"
138-
}) -join "`n") -Confirm:$False -Encoding 'UTF8NoBOM'
138+
} | Join-String -Separator "`n") -Confirm:$False -Encoding 'UTF8NoBOM'
139139
}
140140
}
141141
}

0 commit comments

Comments
 (0)