Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you format text in MSBuild?

Specifically, I am looking to zero pad a number to create a string based label. i.e. build 7 into build 007. You can easily add strings together, but in all my searches on formatting, padding, strings, etc... I have not been able to find any references.

Example of what I am working with.

<PropertyGroup>
  <FileParserVersion>File Parser $(Major).$(Minor).$(Build) Build $(Revision)</FileParserVersion>
  <VersionComment>Automated build: $(FileParserVersion)</VersionComment>
</PropertyGroup>

This is generated: FILEPARSER_1_0_3_BUILD_7
What is preferred: FILEPARSER_1_0_3_BUILD_007

like image 318
Shire Avatar asked Dec 10 '22 18:12

Shire


1 Answers

In 4.0+ you can do it in one line with Property Functions (and on MSDN)

$([System.String]::Format('FILEPARSER_$(Major)_$(Minor)_$(Build)_BUILD_{0:000}', $([MSBuild]::Add($(Revision), 0))))

Unfortunately the bogus "Add" is necessary to trick MSBuild to coerce $(Revision) to a number before it coerces it into the object expected by String.Format. If I don't do that it uses a string, and the padding doesn't work. The coercion inside MSBuild could be a bit smarter here.

like image 199
cheerless bog Avatar answered Feb 03 '23 22:02

cheerless bog