【问题标题】:How to replace third position of assembly version in the PowerShell script如何在 PowerShell 脚本中替换程序集版本的第三个位置
【发布时间】:2016-04-29 19:17:40
【问题描述】:

我有 powershell 脚本来替换程序集版本,但我必须在第三个位置更改版本号 LIKE [程序集:AssemblyVersion("1.0.20.1")] 到 [程序集:AssemblyVersion("1.0.21.1")]

这就是我所拥有的,它会增加最后一个位置:

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

【问题讨论】:

    标签: powershell .net-assembly


    【解决方案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"),就像你想要的那样。

    【讨论】:

      猜你喜欢
      • 2020-07-14
      • 1970-01-01
      • 2020-12-02
      • 1970-01-01
      • 2021-06-09
      • 1970-01-01
      • 1970-01-01
      • 2019-11-14
      • 1970-01-01
      相关资源
      最近更新 更多