functions/Publish-MyModule.ps1

function Publish-MyModule {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [string]$Path,

        [Parameter(Mandatory = $false)]
        [string]$ExcludeListFile = '.\exclude-files.json',

        [switch]$SkipCleanup
    )

    $Path = (Resolve-Path -Path $Path).Path
    $moduleName = Split-Path -Path $Path -Leaf
    $moduleManifestPath = Join-Path -Path $Path -ChildPath "$moduleName.psd1"

    try {
        $null = Test-ModuleManifest -Path $moduleManifestPath -ErrorAction Stop
    } catch {
        throw "Das Modulmanifest '$moduleManifestPath' ist ungültig oder enthält Fehler: $($_.Exception.Message)"
    }

    # Ausschlussliste laden
    $exclusionPaths = @('bin', 'obj') | ForEach-Object {
        Join-Path -Path $Path -ChildPath $_
    }
    $excludedFiles = @()
    if (Test-Path $ExcludeListFile) {
        try {
            $excludedFiles = Get-Content $ExcludeListFile | ConvertFrom-Json
            $excludedFullNames = @()
            $excludedFiles | ForEach-Object { $excludedFullNames += Join-Path -Path $Path -ChildPath $_ }
        } catch {
            Write-Warning "exclude-files.json konnte nicht gelesen werden."
        }
    }

    # Temporäres Arbeitsverzeichnis erstellen
    $tempDir = Join-Path -Path $env:TMP -ChildPath (New-TemporaryFile).BaseName
    $tempDir = Join-Path $tempDir -ChildPath $moduleName
    New-Item -ItemType Directory -Path $tempDir | Out-Null

    # Nur erlaubte Dateien kopieren
    Get-ChildItem -Path $Path -Recurse | Where-Object {
        $fullName = $_.FullName
        $excluded = ($exclusionPaths | Where-Object { $fullName.StartsWith($_) }) -or ($excludedFullNames -contains $fullName)
        -not $excluded
    } | ForEach-Object {
        $target = Join-Path $tempDir ($_.FullName.Substring($Path.Length).TrimStart('\'))
        New-Item -ItemType Directory -Force -Path (Split-Path $target) | Out-Null
        Copy-Item -Path $_.FullName -Destination $target
    }

    # Bestätigung vor dem Veröffentlichen
    $confirmation = Read-Host "Modul unter '$tempDir' zur PowerShell Gallery veröffentlichen? (y/n)"
    if ($confirmation -ne 'y') {
        Write-Host 'Veröffentlichung abgebrochen.'
        return
    }

    # Modul veröffentlichen
    try {
        $apiKey = Get-Secret -Name NuGetApiKeyBcAdmin -AsPlainText
        Publish-Module -Path $tempDir -NuGetApiKey $apiKey -ErrorAction Stop
        Write-Output "✅ Modul BcAdmin erfolgreich veröffentlicht."
    } catch {
        throw $_
    } finally {
        if (-not $SkipCleanup) {
            Remove-Item -Path $tempDir -Recurse -Force
        }
    }
}