TeamCity
 
You are viewing the documentation for an earlier version of TeamCity.

File Content Replacer

Last modified: 20 April 2023

File Content Replacer, the build feature available since TeamCity 9.1, p rocesses text files by performing regular expression replacements before a build. After the build, it r estores the file content to the original state.

The common case of using File Content Replacer is replacing one attribute at a time in particular files , e.g. it can be used to patch files with the build number.

You can add more than one File Content Replacer build feature if you wish to:

  • replace more than one attribute,

  • replace one and the same attribute in different files/projects with different values.

This feature extends the capabilities provided by AssemblyInfo Patcher.

Check the Adding Build Features section for notes on how to add a build feature.

File Content Replacer Settings



You can specify the values manually or use value presets for replacement, which can be edited if needed.

Templates



This section lists the available replacement templates.

.NET templates



The templates for replacing the following Assembly attributes are provided (listed in comparison with AssemblyInfo Patcher):

MFC templates



The templates for replacing the following MFC C++ resource keys are are provided:

  • FileDescription

  • CompanyName

  • ProductName

  • LegalCopyright

  • FileVersion*

  • ProductVersion*

Xcode templates



The templates for replacing the following https://developer.apple.com/library/ios/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html in Info.plist files are provided:

  • CFBundleVersion

  • CFBundleShortVersionString

  • or both CFBundleVersion and CFBundleShortVersionString at the same time

Examples



Extending an attribute value with a custom suffix



Suppose you do not want to replace your AssemblyConfiguration with a fixed literal, but want to preserve your AssemblyConfiguration from AssemblyInfo.cs and just extend it with a custom suffix, e.g.: [assembly: AssemblyConfiguration("${AssemblyConfiguration} built by TeamCity")])

Do the following: For changing complex regex patterns, this external tool might be useful.

Patching only specified files



The default AssemblyInfo templates follow the usual Visual Studio project/solution layout; but a lot of information may be shared across multiple projects and can reside in a shared file (e.g. CommonAssemblyInfo.cs).

Suppose you want to patch this shared file only; or you want to patch AssemblyInfo.cs files on a per-project bassis.

Do the following:

  1. Load the AssemblyInfo template corresponding to the attribute you are trying to process (e.g. AssemblyVersion)

  2. Change the list of file paths in the Wildcards field from the default * /Properties/AssemblyInfo.cs to * /CommonAssemblyInfo.cs or list several files comma- or new-line separated files here, e.g. myproject1/Properties/AssemblyInfo.cs, myproject2/Properties/AssemblyInfo.cs.

Specifying path patterns which contain spaces



Spaces are usually considered a part of the pattern, unless they follow a comma, as the comma is recognised as a delimiter.

Note that the TeamCity server UI trims leading and trailing spaces in input fields, so a single-line pattern like <spaces>foo.bar will become foo.bar upon save. The following workarounds are available:

Changing only the last version part / build number of the AssemblyVersion attribute:



Suppose, your AssemblyVersion in AssemblyInfo.cs is Major.Minor.Revision.Build (set to 1.2.3.*) , and you want to replace the Build portion (following the last dot (the *) only.

  1. Load the AssemblyVersion in AssemblyInfo (C#)_ template and change the default pattern:

    (^\s*\[\s*assembly\s*:\s*((System\s*\.)?\s*Reflection\s*\.)?\s*AssemblyVersion(Attribute)?\s*\(\s*@?\")(([0-9\*])+\.?)+(\"\s*\)\s*\])

    to

    (^\s*\[\s*assembly\s*:\s*((System\s*\.)?\s*Reflection\s*\.)?\s*AssemblyVersion(Attribute)?\s*\(\s*@?\")(([0-9\*]+\.)+)[0-9\*]+(\"\s*\)\s*\])

    . and change the default replacement:

    $1\%build.number%$7

    to

    $1$5\%build.number%$7

    .