modules/devops/Publish-Module.ps1

[CmdletBinding()]
param(
    [Parameter(Mandatory=$true)]
    [string]$NuGetApiKey,
    [switch]$WhatIf
)

$ScriptDir    = Split-Path -Parent $MyInvocation.MyCommand.Path
$GeneratorDir = Split-Path (Split-Path $ScriptDir -Parent) -Parent

Write-Host "================================================" -ForegroundColor DarkCyan
Write-Host " CleanArch - PSGallery Publish " -ForegroundColor Cyan
Write-Host "================================================" -ForegroundColor DarkCyan

# Manifest kontrolu
$manifestPath = Join-Path $GeneratorDir "cleanarch.psd1"
if (-not (Test-Path $manifestPath)) {
    Write-Error "Manifest bulunamadi: $manifestPath"
    exit 1
}

# Versiyon bilgisi
$manifest = Import-PowerShellDataFile $manifestPath
Write-Host "Modul : cleanarch" -ForegroundColor White
Write-Host "Versiyon: $($manifest.ModuleVersion)" -ForegroundColor White
Write-Host ""

# PSGallery, klasor adi ile .psd1 adinin eslesmasini zorunlu kilar.
# generator/ klasoru "cleanarch" adinda olmadigi icin gecici bir klasore kopyaliyoruz.
$tempBase = Join-Path $env:TEMP "cleanarch-publish"
$tempModule = Join-Path $tempBase "cleanarch"

if (Test-Path $tempBase) { Remove-Item $tempBase -Recurse -Force }
New-Item -ItemType Directory -Path $tempModule -Force | Out-Null

Write-Host "Gecici klasore kopyalaniyor: $tempModule" -ForegroundColor DarkGray
Copy-Item -Path "$GeneratorDir\*" -Destination $tempModule -Recurse -Force

# Windows PowerShell 5.1 varsayilan olarak TLS 1.0 kullanir; PSGallery TLS 1.2 gerektirir
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

try {
    if ($WhatIf) {
        Write-Host "[WhatIf] Publish-Module -Path $tempModule" -ForegroundColor Yellow
        Write-Host "WhatIf: Gercek publish yapilmadi." -ForegroundColor Yellow
    } else {
        Write-Host "PSGallery'ye gonderiliyor..." -ForegroundColor Cyan
        Publish-Module -Path $tempModule -NuGetApiKey $NuGetApiKey -Verbose -ErrorAction Stop
        Write-Host ""
        Write-Host "Basariyla yayinlandi! v$($manifest.ModuleVersion)" -ForegroundColor Green
        Write-Host "https://www.powershellgallery.com/packages/cleanarch" -ForegroundColor DarkGray
    }
} catch {
    Write-Error "Publish basarisiz: $_"
    exit 1
} finally {
    Remove-Item $tempBase -Recurse -Force -ErrorAction SilentlyContinue
}