Skip to content

Commit 7d827b0

Browse files
refactor init, boilerplate and output
1 parent ca9e2ea commit 7d827b0

File tree

5 files changed

+140
-122
lines changed

5 files changed

+140
-122
lines changed

action.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ runs:
6666
GITHUB_ACTION_INPUT_Prerelease: ${{ inputs.Prerelease }}
6767
run: |
6868
# GitHub-Script
69-
. $(Join-Path -Path '${{ github.action_path }}' -ChildPath 'scripts\main.ps1')
69+
. ${{ github.action_path }}\scripts\init.ps1
70+
. ${{ github.action_path }}\scripts\boilerplate.ps1
7071
${{ inputs.Script }}
71-
. $(Join-Path -Path '${{ github.action_path }}' -ChildPath 'scripts\outputs.ps1')
72+
. ${{ github.action_path }}\scripts\outputs.ps1

scripts/boilerplate.ps1

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
[CmdletBinding()]
2+
param()
3+
4+
$scriptName = $MyInvocation.MyCommand.Name
5+
Write-Debug "[$scriptName] - Start"
6+
7+
try {
8+
if ($env:GITHUB_ACTION_INPUT_ShowBoilerplate -ne 'true') {
9+
return
10+
}
11+
12+
$title = "┏━━━━━┫ $Name ┣━━━━━┓"
13+
Write-Output $title
14+
15+
LogGroup ' - Installed modules' {
16+
Get-InstalledPSResource | Select-Object Name, Version, Prerelease | Sort-Object -Property Name | Format-Table -AutoSize
17+
}
18+
19+
LogGroup ' - GitHub connection' {
20+
if ($providedClientID -and $providedPrivateKey) {
21+
Connect-GitHub -ClientID $env:GITHUB_ACTION_INPUT_ClientID -PrivateKey $env:GITHUB_ACTION_INPUT_PrivateKey -Silent -PassThru |
22+
Select-Object * | Format-List
23+
} elseif ($providedToken) {
24+
Connect-GitHub -Token $env:GITHUB_ACTION_INPUT_Token -Silent -PassThru |
25+
Select-Object * | Format-List
26+
} else {
27+
Write-Output 'No connection provided'
28+
}
29+
}
30+
31+
LogGroup ' - Configuration' {
32+
Get-GitHubConfig | Format-List
33+
}
34+
35+
$endingFence = '' + ('' * ($title.Length - 2)) + ''
36+
Write-Output $endingFence
37+
} catch {
38+
throw $_
39+
}
40+
41+
Write-Debug "[$scriptName] - End"
42+
$DebugPreference = $env:GITHUB_ACTION_INPUT_Debug -eq 'true' ? 'Continue' : 'SilentlyContinue'
43+
$VerbosePreference = $env:GITHUB_ACTION_INPUT_Verbose -eq 'true' ? 'Continue' : 'SilentlyContinue'

