publish.ps1

<#
.SYNOPSIS
    Local PowerShell Gallery Publisher for LocalPilot.
.DESCRIPTION
    Sets clean NuGet caches in C:\temp, strips any dehydrated OneDrive paths from PSModulePath,
    and publishes the module using the PSGALLERY_API_KEY environment variable.
#>

$env:TEMP = "C:\temp"
$env:TMP = "C:\temp"
$env:NUGET_PACKAGES = "C:\temp\nuget_cache"
$env:NUGET_HTTP_CACHE_PATH = "C:\temp\nuget_http_cache"

foreach ($dir in @($env:TEMP, $env:NUGET_PACKAGES, $env:NUGET_HTTP_CACHE_PATH)) {
    if (-not (Test-Path $dir)) { New-Item -ItemType Directory -Path $dir -Force | Out-Null }
}

# Strip any dehydrated OneDrive module paths to prevent 0x8007016A errors
$cleanPaths = ($env:PSModulePath -split ';') | Where-Object { $_ -notlike "*OneDrive*" }
$env:PSModulePath = $cleanPaths -join ';'

$apiKey = [Environment]::GetEnvironmentVariable("PSGALLERY_API_KEY", "User")
if (-not $apiKey) { $apiKey = [Environment]::GetEnvironmentVariable("PSGALLERY_API_KEY", "Process") }
if (-not $apiKey) { $apiKey = [Environment]::GetEnvironmentVariable("PSGALLERY_API_KEY", "Machine") }
if (-not $apiKey) { $apiKey = $env:PSGALLERY_API_KEY }

if ($apiKey) {
    Write-Host "Publishing LocalPilot from '$PSScriptRoot' to PowerShell Gallery..." -ForegroundColor Cyan
    Import-Module PowerShellGet -Force
    Publish-Module -Path $PSScriptRoot -NuGetApiKey $apiKey -Force -Verbose
    Write-Host "LocalPilot published successfully!" -ForegroundColor Green
} else {
    Write-Error "PSGALLERY_API_KEY environment variable not found in User, Process, or Machine scopes."
}