build.ps1

<#
.SYNOPSIS
    Build, test, analyze, and publish helper script for ggps.

.DESCRIPTION
    Automates running tests, linting with PSScriptAnalyzer, validating the manifest,
    and publishing the module to the PowerShell Gallery.

.PARAMETER Test
    Runs Pester tests.

.PARAMETER Analyze
    Runs PSScriptAnalyzer linting.

.PARAMETER Publish
    Publishes the module to the PowerShell Gallery using Publish-Module.

.PARAMETER NuGetApiKey
    The PowerShell Gallery API key used when -Publish is specified.

.PARAMETER WhatIf
    Simulates the publishing operation without actually uploading.

.EXAMPLE
    .\build.ps1 -Test -Analyze

.EXAMPLE
    .\build.ps1 -Publish -NuGetApiKey "oy2..."

.EXAMPLE
    .\build.ps1 -Publish -WhatIf
#>

[CmdletBinding(SupportsShouldProcess = $true)]
param (
    [Parameter()]
    [switch]$Test,

    [Parameter()]
    [switch]$Analyze,

    [Parameter()]
    [switch]$Publish,

    [Parameter()]
    [string]$NuGetApiKey = $env:PSGALLERY_API_KEY
)

$ErrorActionPreference = 'Stop'
$repoRoot = $PSScriptRoot
$manifestPath = Join-Path $repoRoot 'ggps.psd1'
$scriptPath = Join-Path $repoRoot 'ggps.psm1'
$testsPath = Join-Path $repoRoot 'Tests\ggps.Tests.ps1'

# Default behavior: run Test and Analyze if no switch provided
if (-not $Test -and -not $Analyze -and -not $Publish) {
    $Test = $true
    $Analyze = $true
}

Write-Host "=== ggps Build & Maintenance Tool ===" -ForegroundColor Cyan

# 1. Manifest Validation
Write-Host "`n[1/4] Validating Module Manifest..." -ForegroundColor Yellow
$manifest = Test-ModuleManifest -Path $manifestPath
Write-Host "Manifest OK: $($manifest.Name) version $($manifest.Version)" -ForegroundColor Green

# 2. Script Analysis
if ($Analyze) {
    Write-Host "`n[2/4] Running PSScriptAnalyzer..." -ForegroundColor Yellow
    if (Get-Module -ListAvailable -Name PSScriptAnalyzer) {
        $issues = Invoke-ScriptAnalyzer -Path $scriptPath -Severity Error, Warning
        if ($issues) {
            $issues | Format-Table -AutoSize
            throw "PSScriptAnalyzer found issues."
        }
        Write-Host "PSScriptAnalyzer passed cleanly." -ForegroundColor Green
    } else {
        Write-Warning "PSScriptAnalyzer module is not installed. Skipping analysis."
    }
}

# 3. Pester Tests
if ($Test) {
    Write-Host "`n[3/4] Running Pester Tests..." -ForegroundColor Yellow
    if (Get-Module -ListAvailable -Name Pester) {
        $result = Invoke-Pester -Path $testsPath -Output Detailed -PassThru
        if ($result.FailedCount -gt 0) {
            throw "$($result.FailedCount) test(s) failed."
        }
        Write-Host "All tests passed successfully." -ForegroundColor Green
    } else {
        Write-Warning "Pester module is not installed. Skipping tests."
    }
}

# 4. Publish
if ($Publish) {
    Write-Host "`n[4/4] Publishing to PowerShell Gallery..." -ForegroundColor Yellow
    if ($PSCmdlet.ShouldProcess("PowerShell Gallery", "Publish-Module ggps v$($manifest.Version)")) {
        if (-not $NuGetApiKey) {
            throw "NuGetApiKey is required to publish. Provide -NuGetApiKey or set `$env:PSGALLERY_API_KEY."
        }
        Publish-Module -Path $repoRoot -NuGetApiKey $NuGetApiKey -Verbose
        Write-Host "Module successfully published to PowerShell Gallery!" -ForegroundColor Green
    }
}

Write-Host "`nDone!" -ForegroundColor Cyan