使用powershell更新程序集信息

问题描述 投票:0回答:2

我想更新 teamcity checkout 目录中的所有 assemblyinfo.cs。引用了这篇文章从AssemblyInfo.cs获取并替换AssemblyVersion。看起来缺少一些逻辑并且文件没有更新。 它应该更新行 [程序集:AssemblyVersion(“1.4.0.0”)] 到 [程序集:AssemblyVersion("1.4.0.%build.counter%")]

$BuildCounter = "%build.counter%"
$pattern = '\[assembly: AssemblyVersion\("(.*)"\)\]'enter code here
Get-ChildItem -r -filter AssemblyInfo.cs | foreach-object { $name = $_.FullName}
{

(get-content $name ) | ForEach-Object{
    if($_ -match $pattern){
        # We have found the matching line
        # Edit the version number and put back.
        $fileVersion = [version]$matches[1]
        $newVersion = "{0}.{1}.{2}.{3}" -f $fileVersion.Major, $fileVersion.Minor, $fileVersion.Build, $BuildCounter 
        '[assembly: AssemblyVersion("{0}")]' -f $newVersion
    } else {
        # Output line as is
        $_
    }
} | Set-Content $name

}`enter code here`
powershell teamcity
2个回答
7
投票

没关系,刚刚开始工作,看起来像语法问题

$BuildCounter = "build.counter"
$pattern = '\[assembly: AssemblyVersion\("(.*)"\)\]'
$AssemblyFiles = Get-ChildItem . AssemblyInfo.cs -rec


foreach ($file in $AssemblyFiles)

{

(Get-Content $file.PSPath) | ForEach-Object{
    if($_ -match $pattern){
        # We have found the matching line
        # Edit the version number and put back.
        $fileVersion = [version]$matches[1]
        $newVersion = "{0}.{1}.{2}.{3}" -f $fileVersion.Major, $fileVersion.Minor, $fileVersion.Build, $BuildCounter 
        '[assembly: AssemblyVersion("{0}")]' -f $newVersion
    } else {
        # Output line as is
        $_
    }
} | Set-Content $file.PSPath

}
Write-Host "##teamcity[buildNumber '$newVersion']"

0
投票

您根本不需要这样的脚本,TeamCity 内置了此功能。它被称为AssemblyInfo Patcher

https://www.jetbrains.com/help/teamcity/ assemblyinfo-patcher.html

© www.soinside.com 2019 - 2024. All rights reserved.