Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to escape backslashes in variable of File content replacer teamcity

For example if I need replace some string to %teamcity.agent.work.dir%\\nd_r\\bin\\isf. But if variable teamcity.agent.work.dir is C:\BuildAgent\work\, it removes all backslashes and replace text to C:BuildAgentwork\nd_r\bin\isf.

How I can escape all backslashes in variable, if I initially don't know about it's value?

\Q%env.NDRIVE%\E\nd_r\bin\isf - it doesn't work.

like image 821
Mois Avatar asked Sep 18 '25 17:09

Mois


2 Answers

TeamCity does not support additional processing of values in %-references, it can only be used "as is".

In your case, possible workarounds are:

  • do not use File content replacer and perform the related logic as a first step in the build in a script where you can handle the escaping, if necessary;
  • escape the value and supply to TeamCity as a parameter in already escaped form. Since File content replacer works before the build steps, this can be done in a previous build in the build chain and the parameter can be used from the build in the chain;
  • write a TeamCity plugin which will provide escaped value for a predefined set of parameters
like image 63
Yaegor Avatar answered Sep 20 '25 08:09

Yaegor


Since TeamCity 2017.1, File Content Replacer can run in the fixed strings mode (similar to that of grep -F), as opposed to the original regex mode:

File Content Replacer in FIXED_STRINGS mode

If you use versioned settings (either XML or Kotlin DSL variant), there's yet another mode available to you (in addition to REGEX and FIXED_STRINGS): REGEX_MIXED. In this mode, the search pattern will still be interpreted as a regular expression, but the replacement text will be quoted so that \ and $ characters will no longer have any special meaning.

If you export your settings to Kotlin, sample File Content Replacer configuration might look like:

features {
    replaceContent {
        fileRules = "**/*"
        pattern = "(?iu)the\h+pattern\h+to\h+search\h+for"
        regexMode = FileContentReplacer.RegexMode.REGEX_MIXED
        replacement = """%teamcity.agent.work.dir%\nd_r\bin\isf"""
    }
}
like image 30
Bass Avatar answered Sep 20 '25 09:09

Bass