Set-IncreaseVersion.ps1
function Set-IncreaseVersion { <# .SYNOPSIS 修改 .csproj 的 Version 值 .DESCRIPTION Set-IncreaseVersion -filePath "./abc.csproj" -xpath "/Project/PropertyGroup/Version" -segment "Build" #> param ( [Parameter(Mandatory)][string]$filePath, [Parameter(Mandatory)][string]$xpath, [Parameter(Mandatory)][ValidateSet("Major", "Minor", "Build", "Revision")][string]$segment ) $versionXml = [xml](get-content $filePath) $appVersionNode = $versionXml.SelectSingleNode($xpath) $ver = [Version]::Parse($appVersionNode.InnerXml) $verNew = switch ( $segment ) { "Major" { New-Object -TypeName System.Version -ArgumentList ($ver.Major + 1), $ver.Minor, $ver.Build} "Minor" { New-Object -TypeName System.Version -ArgumentList $ver.Major, ($ver.Minor + 1), $ver.Build } "Build" { New-Object -TypeName System.Version -ArgumentList $ver.Major, $ver.Minor, ($ver.Build + 1) } default { New-Object -TypeName System.Version -ArgumentList $ver.Major, $ver.Minor, ($ver.Build + 1) } } $appVersionNode.RemoveAll() $appVersionNode.AppendChild($versionXml.CreateTextNode($verNew.ToString())) | out-null $versionXml.Save((Resolve-Path $filePath)) } |