Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
refactor init, boilerplate and output
  • Loading branch information
MariusStorhaug committed Jan 22, 2025
commit 7d827b04edf559dbcc2d36bef8726b07612b8fd5
5 changes: 3 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ runs:
GITHUB_ACTION_INPUT_Prerelease: ${{ inputs.Prerelease }}
run: |
# GitHub-Script
. $(Join-Path -Path '${{ github.action_path }}' -ChildPath 'scripts\main.ps1')
. ${{ github.action_path }}\scripts\init.ps1
. ${{ github.action_path }}\scripts\boilerplate.ps1
${{ inputs.Script }}
. $(Join-Path -Path '${{ github.action_path }}' -ChildPath 'scripts\outputs.ps1')
. ${{ github.action_path }}\scripts\outputs.ps1
43 changes: 43 additions & 0 deletions scripts/boilerplate.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
[CmdletBinding()]
param()

$scriptName = $MyInvocation.MyCommand.Name
Write-Debug "[$scriptName] - Start"

try {
if ($env:GITHUB_ACTION_INPUT_ShowBoilerplate -ne 'true') {
return
}

$title = "┏━━━━━┫ $Name ┣━━━━━┓"
Write-Output $title

LogGroup ' - Installed modules' {
Get-InstalledPSResource | Select-Object Name, Version, Prerelease | Sort-Object -Property Name | Format-Table -AutoSize
}

LogGroup ' - GitHub connection' {
if ($providedClientID -and $providedPrivateKey) {
Connect-GitHub -ClientID $env:GITHUB_ACTION_INPUT_ClientID -PrivateKey $env:GITHUB_ACTION_INPUT_PrivateKey -Silent -PassThru |
Select-Object * | Format-List
} elseif ($providedToken) {
Connect-GitHub -Token $env:GITHUB_ACTION_INPUT_Token -Silent -PassThru |
Select-Object * | Format-List
} else {
Write-Output 'No connection provided'
}
}

LogGroup ' - Configuration' {
Get-GitHubConfig | Format-List
}

$endingFence = '┗' + ('━' * ($title.Length - 2)) + '┛'
Write-Output $endingFence
} catch {
throw $_
}

Write-Debug "[$scriptName] - End"
$DebugPreference = $env:GITHUB_ACTION_INPUT_Debug -eq 'true' ? 'Continue' : 'SilentlyContinue'
$VerbosePreference = $env:GITHUB_ACTION_INPUT_Verbose -eq 'true' ? 'Continue' : 'SilentlyContinue'
85 changes: 85 additions & 0 deletions scripts/init.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
[CmdletBinding()]
param()

$scriptName = $MyInvocation.MyCommand.Name
Write-Debug "[$scriptName] - Start"

try {
$env:PSMODULE_GITHUB_SCRIPT = $true

if ($VerbosePreference -eq 'Continue') {
Write-Output '┏━━━━━┫ GitHub-Script - Init ┣━━━━━┓'
Write-Output '::group:: - SetupGitHub PowerShell module'
}
$Name = 'GitHub'
$Version = [string]::IsNullOrEmpty($env:GITHUB_ACTION_INPUT_Version) ? $null : $env:GITHUB_ACTION_INPUT_Version
$Prerelease = $env:GITHUB_ACTION_INPUT_Prerelease -eq 'true'

$alreadyInstalled = Get-InstalledPSResource -Name $Name -ErrorAction SilentlyContinue
if ($Version) {
Write-Verbose "Filtering by version: $Version"
$alreadyInstalled = $alreadyInstalled | Where-Object Version -EQ $Version
}
if ($Prerelease) {
Write-Verbose 'Filtering by prerelease'
$alreadyInstalled = $alreadyInstalled | Where-Object Prerelease -EQ $Prerelease
}
Write-Verbose 'Already installed:'
Write-Verbose ($alreadyInstalled | Format-Table)
if (-not $alreadyInstalled) {
$params = @{
Name = $Name
Repository = 'PSGallery'
TrustRepository = $true
Prerelease = $Prerelease
}
if ($Version) {
$params['Version'] = $Version
}
$Count = 5
$Delay = 10
for ($i = 1; $i -le $Count; $i++) {
try {
Install-PSResource @params -ErrorAction Stop
break
} catch {
Write-Warning $_.Exception.Message
if ($i -eq $Count) {
throw $_
}
Start-Sleep -Seconds $Delay
}
}
}

$alreadyImported = Get-Module -Name $Name
Write-Verbose 'Already imported:'
Write-Verbose ($alreadyImported | Format-Table)
if (-not $alreadyImported) {
Write-Verbose "Importing module: $Name"
Import-Module -Name $Name
}

$providedToken = -not [string]::IsNullOrEmpty($env:GITHUB_ACTION_INPUT_Token)
$providedClientID = -not [string]::IsNullOrEmpty($env:GITHUB_ACTION_INPUT_ClientID)
$providedPrivateKey = -not [string]::IsNullOrEmpty($env:GITHUB_ACTION_INPUT_PrivateKey)
$moduleStatus = [pscustomobject]@{
Name = $Name
Version = [string]::IsNullOrEmpty($Version) ? 'latest' : $Version
Prerelease = $Prerelease
'Already installed' = $null -ne $alreadyInstalled
'Already imported' = $null -ne $alreadyImported
'Provided Token' = $providedToken
'Provided ClientID' = $providedClientID
'Provided PrivateKey' = $providedPrivateKey
}
Write-Verbose ($moduleStatus | Format-List)
if ($VerbosePreference -eq 'Continue') {
Write-Output '::endgroup::'
Write-Output '┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛'
}
} catch {
throw $_
}

Write-Debug "[$scriptName] - End"
108 changes: 0 additions & 108 deletions scripts/main.ps1

This file was deleted.

21 changes: 9 additions & 12 deletions scripts/outputs.ps1
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
[CmdletBinding()]
param()

begin {
$DebugPreference = 'SilentlyContinue'
$VerbosePreference = 'SilentlyContinue'
Write-Debug '[outputs] - Start'
}
$scriptName = $MyInvocation.MyCommand.Name
Write-Debug "[$scriptName] - Start"

process {
try {
Write-Debug "[outputs] - ShowOutput: $env:GITHUB_ACTION_INPUT_ShowOutput"
Write-Debug "[$scriptName] - ShowOutput: $env:GITHUB_ACTION_INPUT_ShowOutput"
if ($env:GITHUB_ACTION_INPUT_ShowOutput -ne 'true') {
return
}

$result = (Get-GitHubOutput).result
Write-Debug "[outputs] - Result: $(-not $result)"
Write-Debug "[$scriptName] - Result: $(-not $result)"
if (-not $result) {
return
}
Write-Host '┏━━━━━┫ GitHub-Script ┣━━━━━┓'
$title = "┏━━━━━┫ $Name ┣━━━━━┓"
Write-Output $title
LogGroup ' - Outputs' {
if ([string]::IsNullOrEmpty($env:GITHUB_ACTION)) {
Write-GitHubWarning 'Outputs cannot be accessed as the step has no ID.'
Expand All @@ -32,12 +31,10 @@ process {
$result | Format-List
Write-Host "Access outputs using `${{ fromJson(steps.$env:GITHUB_ACTION.outputs.result).<output-name> }}"
}
Write-Host '┗━━━━━━━━━━━━━━━━━━━━━━━━━━━┛'
$endingFence = '┗' + ('━' * ($title.Length - 2)) + '┛'
Write-Output $endingFence
} catch {
throw $_
}
}

end {
Write-Debug '[outputs] - End'
}
Write-Debug "$scriptName - End"
Loading