-1

I'm using this stack overflow answer to return the built assembly's @(Version) as an ItemGroup for use in the post-build event.

However, I want to have Major and Minor separately, as I only want vMajor.Minor in the post build. To do this, I'd like to index inside the Include attribute.

The following works, but will break if the Major or Minor numbers go over 9 (single digits only):

<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>

What I'm looking for is something more along the lines of:

<Major Include="@(Version->Split('.')[0])" />
<Minor Include="@(Version->Split('.')[1])" />

or, only if an indexer is really not possible,

<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)')" />

What options, if any, do I have to index an array or otherwise pick apart a version number like 1.0.2838.24877 into 1 and 0?

Edit

This now works, by first using a <PropertyGroup> I can utilitize Split('.')[n] like in this post, but the $(Major) Properties ($()) are not available in the Post-Build event (why not? Am I missing something?). This would be more elegant if I could just directly use $(Major) in the post-build.

<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>

Why are ItemGroups @() available and PropertyGroups $() not?

Community
  • 1
  • 1
Ehryk
  • 1,930
  • 2
  • 27
  • 47

1 Answers1

1

This is the best I've come to, with little understanding of what else would be available or how to use indexers within 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>

I would like to be able to accomplish this without the <PropertyGroup> section, if possible.

Ehryk
  • 1,930
  • 2
  • 27
  • 47