Skip to content

Commit 6b31b16

Browse files
committed
20220626A
1 parent fdd2c1f commit 6b31b16

File tree

6 files changed

+147
-40
lines changed

6 files changed

+147
-40
lines changed

hugoalh.GitHubActionsToolkit/module/artifact.psm1

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,29 +33,31 @@ Function Export-Artifact {
3333
}, ErrorMessage = '`{0}` is not a valid GitHub Actions artifact name!')][String]$Name,
3434
[Parameter(Mandatory = $True, ParameterSetName = 'Path', Position = 1, ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True)][SupportsWildcards()][Alias('File', 'Files', 'Paths')][String[]]$Path,
3535
[Parameter(Mandatory = $True, ParameterSetName = 'LiteralPath', ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True)][Alias('LiteralFile', 'LiteralFiles', 'LiteralPaths', 'LP', 'PSPath', 'PSPaths')][String[]]$LiteralPath,
36-
[Alias('Root')][String]$BaseRoot = $Env:GITHUB_WORKSPACE,
36+
[ValidateScript({
37+
Return ([System.IO.Path]::IsPathRooted($_) -and (Test-Path -LiteralPath $_ -PathType 'Container'))
38+
}, ErrorMessage = '`{0}` is not an exist and valid GitHub Actions artifact base root!')][Alias('Root')][String]$BaseRoot = $Env:GITHUB_WORKSPACE,
3739
[Alias('ContinueOnError', 'ContinueOnIssue', 'ContinueOnIssues', 'IgnoreIssuePath')][Switch]$IgnoreIssuePaths,
3840
[ValidateRange(1, 90)][AllowNull()][Alias('RetentionDay')][Byte]$RetentionTime = $Null
3941
)
4042
Begin {
41-
If (
42-
$Null -ieq $BaseRoot -or
43-
![System.IO.Path]::IsPathRooted($BaseRoot) -or
44-
!(Test-Path -LiteralPath $BaseRoot -PathType 'Container')
45-
) {
46-
Return (Write-Error -Message "``$BaseRoot`` is not an exist and valid GitHub Actions artifact base root!" -Category 'SyntaxError')
43+
If (!(Test-GitHubActionsEnvironment -Artifact)) {
44+
Return (Write-Error -Message 'Unable to get GitHub Actions artifact resources!' -Category 'ResourceUnavailable')
4745
Break# This is the best way to early terminate this function without terminate caller/invoker process.
4846
}
47+
<# Disable, not a good method.
4948
[String]$BaseRootRegularExpression = "^$([RegEx]::Escape((Resolve-Path -LiteralPath $BaseRoot)))"
5049
[String[]]$PathsValid = @()
5150
[String[]]$PathsInvalid = @()
51+
#>
52+
[String[]]$PathsProceed = @()
5253
}
5354
Process {
5455
Switch ($PSCmdlet.ParameterSetName) {
5556
'Path' {
57+
<# Disable, not a good method.
5658
ForEach ($ItemPath In $Path) {
5759
Try {
58-
ForEach ($ItemResolve In [String[]](Resolve-Path -Path ([System.IO.Path]::IsPathRooted($ItemPath) ? $ItemPath : (Join-Path -Path $BaseRoot -ChildPath $ItemPath)))) {
60+
ForEach ($ItemResolve In [String[]](Resolve-Path -Path ([System.IO.Path]::IsPathRooted($ItemPath) ? $ItemPath : (Join-Path -Path $BaseRoot -ChildPath $ItemPath)) -ErrorAction 'SilentlyContinue')) {
5961
If (!(Test-Path -LiteralPath $ItemResolve -PathType 'Leaf')) {
6062
Continue
6163
}
@@ -72,11 +74,14 @@ Function Export-Artifact {
7274
$PathsInvalid += $ItemPath
7375
}
7476
}
77+
#>
78+
$PathsProceed += $Path
7579
}
7680
'LiteralPath' {
81+
<# Disable, not a good method
7782
ForEach ($ItemLiteralPath In $LiteralPath) {
7883
Try {
79-
ForEach ($ItemResolve In [String[]](Resolve-Path -LiteralPath ([System.IO.Path]::IsPathRooted($ItemLiteralPath) ? $ItemLiteralPath : (Join-Path -Path $BaseRoot -ChildPath $ItemLiteralPath)))) {
84+
ForEach ($ItemResolve In [String[]](Resolve-Path -LiteralPath ([System.IO.Path]::IsPathRooted($ItemLiteralPath) ? $ItemLiteralPath : (Join-Path -Path $BaseRoot -ChildPath $ItemLiteralPath)) -ErrorAction 'SilentlyContinue')) {
8085
If (!(Test-Path -LiteralPath $ItemResolve -PathType 'Leaf')) {
8186
Continue
8287
}
@@ -93,26 +98,35 @@ Function Export-Artifact {
9398
$PathsInvalid += $ItemLiteralPath
9499
}
95100
}
101+
#>
102+
$PathsProceed += ($LiteralPath | ForEach-Object -Process {
103+
Return [WildcardPattern]::Escape($_)
104+
})
96105
}
97106
}
98107
}
99108
End {
100-
If (!(Test-GitHubActionsEnvironment -Artifact)) {
101-
Return (Write-Error -Message 'Unable to get GitHub Actions artifact resources!' -Category 'ResourceUnavailable')
102-
}
109+
<# Disable, not a good method.
103110
If ($PathsInvalid.Count -igt 0 -and !$IgnoreIssuePaths.IsPresent) {
104111
Return ($PathsInvalid | ForEach-Object -Process {
105112
Return (Write-Error -Message "``$_`` is not an exist and valid file path!" -Category 'SyntaxError')
106113
})
107114
}
108115
If ($PathsValid.Count -ieq 0) {
109-
Return (Write-Error -Message 'No valid path is defined!' -Category 'NotSpecified')
116+
Return (Write-Error -Message 'No valid file path is defined!' -Category 'NotSpecified')
117+
}
118+
#>
119+
If ($PathsProceed.Count -ieq 0) {
120+
Return (Write-Error -Message 'No path is defined!' -Category 'NotSpecified')
110121
}
111122
[Hashtable]$InputObject = @{
112123
Name = $Name
124+
<# Disable, not a good method.
113125
Path = ($PathsValid | ForEach-Object -Process {
114126
Return ($_ -ireplace $BaseRootRegularExpression, '' -ireplace '\\', '/')
115127
})
128+
#>
129+
Path = $PathsProceed
116130
BaseRoot = $BaseRoot
117131
IgnoreIssuePaths = $IgnoreIssuePaths.IsPresent
118132
}
@@ -123,10 +137,13 @@ Function Export-Artifact {
123137
If ($ResultRaw -ieq $False) {
124138
Return
125139
}
140+
<# Disable, not a good method.
126141
[Hashtable]$Result = ($ResultRaw | ConvertFrom-Json -AsHashtable -Depth 100)
127142
$Result.FailedItem += $PathsInvalid
128143
$Result.FailedItems += $PathsInvalid
129144
Return [PSCustomObject]$Result
145+
#>
146+
Return ($ResultRaw | ConvertFrom-Json -Depth 100)
130147
}
131148
}
132149
Set-Alias -Name 'Save-Artifact' -Value 'Export-Artifact' -Option 'ReadOnly' -Scope 'Local'
@@ -150,7 +167,9 @@ Function Import-Artifact {
150167
[CmdletBinding(DefaultParameterSetName = 'Select', HelpUri = 'https://github.com/hugoalh-studio/ghactions-toolkit-powershell/wiki/api_function_import-githubactionsartifact#Import-GitHubActionsArtifact')]
151168
[OutputType([PSCustomObject[]])]
152169
Param (
153-
[Parameter(Mandatory = $True, ParameterSetName = 'Select', Position = 0, ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True)][String[]]$Name,
170+
[Parameter(Mandatory = $True, ParameterSetName = 'Select', Position = 0, ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True)][ValidateScript({
171+
Return (Test-ArtifactName -InputObject $_)
172+
}, ErrorMessage = '`{0}` is not a valid GitHub Actions artifact name!')][String[]]$Name,
154173
[Parameter(ParameterSetName = 'Select')][Switch]$CreateSubfolder,
155174
[Parameter(Mandatory = $True, ParameterSetName = 'All')][Switch]$All,
156175
[String]$Destination = $Env:GITHUB_WORKSPACE

hugoalh.GitHubActionsToolkit/module/cache.psm1

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,24 +38,25 @@ Function Restore-Cache {
3838
[ValidateRange(5, 900)][AllowNull()][UInt16]$Timeout = $Null
3939
)
4040
Begin {
41+
If (!(Test-GitHubActionsEnvironment -Cache)) {
42+
Return (Write-Error -Message 'Unable to get GitHub Actions cache resources!' -Category 'ResourceUnavailable')
43+
Break# This is the best way to early terminate this function without terminate caller/invoker process.
44+
}
4145
[String[]]$PathsProceed = @()
4246
}
4347
Process {
4448
Switch ($PSCmdlet.ParameterSetName) {
45-
'Path' {
46-
$PathsProceed += $Path
47-
}
4849
'LiteralPath' {
4950
$PathsProceed += ($LiteralPath | ForEach-Object -Process {
5051
Return [WildcardPattern]::Escape($_)
5152
})
5253
}
54+
'Path' {
55+
$PathsProceed += $Path
56+
}
5357
}
5458
}
5559
End {
56-
If (!(Test-GitHubActionsEnvironment -Cache)) {
57-
Return (Write-Error -Message 'Unable to get GitHub Actions cache resources!' -Category 'ResourceUnavailable')
58-
}
5960
[String[]]$KeysProceed = @()
6061
If ($Key.Count -igt 10) {
6162
Write-Warning -Message "Keys are limit to maximum count of 10! Only first 10 keys will use."
@@ -123,14 +124,14 @@ Function Save-Cache {
123124
}
124125
Process {
125126
Switch ($PSCmdlet.ParameterSetName) {
126-
'Path' {
127-
$PathsProceed += $Path
128-
}
129127
'LiteralPath' {
130128
$PathsProceed += ($LiteralPath | ForEach-Object -Process {
131129
Return [WildcardPattern]::Escape($_)
132130
})
133131
}
132+
'Path' {
133+
$PathsProceed += $Path
134+
}
134135
}
135136
}
136137
End {

hugoalh.GitHubActionsToolkit/module/environment-variable.psm1

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ Function Add-PATH {
4343
}
4444
End {
4545
If ($Result.Count -igt 0) {
46-
Switch ($Scope.ToString() -isplit ', ') {
47-
{ $_ -icontains 'Current' } {
46+
Switch -Exact ($Scope.ToString() -isplit ', ') {
47+
'Current' {
4848
[String[]]$PATHRaw = [System.Environment]::GetEnvironmentVariable('PATH') -isplit [System.IO.Path]::PathSeparator
4949
$PATHRaw += $Result
5050
[System.Environment]::SetEnvironmentVariable('PATH', ($PATHRaw -join [System.IO.Path]::PathSeparator))
5151
}
52-
{ $_ -icontains 'Subsequent' } {
52+
'Subsequent' {
5353
If ($Null -ieq $Env:GITHUB_PATH) {
5454
ForEach ($Item In $Result) {
5555
Write-GitHubActionsCommand -Command 'add-path' -Value $Item
@@ -127,13 +127,13 @@ Function Set-EnvironmentVariable {
127127
End {
128128
If ($Result.Count -igt 0) {
129129
[PSCustomObject[]]$ResultEnumerator = $Result.GetEnumerator()
130-
Switch ($Scope.ToString() -isplit ', ') {
131-
{ $_ -icontains 'Current' } {
130+
Switch -Exact ($Scope.ToString() -isplit ', ') {
131+
'Current' {
132132
ForEach ($Item In $ResultEnumerator) {
133133
[System.Environment]::SetEnvironmentVariable($Item.Name, $Item.Value)
134134
}
135135
}
136-
{ $_ -icontains 'Subsequent' } {
136+
'Subsequent' {
137137
If ($Null -ieq $Env:GITHUB_ENV) {
138138
ForEach ($Item In $ResultEnumerator) {
139139
Write-GitHubActionsCommand -Command 'set-env' -Value $Item.Value -Parameter @{ 'name' = $Item.Name }

hugoalh.GitHubActionsToolkit/module/nodejs-invoke.psm1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Function Invoke-NodeJsWrapper {
4141
for ($ResultIndex = 0; $ResultIndex -lt $Result.Count; $ResultIndex++) {
4242
[String]$Item = $Result[$ResultIndex]
4343
If (
44+
$Item -imatch '^::debug' -or
4445
$Item -imatch '^::error' -or
4546
$Item -imatch '^::notice' -or
4647
$Item -imatch '^::warning'

hugoalh.GitHubActionsToolkit/module/problem-matcher.psm1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ Function Add-ProblemMatcher {
2525
Begin {}
2626
Process {
2727
Switch ($PSCmdlet.ParameterSetName) {
28-
'Path' {
29-
ForEach ($Item In [String[]](Resolve-Path -Path $Path)) {
28+
'LiteralPath' {
29+
ForEach ($Item In $LiteralPath) {
3030
Write-GitHubActionsCommand -Command 'add-matcher' -Value ($Item -ireplace '^\.[\\\/]', '' -ireplace '\\', '/')
3131
}
3232
}
33-
'LiteralPath' {
34-
ForEach ($Item In $LiteralPath) {
33+
'Path' {
34+
ForEach ($Item In [String[]](Resolve-Path -Path $Path -ErrorAction 'SilentlyContinue')) {
3535
Write-GitHubActionsCommand -Command 'add-matcher' -Value ($Item -ireplace '^\.[\\\/]', '' -ireplace '\\', '/')
3636
}
3737
}

hugoalh.GitHubActionsToolkit/module/tool-cache.psm1

Lines changed: 93 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,41 @@ Import-Module -Name @(
44
(Join-Path -Path $PSScriptRoot -ChildPath 'nodejs-invoke.psm1'),
55
(Join-Path -Path $PSScriptRoot -ChildPath 'utility.psm1')
66
) -Prefix 'GitHubActions' -Scope 'Local'
7+
<#
8+
.SYNOPSIS
9+
GitHub Actions - Expand Tool Cache Compressed File
10+
.DESCRIPTION
11+
Expand a compressed archive/file.
12+
.PARAMETER File
13+
Compressed archive/file path.
14+
.PARAMETER Destination
15+
Expand destination path.
16+
.PARAMETER Method
17+
Method to expand compressed archive/file; Define this value will enforce to use defined method.
18+
.PARAMETER 7zrPath
19+
`7zr` path, for long path support (only for expand method is `7z`); Most `.7z` archives do not have this problem, if `.7z` archive contains very long path, pass the path to `7zr` which will gracefully handle long paths, by default `7zdec` is used because it is a very small program and is bundled with the GitHub Actions NodeJS toolkit, however it does not support long paths, `7zr` is the reduced command line interface, it is smaller than the full command line interface, and it does support long paths, at the time of this writing, it is freely available from the LZMA SDK that is available on the 7-Zip website, be sure to check the current license agreement, if `7zr` is bundled with your action, then the path to `7zr` can be pass to this function.
20+
.PARAMETER Flag
21+
Flag for expand method is `Tar` or `Xar`, to use for extraction.
22+
.OUTPUTS
23+
[String] Expand destination path.
24+
#>
725
Function Expand-ToolCacheCompressedFile {
826
[CmdletBinding(HelpUri = 'https://github.com/hugoalh-studio/ghactions-toolkit-powershell/wiki/api_function_expand-githubactionstoolcachecompressedfile#Expand-GitHubActionsToolCacheCompressedFile')]
927
[OutputType([String])]
1028
Param (
1129
[Parameter(Mandatory = $True, Position = 0)][Alias('Source')][String]$File,
1230
[Alias('Target')][String]$Destination,
13-
[ValidateSet('7z', 'A', 'Auto', 'Automatic', 'Automatically', 'Tar', 'Xar', 'Zip')][String]$Method = 'Automatically',
31+
[ValidateSet('7z', 'Auto', 'Automatic', 'Automatically', 'Tar', 'Xar', 'Zip')][String]$Method = 'Automatically',
1432
[String]$7zrPath,
1533
[Alias('Flags')][String]$Flag
1634
)
35+
If (!(Test-GitHubActionsEnvironment -ToolCache)) {
36+
Return (Write-Error -Message 'Unable to get GitHub Actions tool cache resources!' -Category 'ResourceUnavailable')
37+
}
1738
If (!(Test-Path -LiteralPath $File -PathType 'Leaf')) {
1839
Return (Write-Error -Message "``$File`` is not a valid file path!" -Category 'SyntaxError')
1940
}
20-
If ($Method -imatch '^A(?:uto(?:matic(?:ally)?)?)?$') {
41+
If ($Method -imatch '^Auto(?:matic(?:ally)?)?$') {
2142
Switch -RegEx ($File) {
2243
'\.7z$' {
2344
$Method = '7z'
@@ -27,11 +48,7 @@ Function Expand-ToolCacheCompressedFile {
2748
$Method = 'Xar'
2849
Break
2950
}
30-
'\.tar$' {
31-
$Method = 'Tar'
32-
Break
33-
}
34-
'\.tar\.gz$' {
51+
'\.tar(?:\.gz)?$' {
3552
$Method = 'Tar'
3653
Break
3754
}
@@ -63,4 +80,73 @@ Function Expand-ToolCacheCompressedFile {
6380
}
6481
Return ($ResultRaw | ConvertFrom-Json -Depth 100).Path
6582
}
83+
Set-Alias -Name 'Expand-ToolCacheArchive' -Value 'Expand-ToolCacheCompressedFile' -Option 'ReadOnly' -Scope 'Local'
84+
Set-Alias -Name 'Expand-ToolCacheCompressedArchive' -Value 'Expand-ToolCacheCompressedFile' -Option 'ReadOnly' -Scope 'Local'
6685
Set-Alias -Name 'Expand-ToolCacheFile' -Value 'Expand-ToolCacheCompressedFile' -Option 'ReadOnly' -Scope 'Local'
86+
<#
87+
.SYNOPSIS
88+
GitHub Actions - Invoke Tool Cache Tool Downloader
89+
.DESCRIPTION
90+
Download a tool from URI and stream it into a file.
91+
.PARAMETER Uri
92+
Tool URI.
93+
.PARAMETER Destination
94+
Tool destination path.
95+
.PARAMETER Authorization
96+
Tool URI request authorization.
97+
.PARAMETER Header
98+
Tool URI request header.
99+
.OUTPUTS
100+
[String[]] Path of the downloaded tool.
101+
#>
102+
Function Invoke-ToolCacheToolDownloader {
103+
[CmdletBinding(HelpUri = 'https://github.com/hugoalh-studio/ghactions-toolkit-powershell/wiki/api_function_invoke-githubactionstoolcachetooldownloader#Invoke-GitHubActionsToolCacheToolDownloader')]
104+
[OutputType([String[]])]
105+
Param (
106+
[Parameter(Mandatory = $True, Position = 0, ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True)][ValidateScript({
107+
$URIObject = $_ -as [System.URI]
108+
Return (($Null -ine $URIObject.AbsoluteURI) -and ($_ -imatch '^https?:\/\/'))
109+
}, ErrorMessage = '`{0}` is not a valid URI!')][Alias('Url')][String[]]$Uri,
110+
[Alias('Target')][String]$Destination,
111+
[Alias('Auth')][String]$Authorization,
112+
[Alias('Headers')][Hashtable]$Header
113+
)
114+
Begin {
115+
If (!(Test-GitHubActionsEnvironment -ToolCache)) {
116+
Return (Write-Error -Message 'Unable to get GitHub Actions tool cache resources!' -Category 'ResourceUnavailable')
117+
}
118+
[String[]]$OutputObject = @()
119+
}
120+
Process {
121+
ForEach ($Item In $Uri) {
122+
[Hashtable]$InputObject = @{
123+
Uri = $Item
124+
}
125+
If ($Destination.Length -igt 0) {
126+
$InputObject.Destination = $Destination
127+
}
128+
If ($Authorization.Length -igt 0) {
129+
$InputObject.Authorization = $Authorization
130+
}
131+
If ($Header.Count -igt 0) {
132+
$InputObject.Header = ([PSCustomObject]$Header | ConvertTo-Json -Depth 100 -Compress)
133+
}
134+
$ResultRaw = Invoke-GitHubActionsNodeJsWrapper -Path 'tool-cache\download-tool.js' -InputObject ([PSCustomObject]$InputObject | ConvertTo-Json -Depth 100 -Compress)
135+
If ($ResultRaw -ieq $False) {
136+
Continue
137+
}
138+
$OutputObject += ($ResultRaw | ConvertFrom-Json -Depth 100).Path
139+
}
140+
}
141+
End {
142+
Return $OutputObject
143+
}
144+
}
145+
Export-ModuleMember -Function @(
146+
'Expand-ToolCacheCompressedFile',
147+
'Invoke-ToolCacheToolDownloader'
148+
) -Alias @(
149+
'Expand-ToolCacheArchive',
150+
'Expand-ToolCacheCompressedArchive',
151+
'Expand-ToolCacheFile'
152+
)

0 commit comments

Comments
 (0)