简体   繁体   English

如何在PowerShell脚本中替换程序集版本的第三个位置

[英]How to replace third position of assembly version in the PowerShell script

I have powershell script to replace assembly versions, but i have to change the version number at third postion LIKE [assembly: AssemblyVersion("1.0.20.1")] to [assembly: AssemblyVersion("1.0.21.1")]我有 powershell 脚本来替换程序集版本,但我必须将第三个位置的版本号更改为 [assembly: AssemblyVersion("1.0.20.1")] 到 [assembly: AssemblyVersion("1.0.21.1")]

This is what I have, which increments last postion:这就是我所拥有的,它增加了最后一个位置:

#
# 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)

I would personally make an object out of it with values for the various parts, then you can increment whichever part you want and restructure it into a string later.我个人会用它的各个部分的值制作一个对象,然后您可以增加您想要的任何部分,然后将其重组为字符串。

$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]
    }
}

Then you can increment as simply as $Version.Third++ to increase the third set.然后你可以简单地增加$Version.Third++来增加第三组。 Then just use a formatted string to spit it back out:然后只需使用格式化的字符串将其吐出:

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

That will produce AssemblyVersion("1.0.21.1") just like you want.这将产生你想要的AssemblyVersion("1.0.21.1")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM