views:

150

answers:

2

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

A: 

Either look in Communty Tasks or write your own ITask (it's simple to do).

Joe
I had looked at those and did not see anything obvious that would do what I needed. So before reinventing the wheel and coding my own task...
Shire
+1  A: 

Consider the following ITask:

using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

namespace My.MSBuild.Tasks
{
    public class FormatRevision : Task
    {
        #region Public Properties

        [Required]
        public int Revision { get; set; }

        [Required]
        public string MajorVersion { get; set; }

        [Output]
        public string OutputVersion { get; private set; }

        #endregion

        #region ITask Methods

        public override bool Execute()
        {
            OutputVersion = string.Format("{0}.{1}"
                , MajorVersion
                , Revision < 10 ?
                    "00" + Revision : Revision < 100 ?
                        "0" + Revision : Revision.ToString());

            Log.LogMessage("Revision: {0} -> Output Version: {1}"
                 , Revision, OutputVersion);

            return true;
        }

        #endregion
    }
}

MSBuild target (formatvesion.proj):

 <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"&gt;

 <Target Name="FormatRevision"> 
    <FormatRevision MajorVersion="$(MajorVersion)" Revision="$(Revision)">
     <Output TaskParameter="OutputVersion" PropertyName="FormattedVersion"/>
     </FormatRevision>
  </Target>

 <UsingTask TaskName="My.MSBuild.Tasks.FormatRevision" AssemblyFile="My.MSBuild.Tasks.dll" /> 

 </Project>

Invoked by command:

msbuild formatvesion.proj /t:FormatRevision /p:MajorVersion=1.0;Revision=7

Alternatively, if you wish to use CreateProperty:

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

<PropertyGroup>
  <PaddedRevision Condition="$(Revision) &lt; 1000">$(Revision)</PaddedRevision> 
  <PaddedRevision Condition="$(Revision) &lt; 100">0$(Revision)</PaddedRevision>  
  <PaddedRevision Condition="$(Revision) &lt; 10">00$(Revision)</PaddedRevision>
</PropertyGroup>

<Target Name="test"> 

    <CreateProperty 
     Value="FILEPARSER_$(Major)_$(Minor)_$(Build)_BUILD_$(PaddedRevision)">
     <Output TaskParameter="Value" PropertyName="MyFileVersion" />
    </CreateProperty>
    <Message Text="$(VersionComment) -> $(MyFileVersion)" />

</Target>
KMoraz
I am not looking to change the version, I am trying to create a text string that will be used as a label in source control and as a folder name for deploying to QA.
Shire
CreateProperty task should do:http://msdn.microsoft.com/en-us/library/63ckb9s9.aspxIf it doesn't, post some snippets of the project you're trying to debug.
KMoraz
I added a snippet in the original with the actual versus desired output.
Shire
See my updated example above. It produces the desired output given the properties in your snippet.
KMoraz
Perfect! Didn't even occur to me to try a bit of clever math!
Shire
/NitpickerNeater to do a Right("000" + Revision.ToString(), 3) ofcourse
Bart Janson