.github/scripts/New-SmartRelease.ps1

<#
.SYNOPSIS
    Creates a complete GitHub Release with semantic smart tags using K.PSGallery.Smartagr.
 
.DESCRIPTION
    This script installs K.PSGallery.Smartagr from PSGallery and uses its New-SmartRelease
    function to create a complete release with the proven Draft → Smart Tags → Publish workflow.
 
    The function:
    1. Creates a GitHub Release as DRAFT (safe, reversible)
    2. Creates semantic smart tags (v1, v1.2, latest, etc.)
    3. Publishes the release only if all steps succeed
 
.PARAMETER TargetVersion
    The semantic version for the release (e.g., "v1.2.0" or "1.2.3").
 
.PARAMETER RepositoryPath
    Path to the Git repository. Defaults to current directory.
 
.PARAMETER ReleaseNotesFile
    Optional path to a file containing release notes.
 
.PARAMETER ModuleName
    The module name for generating installation instructions in release notes.
 
.OUTPUTS
    Writes detailed status to GitHub Actions step summary ($env:GITHUB_STEP_SUMMARY).
    Throws on failure.
 
.EXAMPLE
    ./New-SmartRelease.ps1 -TargetVersion "v1.2.0" -ModuleName "K.PSGallery.ManifestVersioning"
    # Creates complete release with smart tags
 
.EXAMPLE
    ./New-SmartRelease.ps1 -TargetVersion "2.0.0" -ReleaseNotesFile "./CHANGELOG.md"
    # Creates release with custom notes from file
 
.NOTES
    Requires:
    - K.PSGallery.Smartagr module (auto-installed from PSGallery)
    - GitHub CLI (gh) installed and authenticated
    - Repository must be hosted on GitHub
    Cross-platform: Windows, Linux, macOS
#>

[CmdletBinding()]
param(
    [Parameter(Mandatory)]
    [ValidatePattern('^v?\d+\.\d+\.\d+(-[\w\.]+)?$')]
    [string]$TargetVersion,

    [Parameter()]
    [string]$RepositoryPath = $PWD.Path,

    [Parameter()]
    [string]$ReleaseNotesFile,

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

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

# Normalize version to have v prefix
if (-not $TargetVersion.StartsWith('v')) {
    $TargetVersion = "v$TargetVersion"
}

$versionNumber = $TargetVersion.TrimStart('v')

# Install K.PSGallery.Smartagr from PSGallery
Write-Output '📦 Installing K.PSGallery.Smartagr from PSGallery...'
Install-Module -Name K.PSGallery.Smartagr -Scope CurrentUser -Force -AllowClobber
Import-Module K.PSGallery.Smartagr -Force

Write-Output "🚀 Creating smart release for $TargetVersion"
Write-Output "📁 Repository path: $RepositoryPath"

# Generate release notes
$releaseDate = Get-Date -Format 'MMMM dd, yyyy'
$releaseNotes = @"
## 🎉 Release $TargetVersion
 
> Released on $releaseDate
 
### 📦 Installation
 
``````powershell
# Install from PowerShell Gallery
Install-Module -Name $ModuleName -RequiredVersion $versionNumber
 
# Or update existing installation
Update-Module -Name $ModuleName -RequiredVersion $versionNumber
``````
 
### 🔗 Links
 
- [📦 PowerShell Gallery](https://www.powershellgallery.com/packages/$ModuleName/$versionNumber)
- [📋 Source Code](https://github.com/GrexyLoco/$ModuleName)
 
---
*Generated by [K.PSGallery.Smartagr](https://github.com/GrexyLoco/K.PSGallery.Smartagr)*
"@


# Create the smart release with named parameters
try {
    if ($ReleaseNotesFile -and (Test-Path $ReleaseNotesFile)) {
        $result = New-SmartRelease `
            -TargetVersion $TargetVersion `
            -RepositoryPath $RepositoryPath `
            -ReleaseNotesFile $ReleaseNotesFile `
            -PushToRemote
    }
    else {
        $result = New-SmartRelease `
            -TargetVersion $TargetVersion `
            -RepositoryPath $RepositoryPath `
            -ReleaseNotes $releaseNotes `
            -PushToRemote
    }

    if ($result.Success) {
        # Write success summary
        "## ✅ Smart Release Created Successfully" >> $env:GITHUB_STEP_SUMMARY
        "" >> $env:GITHUB_STEP_SUMMARY
        "**Version:** ``$TargetVersion``" >> $env:GITHUB_STEP_SUMMARY
        "**Release URL:** $($result.ReleaseUrl)" >> $env:GITHUB_STEP_SUMMARY
        "" >> $env:GITHUB_STEP_SUMMARY
        "### 🏷️ Tags Created" >> $env:GITHUB_STEP_SUMMARY
        foreach ($tag in $result.TagsCreated) {
            "- ``$tag``" >> $env:GITHUB_STEP_SUMMARY
        }
        "" >> $env:GITHUB_STEP_SUMMARY
        "🔗 *Powered by [K.PSGallery.Smartagr](https://github.com/GrexyLoco/K.PSGallery.Smartagr)*" >> $env:GITHUB_STEP_SUMMARY

        Write-Output "✅ Smart release created successfully!"
        Write-Output "🔗 Release URL: $($result.ReleaseUrl)"
    }
    else {
        throw "Smart release creation failed. Check the logs for details."
    }
}
catch {
    "## ❌ Smart Release Failed" >> $env:GITHUB_STEP_SUMMARY
    "" >> $env:GITHUB_STEP_SUMMARY
    "**Error:** $($_.Exception.Message)" >> $env:GITHUB_STEP_SUMMARY
    throw
}