2016-04-29 28 views
2

我有powershell腳本來替換程序集版本,但我必須在第三個位置更改版本號 LIKE [assembly:AssemblyVersion(「1.0.20.1」)] to [裝配:的AssemblyVersion(「1.0.21.1」)如何在PowerShell腳本中替換程序集版本的第三個位置

這就是我,這遞增上次位置:

# 
# This script will increment the build number in an AssemblyInfo.cs file 
# 

$assemblyInfoPath = "C:\Users\kondas\Desktop\PowerShell\AssemblyInfo.cs" 

$contents = [System.IO.File]::ReadAllText($assemblyInfoPath) 

$versionString = [RegEx]::Match($contents,"(AssemblyFileVersion\("")(?:\d+\.\d+\.\d+\.\d+)(""\))") 
Write-Host ("AssemblyFileVersion: " +$versionString) 

#Parse out the current build number from the AssemblyFileVersion 
$currentBuild = [RegEx]::Match($versionString,"(\.)(\d+)(""\))").Groups[2] 
Write-Host ("Current Build: " + $currentBuild.Value) 

#Increment the build number 
$newBuild= [int]$currentBuild.Value + 1 
Write-Host ("New Build: " + $newBuild) 

#update AssemblyFileVersion and AssemblyVersion, then write to file 


Write-Host ("Setting version in assembly info file ") 
$contents = [RegEx]::Replace($contents, "(AssemblyVersion\(""\d+\.\d+\.\d+\.)(?:\d+)(""\))", ("`${1}" + $newBuild.ToString() + "`${2}")) 
$contents = [RegEx]::Replace($contents, "(AssemblyFileVersion\(""\d+\.\d+\.\d+\.)(?:\d+)(""\))", ("`${1}" + $newBuild.ToString() + "`${2}")) 
[System.IO.File]::WriteAllText($assemblyInfoPath, $contents) 

回答

1

我會親自做一個對象並將其值的各部分,那麼你可以增加你想要的任何部分,並在稍後將其重新構造成字符串。

$Version = $contents | ?{$_ -match 'AssemblyVersion\("(\d+)\.(\d+)\.\(d+)\.(\d+)"\)'}|%{ 
    [PSCustomObject]@{ 
     [int]'First'=$Matches[1] 
     [int]'Second'=$Matches[2] 
     [int]'Third'=$Matches[3] 
     [int]'Fourth'=$Matches[4] 
    } 
} 

然後你就可以增加簡單,如$Version.Third++增加了第三局。然後,只需使用一個格式化字符串吐出背出:

'AssemblyVersion("{0}.{1}.{2}.{3}")' -f $Version.First, $Version.Second, $Version.Third, $Version.Fourth 

這將產生AssemblyVersion("1.0.21.1")就像你想要的。

相關問題