具体来说,我期待零填充数字以创建基于字符串的标签。即将构建7构建到构建007.您可以轻松地将字符串添加到一起,但在我对格式化,填充,字符串等的所有搜索中...我无法找到任何引用。
我正在使用的示例。
<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
答案 0 :(得分:6)
在4.0+中,您可以使用Property Functions(和on MSDN)
在一行中完成$([System.String]::Format('FILEPARSER_$(Major)_$(Minor)_$(Build)_BUILD_{0:000}', $([MSBuild]::Add($(Revision), 0))))
不幸的是,伪造“添加”是必要的,以诱骗MSBuild在将数字强制转换为String.Format所期望的对象之前强制$(修订)到一个数字。如果我不这样做它使用一个字符串,填充不起作用。 MSBuild内部的强制措施在这里可能会更加明智。
答案 1 :(得分:4)
考虑以下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目标(formatvesion.proj):
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<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>
由命令调用:
msbuild formatvesion.proj /t:FormatRevision /p:MajorVersion=1.0;Revision=7
或者,如果您想使用CreateProperty:
<PropertyGroup>
<FileParserVersion>File Parser $(Major).$(Minor).$(Build) Build $(Revision)</FileParserVersion>
<VersionComment>Automated build: $(FileParserVersion)</VersionComment>
</PropertyGroup>
<PropertyGroup>
<PaddedRevision Condition="$(Revision) < 1000">$(Revision)</PaddedRevision>
<PaddedRevision Condition="$(Revision) < 100">0$(Revision)</PaddedRevision>
<PaddedRevision Condition="$(Revision) < 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>