MSBuild ItemGroups 中是否有可用的索引器?
Is there an indexer available in MSBuild ItemGroups?
我正在使用 this stack overflow answer 到 return 构建程序集的 @(Version)
作为 ItemGroup 用于 post-build 事件。
但是,我想分别拥有 Major 和 Minor,因为我只想要 post 构建中的 vMajor.Minor
。为此,我想在 Include
属性中建立索引。
以下有效,但如果主编号或次编号超过 9(仅限个位数),则会中断:
<Target Name="PostBuildMacros">
<GetAssemblyIdentity AssemblyFiles="$(TargetPath)">
<Output TaskParameter="Assemblies" ItemName="Targets" />
</GetAssemblyIdentity>
<ItemGroup>
<Version Include="@(Targets->'%(Version)')" />
<Major Include="@(Version->Substring(0,1))" />
<Minor Include="@(Version->Substring(2,1))" />
</ItemGroup>
</Target>
<PropertyGroup>
<PostBuildEventDependsOn>
$(PostBuildEventDependsOn);
PostBuildMacros;
</PostBuildEventDependsOn>
<PostBuildEvent>
if $(ConfigurationName) == Release copy /y $(TargetPath) $(SolutionDir)..\Builds$(TargetName)_v@(Major).@(Minor)$(TargetExt)
</PostBuildEvent>
</PropertyGroup>
我正在寻找的是更多类似的东西:
<Major Include="@(Version->Split('.')[0])" />
<Minor Include="@(Version->Split('.')[1])" />
或者,仅当确实无法使用索引器时,
<Major Include="@(Targets->Version->Major)" />
<Minor Include="@(Targets->Version->Minor)" />
<Major Include="@(Targets->'%(Version)'->'%(Major)')" />
<Minor Include="@(Targets->'%(Version)'->'%(Minor)')" />
<Major Include="@(Targets->'%(Version.Major)')" />
<Minor Include="@(Targets->'%(Version.Minor)')" />
有什么选项,如果有的话,我必须索引一个数组或以其他方式将 1.0.2838.24877
之类的版本号分成 1
和 0
?
编辑
这现在可以工作了,首先使用 <PropertyGroup>
我可以像 this post 那样利用 Split('.')[n]
,但是 $(Major)
属性($()
)是在 Post-Build 事件中不可用(为什么不可用?我错过了什么吗?)。如果我可以直接在 post-build.
中使用 $(Major)
会更优雅
<Target Name="PostBuildMacros">
<GetAssemblyIdentity AssemblyFiles="$(TargetPath)">
<Output TaskParameter="Assemblies" ItemName="Targets" />
</GetAssemblyIdentity>
<PropertyGroup>
<Version>@(Targets->'%(Version)')</Version>
<Major>$(Version.Split('.')[0])</Major>
<Minor>$(Version.Split('.')[1])</Minor>
<Build>$(Version.Split('.')[2])</Build>
<Revision>$(Version.Split('.')[3])</Revision>
</PropertyGroup>
<ItemGroup>
<Version Include="$(Version)" />
<Major Include="$(Major)" />
<Minor Include="$(Minor)" />
<Build Include="$(Build)" />
<Revision Include="$(Revision)" />
</ItemGroup>
<Message Text="Version: @(Version)" />
<Message Text="Major.Minor.Build.Revision: @(Major).@(Minor).@(Build).@(Revision)" />
</Target>
<PropertyGroup>
<PostBuildEventDependsOn>
$(PostBuildEventDependsOn);
PostBuildMacros;
</PostBuildEventDependsOn>
<PostBuildEvent>
if $(ConfigurationName) == Release copy /y $(TargetPath) $(SolutionDir)..\Builds$(TargetName)_v@(Major).@(Minor)$(TargetExt)
</PostBuildEvent>
</PropertyGroup>
为什么 ItemGroups @()
可用而 PropertyGroups $()
不可用?
这是我得到的最好的结果,我对其他可用的内容或如何在 ItemGroups
中使用索引器知之甚少。
<Target Name="PostBuildMacros">
<GetAssemblyIdentity AssemblyFiles="$(TargetPath)">
<Output TaskParameter="Assemblies" ItemName="Targets" />
</GetAssemblyIdentity>
<PropertyGroup>
<Version>@(Targets->'%(Version)')</Version>
</PropertyGroup>
<ItemGroup>
<Version Include="$(Version)" />
<Major Include="$(Version.Split('.')[0])" />
<Minor Include="$(Version.Split('.')[1])" />
<Build Include="$(Version.Split('.')[2])" />
<Revision Include="$(Version.Split('.')[3])" />
</ItemGroup>
<Message Text="Version: @(Version)" />
<Message Text="Major.Minor.Build.Revision: @(Major).@(Minor).@(Build).@(Revision)" />
</Target>
<PropertyGroup>
<PostBuildEventDependsOn>
$(PostBuildEventDependsOn);
PostBuildMacros;
</PostBuildEventDependsOn>
<PostBuildEvent>
if $(ConfigurationName) == Release copy /y $(TargetPath) $(SolutionDir)..\Builds$(TargetName)_v@(Major).@(Minor)$(TargetExt)
</PostBuildEvent>
</PropertyGroup>
如果可能的话,我希望能够在没有 <PropertyGroup>
部分的情况下完成此操作。
我正在使用 this stack overflow answer 到 return 构建程序集的 @(Version)
作为 ItemGroup 用于 post-build 事件。
但是,我想分别拥有 Major 和 Minor,因为我只想要 post 构建中的 vMajor.Minor
。为此,我想在 Include
属性中建立索引。
以下有效,但如果主编号或次编号超过 9(仅限个位数),则会中断:
<Target Name="PostBuildMacros">
<GetAssemblyIdentity AssemblyFiles="$(TargetPath)">
<Output TaskParameter="Assemblies" ItemName="Targets" />
</GetAssemblyIdentity>
<ItemGroup>
<Version Include="@(Targets->'%(Version)')" />
<Major Include="@(Version->Substring(0,1))" />
<Minor Include="@(Version->Substring(2,1))" />
</ItemGroup>
</Target>
<PropertyGroup>
<PostBuildEventDependsOn>
$(PostBuildEventDependsOn);
PostBuildMacros;
</PostBuildEventDependsOn>
<PostBuildEvent>
if $(ConfigurationName) == Release copy /y $(TargetPath) $(SolutionDir)..\Builds$(TargetName)_v@(Major).@(Minor)$(TargetExt)
</PostBuildEvent>
</PropertyGroup>
我正在寻找的是更多类似的东西:
<Major Include="@(Version->Split('.')[0])" />
<Minor Include="@(Version->Split('.')[1])" />
或者,仅当确实无法使用索引器时,
<Major Include="@(Targets->Version->Major)" />
<Minor Include="@(Targets->Version->Minor)" />
<Major Include="@(Targets->'%(Version)'->'%(Major)')" />
<Minor Include="@(Targets->'%(Version)'->'%(Minor)')" />
<Major Include="@(Targets->'%(Version.Major)')" />
<Minor Include="@(Targets->'%(Version.Minor)')" />
有什么选项,如果有的话,我必须索引一个数组或以其他方式将 1.0.2838.24877
之类的版本号分成 1
和 0
?
编辑
这现在可以工作了,首先使用 <PropertyGroup>
我可以像 this post 那样利用 Split('.')[n]
,但是 $(Major)
属性($()
)是在 Post-Build 事件中不可用(为什么不可用?我错过了什么吗?)。如果我可以直接在 post-build.
$(Major)
会更优雅
<Target Name="PostBuildMacros">
<GetAssemblyIdentity AssemblyFiles="$(TargetPath)">
<Output TaskParameter="Assemblies" ItemName="Targets" />
</GetAssemblyIdentity>
<PropertyGroup>
<Version>@(Targets->'%(Version)')</Version>
<Major>$(Version.Split('.')[0])</Major>
<Minor>$(Version.Split('.')[1])</Minor>
<Build>$(Version.Split('.')[2])</Build>
<Revision>$(Version.Split('.')[3])</Revision>
</PropertyGroup>
<ItemGroup>
<Version Include="$(Version)" />
<Major Include="$(Major)" />
<Minor Include="$(Minor)" />
<Build Include="$(Build)" />
<Revision Include="$(Revision)" />
</ItemGroup>
<Message Text="Version: @(Version)" />
<Message Text="Major.Minor.Build.Revision: @(Major).@(Minor).@(Build).@(Revision)" />
</Target>
<PropertyGroup>
<PostBuildEventDependsOn>
$(PostBuildEventDependsOn);
PostBuildMacros;
</PostBuildEventDependsOn>
<PostBuildEvent>
if $(ConfigurationName) == Release copy /y $(TargetPath) $(SolutionDir)..\Builds$(TargetName)_v@(Major).@(Minor)$(TargetExt)
</PostBuildEvent>
</PropertyGroup>
为什么 ItemGroups @()
可用而 PropertyGroups $()
不可用?
这是我得到的最好的结果,我对其他可用的内容或如何在 ItemGroups
中使用索引器知之甚少。
<Target Name="PostBuildMacros">
<GetAssemblyIdentity AssemblyFiles="$(TargetPath)">
<Output TaskParameter="Assemblies" ItemName="Targets" />
</GetAssemblyIdentity>
<PropertyGroup>
<Version>@(Targets->'%(Version)')</Version>
</PropertyGroup>
<ItemGroup>
<Version Include="$(Version)" />
<Major Include="$(Version.Split('.')[0])" />
<Minor Include="$(Version.Split('.')[1])" />
<Build Include="$(Version.Split('.')[2])" />
<Revision Include="$(Version.Split('.')[3])" />
</ItemGroup>
<Message Text="Version: @(Version)" />
<Message Text="Major.Minor.Build.Revision: @(Major).@(Minor).@(Build).@(Revision)" />
</Target>
<PropertyGroup>
<PostBuildEventDependsOn>
$(PostBuildEventDependsOn);
PostBuildMacros;
</PostBuildEventDependsOn>
<PostBuildEvent>
if $(ConfigurationName) == Release copy /y $(TargetPath) $(SolutionDir)..\Builds$(TargetName)_v@(Major).@(Minor)$(TargetExt)
</PostBuildEvent>
</PropertyGroup>
如果可能的话,我希望能够在没有 <PropertyGroup>
部分的情况下完成此操作。