functions/Update-ModuleVersion.ps1

function Update-ModuleVersion {
    param (
        [Parameter(Mandatory = $true)]
        [string]$Path,

        [string]$Version,

        [string]$ReleaseNote,

        [switch]$AutoIncrementRevision
    )

    $Path = (Resolve-Path -Path $Path).Path

    if (-not $moduleName) {
        $moduleName = Split-Path -Path $Path -Leaf
    }

    $moduleManifestPath = Join-Path -Path $Path -ChildPath "$moduleName.psd1"
    $csprojPath = Join-Path -Path $Path -ChildPath "$moduleName.csproj"

    if (-not (Test-Path $moduleManifestPath)) {
        throw "Module manifest '$moduleManifestPath' not found."
    }
    if (-not (Test-Path $csprojPath)) {
        throw "CSPROJ file '$csprojPath' not found."
    }

    # --- PSD1 laden und Version holen ---
    $psd1Text = Get-Content $moduleManifestPath -Raw
    $currentVersionMatch = [regex]::Match($psd1Text, "ModuleVersion\s*=\s*'(?<ver>\d+\.\d+\.\d+)'")
    if (-not $currentVersionMatch.Success) {
        throw "Could not find current ModuleVersion in psd1."
    }
    $currentVersion = [version]$currentVersionMatch.Groups["ver"].Value

    # --- Neue Version bestimmen ---
    if ($Version) {
        $newVersion = $Version
    } elseif ($AutoIncrementRevision) {
        $newVersion = "{0}.{1}.{2}" -f $currentVersion.Major, $currentVersion.Minor, ($currentVersion.Build + 1)
    } else {
        throw "Specify either -Version or -AutoIncrementRevision."
    }

    # --- PSD1 aktualisieren ---
    $psd1Text = $psd1Text -replace "ModuleVersion\s*=\s*'[^']+'", "ModuleVersion = '$newVersion'"

    if ($ReleaseNote) {
        $releaseNoteBlock = "$newVersion`n$ReleaseNote"

        # Falls ReleaseNotes existiert, ersetzen
        if ($psd1Text -match "ReleaseNotes\s*=\s*'((?:.|\n)*?)'") {
            $existingNotes = $matches[1].Trim()
            $combinedNotes = "$releaseNoteBlock`n`n$existingNotes".Trim()
            $escapedCombined = $combinedNotes -replace "'", "''"  # Single quotes doppeln
            $psd1Text = [regex]::Replace($psd1Text, "ReleaseNotes\s*=\s*'(?:.|\n)*?'", "ReleaseNotes = '$escapedCombined'")
        } else {
            # Einfügen innerhalb von PrivateData -> PSData
            $psd1Text = $psd1Text -replace '(?s)(PSData\s*=\s*@\{\s*)', "`$1`n ReleaseNotes = '$releaseNoteBlock'"
            #$psd1Text = $psd1Text -replace "(\r?\n)+\z", ''
        }
    }

    [System.IO.File]::WriteAllText($moduleManifestPath, $psd1Text.TrimEnd("`r", "`n"), [System.Text.Encoding]::UTF8)

    # --- CSPROJ aktualisieren ---
    $csprojText = Get-Content $csprojPath -Raw
    $semver = [version]$newVersion
    $newAssemblyVersion = "$($semver.Major).$($semver.Minor).$($semver.Build).0"

    $csprojText = [regex]::Replace($csprojText, '<(?<tag>Version|AssemblyVersion|FileVersion)>[^<]+</\k<tag>>', {
        param($m)
        "<$($m.Groups["tag"].Value)>$newAssemblyVersion</$($m.Groups["tag"].Value)>"
    })
    #$csprojText = $csprojText -replace "(\r?\n)+\z", ''

    [System.IO.File]::WriteAllText($csprojPath, $csprojText.TrimEnd("`r", "`n"), [System.Text.Encoding]::UTF8)

    Write-Host "✔ Modulversion aktualisiert auf $newVersion" -ForegroundColor Green
    if ($ReleaseNote) {
        Write-Host "➕ ReleaseNotes hinzugefügt: $ReleaseNote" -ForegroundColor DarkCyan
    }
}