scripts/init.ps1

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
[CmdletBinding()]
2+
param()
3+
4+
$scriptName = $MyInvocation.MyCommand.Name
5+
Write-Debug "[$scriptName] - Start"
6+
7+
try {
8+
$env:PSMODULE_GITHUB_SCRIPT = $true
9+
10+
if ($VerbosePreference -eq 'Continue') {
11+
Write-Output '┏━━━━━┫ GitHub-Script - Init ┣━━━━━┓'
12+
Write-Output '::group:: - SetupGitHub PowerShell module'
13+
}
14+
$Name = 'GitHub'
15+
$Version = [string]::IsNullOrEmpty($env:GITHUB_ACTION_INPUT_Version) ? $null : $env:GITHUB_ACTION_INPUT_Version
16+
$Prerelease = $env:GITHUB_ACTION_INPUT_Prerelease -eq 'true'
17+
18+
$alreadyInstalled = Get-InstalledPSResource -Name $Name -ErrorAction SilentlyContinue
19+
if ($Version) {
20+
Write-Verbose "Filtering by version: $Version"
21+
$alreadyInstalled = $alreadyInstalled | Where-Object Version -EQ $Version
22+
}
23+
if ($Prerelease) {
24+
Write-Verbose 'Filtering by prerelease'
25+
$alreadyInstalled = $alreadyInstalled | Where-Object Prerelease -EQ $Prerelease
26+
}
27+
Write-Verbose 'Already installed:'
28+
Write-Verbose ($alreadyInstalled | Format-Table)
29+
if (-not $alreadyInstalled) {
30+
$params = @{
31+
Name = $Name
32+
Repository = 'PSGallery'
33+
TrustRepository = $true
34+
Prerelease = $Prerelease
35+
}
36+
if ($Version) {
37+
$params['Version'] = $Version
38+
}
39+
$Count = 5
40+
$Delay = 10
41+
for ($i = 1; $i -le $Count; $i++) {
42+
try {
43+
Install-PSResource @params -ErrorAction Stop
44+
break
45+
} catch {
46+
Write-Warning $_.Exception.Message
47+
if ($i -eq $Count) {
48+
throw $_
49+
}
50+
Start-Sleep -Seconds $Delay
51+
}
52+
}
53+
}
54+
55+
$alreadyImported = Get-Module -Name $Name
56+
Write-Verbose 'Already imported:'
57+
Write-Verbose ($alreadyImported | Format-Table)
58+
if (-not $alreadyImported) {
59+
Write-Verbose "Importing module: $Name"
60+
Import-Module -Name $Name
61+
}
62+
63+
$providedToken = -not [string]::IsNullOrEmpty($env:GITHUB_ACTION_INPUT_Token)
64+
$providedClientID = -not [string]::IsNullOrEmpty($env:GITHUB_ACTION_INPUT_ClientID)
65+
$providedPrivateKey = -not [string]::IsNullOrEmpty($env:GITHUB_ACTION_INPUT_PrivateKey)
66+
$moduleStatus = [pscustomobject]@{
67+
Name = $Name
68+
Version = [string]::IsNullOrEmpty($Version) ? 'latest' : $Version
69+
Prerelease = $Prerelease
70+
'Already installed' = $null -ne $alreadyInstalled
71+
'Already imported' = $null -ne $alreadyImported
72+
'Provided Token' = $providedToken
73+
'Provided ClientID' = $providedClientID
74+
'Provided PrivateKey' = $providedPrivateKey
75+
}
76+
Write-Verbose ($moduleStatus | Format-List)
77+
if ($VerbosePreference -eq 'Continue') {
78+
Write-Output '::endgroup::'
79+
Write-Output '┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛'
80+
}
81+
} catch {
82+
throw $_
83+
}
84+
85+
Write-Debug "[$scriptName] - End"

scripts/main.ps1

Lines changed: 0 additions & 108 deletions
This file was deleted.

scripts/outputs.ps1

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
[CmdletBinding()]
22
param()
33

4-
begin {
54
$DebugPreference = 'SilentlyContinue'
65
$VerbosePreference = 'SilentlyContinue'
7-
Write-Debug '[outputs] - Start'
8-
}
6+
$scriptName = $MyInvocation.MyCommand.Name
7+
Write-Debug "[$scriptName] - Start"
98

10-
process {
119
try {
12-
Write-Debug "[outputs] - ShowOutput: $env:GITHUB_ACTION_INPUT_ShowOutput"
10+
Write-Debug "[$scriptName] - ShowOutput: $env:GITHUB_ACTION_INPUT_ShowOutput"
1311
if ($env:GITHUB_ACTION_INPUT_ShowOutput -ne 'true') {
1412
return
1513
}
1614

1715
$result = (Get-GitHubOutput).result
18-
Write-Debug "[outputs] - Result: $(-not $result)"
16+
Write-Debug "[$scriptName] - Result: $(-not $result)"
1917
if (-not $result) {
2018
return
2119
}
22-
Write-Host '┏━━━━━┫ GitHub-Script ┣━━━━━┓'
20+
$title = "┏━━━━━┫ $Name ┣━━━━━┓"
21+
Write-Output $title
2322
LogGroup ' - Outputs' {
2423
if ([string]::IsNullOrEmpty($env:GITHUB_ACTION)) {
2524
Write-GitHubWarning 'Outputs cannot be accessed as the step has no ID.'
@@ -32,12 +31,10 @@ process {
3231
$result | Format-List
3332
Write-Host "Access outputs using `${{ fromJson(steps.$env:GITHUB_ACTION.outputs.result).<output-name> }}"
3433
}
35-
Write-Host '┗━━━━━━━━━━━━━━━━━━━━━━━━━━━┛'
34+
$endingFence = '' + ('' * ($title.Length - 2)) + ''
35+
Write-Output $endingFence
3636
} catch {
3737
throw $_
3838
}
39-
}
4039

41-
end {
42-
Write-Debug '[outputs] - End'
43-
}
40+
Write-Debug "$scriptName - End"

0 commit comments

Comments
 (0)