2

I want to update all assemblyinfo.cs in teamcity checkout directory. Referred this post Get and Replace AssemblyVersion from AssemblyInfo.cs. Looks like some logic is missing and file is not getting updated. It should update line [assembly: AssemblyVersion("1.4.0.0")] to [assembly: 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`
Ashish Mishra
  • 411
  • 5
  • 25

1 Answers1

7

Never Mind, Just got it working, Looks like syntax problem

$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']"
Ashish Mishra
  • 411
  • 5
  • 25