Build/Publish-Module.ps1
|
<#
.SYNOPSIS Publish iFacto.AICodeReview module to PowerShell Gallery .DESCRIPTION This script publishes the module to PowerShell Gallery. Requires a NuGet API key from https://www.powershellgallery.com/account/apikeys .PARAMETER NuGetApiKey NuGet API key for PowerShell Gallery. Can also be set via $env:NUGET_API_KEY .PARAMETER WhatIf Show what would be published without actually publishing .PARAMETER Force Skip confirmation prompt .EXAMPLE .\Publish-Module.ps1 -NuGetApiKey "your-api-key" .EXAMPLE $env:NUGET_API_KEY = "your-api-key" .\Publish-Module.ps1 -Force #> [CmdletBinding(SupportsShouldProcess)] param( [Parameter()] [string]$NuGetApiKey = $env:NUGET_API_KEY, [Parameter()] [switch]$Force ) $ErrorActionPreference = 'Stop' # Get module root directory $moduleRoot = Split-Path $PSScriptRoot -Parent $moduleName = 'iFacto.AICodeReview' $manifestPath = Join-Path $moduleRoot "$moduleName.psd1" Write-Host "========================================" -ForegroundColor Cyan Write-Host " Publishing $moduleName to PowerShell Gallery" -ForegroundColor Cyan Write-Host "========================================" -ForegroundColor Cyan Write-Host "" # Step 1: Validate API key if ([string]::IsNullOrWhiteSpace($NuGetApiKey)) { throw "NuGet API key is required. Set `$env:NUGET_API_KEY or use -NuGetApiKey parameter" } Write-Host "✓ NuGet API key found" -ForegroundColor Green # Step 2: Validate module first Write-Host "" Write-Host "✓ Running build validation..." -ForegroundColor Yellow $buildScript = Join-Path $PSScriptRoot 'Build-Module.ps1' & $buildScript # Step 3: Get module version $manifest = Import-PowerShellDataFile -Path $manifestPath $version = $manifest.ModuleVersion Write-Host "" Write-Host "Module details:" -ForegroundColor Cyan Write-Host " Name: $moduleName" -ForegroundColor White Write-Host " Version: $version" -ForegroundColor White Write-Host " Path: $moduleRoot" -ForegroundColor White # Step 4: Check if version already published Write-Host "" Write-Host "✓ Checking PowerShell Gallery..." -ForegroundColor Yellow try { $published = Find-Module -Name $moduleName -RequiredVersion $version -ErrorAction SilentlyContinue if ($published) { throw "Version $version is already published to PowerShell Gallery. Update version in manifest first." } Write-Host " ✓ Version $version not yet published" -ForegroundColor Green } catch { if ($_.Exception.Message -like "*already published*") { throw } # Module might not exist yet, which is fine Write-Host " ✓ Ready to publish" -ForegroundColor Green } # Step 5: Confirm publication if (-not $Force -and -not $WhatIf) { Write-Host "" $confirm = Read-Host "Publish $moduleName v$version to PowerShell Gallery? (y/N)" if ($confirm -ne 'y' -and $confirm -ne 'Y') { Write-Host "Publication cancelled" -ForegroundColor Yellow return } } # Step 6: Publish Write-Host "" Write-Host "✓ Publishing to PowerShell Gallery..." -ForegroundColor Yellow if ($PSCmdlet.ShouldProcess("$moduleName v$version", "Publish to PowerShell Gallery")) { try { Publish-Module -Path $moduleRoot -NuGetApiKey $NuGetApiKey -Verbose Write-Host "" Write-Host "========================================" -ForegroundColor Green Write-Host " ✓ Successfully Published!" -ForegroundColor Green Write-Host "========================================" -ForegroundColor Green Write-Host "" Write-Host "Module published: $moduleName v$version" -ForegroundColor Cyan Write-Host "Gallery URL: https://www.powershellgallery.com/packages/$moduleName/$version" -ForegroundColor White Write-Host "" Write-Host "Users can now install with:" -ForegroundColor Cyan Write-Host " Install-Module $moduleName -Scope CurrentUser" -ForegroundColor White Write-Host "" Write-Host "Next steps:" -ForegroundColor Cyan Write-Host " 1. Create GitHub release with tag v$version" -ForegroundColor White Write-Host " 2. Update CHANGELOG.md for next version" -ForegroundColor White Write-Host " 3. Consider bumping version in manifest for dev work" -ForegroundColor White Write-Host "" } catch { Write-Host "" Write-Host "========================================" -ForegroundColor Red Write-Host " ✗ Publication Failed" -ForegroundColor Red Write-Host "========================================" -ForegroundColor Red Write-Host "" Write-Host "Error: $_" -ForegroundColor Red Write-Host "" if ($_.Exception.Message -like "*409*" -or $_.Exception.Message -like "*already exists*") { Write-Host "This version may already be published. Check:" -ForegroundColor Yellow Write-Host " https://www.powershellgallery.com/packages/$moduleName" -ForegroundColor White } throw } } |