[CmdletBinding(DefaultParameterSetName = "Patch")] param( [Parameter(Mandatory = $true, ParameterSetName = "Major")] [switch]$Major, [Parameter(Mandatory = $true, ParameterSetName = "Minor")] [switch]$Minor, [Parameter(Mandatory = $true, ParameterSetName = "Patch")] [switch]$Patch, [string]$Project = "src/RgbLedStripController/RgbLedStripController.csproj" ) $ErrorActionPreference = "Stop" Set-StrictMode -Version Latest $repoRoot = [System.IO.Path]::GetFullPath((Join-Path $PSScriptRoot "..\..")) $projectPath = if ([System.IO.Path]::IsPathRooted($Project)) { $Project } else { Join-Path $repoRoot $Project } $projectPath = [System.IO.Path]::GetFullPath($projectPath) if (-not (Test-Path -LiteralPath $projectPath)) { throw "Project file not found: $projectPath" } $content = Get-Content -Raw -LiteralPath $projectPath $versionMatch = [System.Text.RegularExpressions.Regex]::Match($content, "(?\d+\.\d+\.\d+)") if (-not $versionMatch.Success) { throw "No semantic value found in project file: $projectPath" } $currentVersion = $versionMatch.Groups["value"].Value $parts = $currentVersion.Split(".") | ForEach-Object { [int]$_ } $newVersion = switch ($PSCmdlet.ParameterSetName) { "Major" { "{0}.0.0" -f ($parts[0] + 1) } "Minor" { "{0}.{1}.0" -f $parts[0], ($parts[1] + 1) } default { "{0}.{1}.{2}" -f $parts[0], $parts[1], ($parts[2] + 1) } } $newAssemblyVersion = "$newVersion.0" function Replace-FirstMatch { param( [string]$Text, [string]$Pattern, [string]$Replacement ) $regex = [System.Text.RegularExpressions.Regex]::new($Pattern) return $regex.Replace($Text, $Replacement, 1) } $content = Replace-FirstMatch $content "[^<]+" "$newVersion" $content = Replace-FirstMatch $content "[^<]+" "$newAssemblyVersion" $content = Replace-FirstMatch $content "[^<]+" "$newAssemblyVersion" $content = Replace-FirstMatch $content "[^<]+" "$newVersion" $utf8NoBom = [System.Text.UTF8Encoding]::new($false) [System.IO.File]::WriteAllText($projectPath, $content, $utf8NoBom) Write-Host ("Version updated: {0} -> {1}" -f $currentVersion, $newVersion) -ForegroundColor Green Write-Host ("Project file: {0}" -f $projectPath)