.github/scripts/Publish-ToGitHubPackages.ps1

<#
.SYNOPSIS
    Publishes the PowerShell module to GitHub Packages.
 
.DESCRIPTION
    This script registers the GitHub Packages NuGet repository and publishes
    the module using K.PSGallery.PackageRepoProvider. It provides detailed
    output to GitHub Actions step summary.
 
    The script:
    1. Installs K.PSGallery.PackageRepoProvider from PSGallery (or bootstrap)
    2. Registers the GitHub Packages repository
    3. Publishes the module to GitHub Packages
 
.PARAMETER SecureToken
    GitHub token for authentication (from GITHUB_TOKEN secret).
    Must have packages:write scope.
 
.PARAMETER Version
    The version to publish. Should match the module manifest version.
 
.PARAMETER ModulePath
    Path to the module directory. Defaults to repository root (two levels up from script).
 
.PARAMETER ModuleName
    The name of the module being published. Used for summary output.
    Defaults to "K.PSGallery.ManifestVersioning".
 
.PARAMETER Owner
    GitHub organization/user that owns the package registry.
    Defaults to "GrexyLoco".
 
.OUTPUTS
    Writes detailed status to GitHub Actions step summary ($env:GITHUB_STEP_SUMMARY).
    Throws on failure.
 
.EXAMPLE
    $token = ConvertTo-SecureString $env:GITHUB_TOKEN -AsPlainText -Force
    ./Publish-ToGitHubPackages.ps1 -SecureToken $token -Version "1.2.0"
    # Publishes module to GitHub Packages
 
.NOTES
    Requires:
    - K.PSGallery.PackageRepoProvider module (installed from PSGallery or bootstrapped)
    - GITHUB_TOKEN with packages:write scope
    Cross-platform: Windows, Linux, macOS
#>

[CmdletBinding()]
param(
    [Parameter(Mandatory)]
    [SecureString]$SecureToken,

    [Parameter(Mandatory)]
    [ValidatePattern('^\d+\.\d+\.\d+(-[\w\.]+)?$')]
    [string]$Version,

    [Parameter()]
    [string]$ModulePath,

    [Parameter()]
    [string]$ModuleName = 'K.PSGallery.ManifestVersioning',

    [Parameter()]
    [string]$Owner = 'GrexyLoco'
)

Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
$InformationPreference = 'Continue'

# Determine module path if not specified
if (-not $ModulePath) {
    $ModulePath = Join-Path $PSScriptRoot '../..'
}

$ModulePath = Resolve-Path $ModulePath

Write-Information "🚀 Publishing $ModuleName to GitHub Packages"
Write-Information "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
Write-Information "📦 Module: $ModuleName"
Write-Information "🏷️ Version: $Version"
Write-Information "📁 Path: $ModulePath"

# Install PackageRepoProvider if not already available
if (-not (Get-Module -ListAvailable -Name K.PSGallery.PackageRepoProvider)) {
    Write-Information "📦 Installing K.PSGallery.PackageRepoProvider from PSGallery..."
    Install-Module -Name K.PSGallery.PackageRepoProvider -Scope CurrentUser -Force -AllowClobber
}
Import-Module K.PSGallery.PackageRepoProvider -Force

# Register GitHub Packages repository
$registryUri = "https://nuget.pkg.github.com/$Owner/index.json"
Write-Information "📦 Registering GitHub Packages repository..."
Write-Information " Registry: $registryUri"

try {
    Register-PackageRepo -Uri $registryUri -SecureToken $SecureToken -Verbose
    Write-Information "✅ Repository registered successfully"
}
catch {
    Write-Warning "⚠️ Repository registration warning: $($_.Exception.Message)"
    Write-Information " Continuing with publish attempt..."
}

# Publish the module
Write-Information ""
Write-Information "📤 Publishing module to GitHub Packages..."

try {
    Publish-Package -Path $ModulePath -RegistryUri $registryUri -SecureToken $SecureToken -Verbose
    
    Write-Information ""
    Write-Information "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
    Write-Information "✅ Package published successfully!"
    
    # Write success summary
    $summary = @"
## ✅ Published to GitHub Packages
 
| Property | Value |
|----------|-------|
| 📦 Module | **$ModuleName** |
| 🏷️ Version | **$Version** |
| 🔗 Registry | ``$registryUri`` |
 
### 📦 Installation
 
``````powershell
# Register GitHub Packages (if not already registered)
`$cred = Get-Credential
Register-PSResourceRepository -Name GitHubPackages ``
    -Uri '$registryUri' ``
    -Trusted
 
# Install the module
Install-PSResource -Name $ModuleName ``
    -Version $Version ``
    -Repository GitHubPackages ``
    -Credential `$cred
``````
 
### 🔗 Links
 
- [📦 GitHub Packages](https://github.com/orgs/$Owner/packages?repo_name=$ModuleName)
"@

    
    $summary >> $env:GITHUB_STEP_SUMMARY
}
catch {
    $errorSummary = @"
## ❌ GitHub Packages Publish Failed
 
**Error:** $($_.Exception.Message)
 
**Module:** $ModuleName
**Version:** $Version
"@

    $errorSummary >> $env:GITHUB_STEP_SUMMARY
    
    Write-Error "❌ Publish failed: $($_.Exception.Message)"
    throw
}