Skip to content

Instantly share code, notes, and snippets.

@jstangroome
Last active September 28, 2015 17:58
Show Gist options
  • Save jstangroome/1475490 to your computer and use it in GitHub Desktop.
Save jstangroome/1475490 to your computer and use it in GitHub Desktop.
An MSBuild script that allows a C# application's assembly version information to be overridden by an MSBuild property
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- Example usage: msbuild someproject.csproj /p:AssemblyVersion=1.2.3.4 /p:AssemblyFileVersion=2.3.4.5 /p:AssemblyInformationVersion="Codename Frank" -->
<Target Name="OverrideAssemblyVersion"
BeforeTargets="CoreCompile">
<!-- CallTarget technique used to allow version properties to be set by another target -->
<CallTarget Targets="CoreOverrideAssemblyVersion"
Condition=" '$(AssemblyVersion)'!='' or '$(AssemblyFileVersion)'!='' or '$(AssemblyInformationalVersion)'!='' " />
</Target>
<Target Name="CoreOverrideAssemblyVersion">
<PropertyGroup>
<VersionedAssemblyInfoPath>$(IntermediateOutputPath)\AssemblyInfo.versioned$(DefaultLanguageSourceExtension)</VersionedAssemblyInfoPath>
<AssemblyInfoRelativePath>Properties\AssemblyInfo$(DefaultLanguageSourceExtension)</AssemblyInfoRelativePath>
<AssemblyAttributePrefix>[assembly: </AssemblyAttributePrefix>
<AssemblyAttributeSuffix>]</AssemblyAttributeSuffix>
</PropertyGroup>
<PropertyGroup Condition="'$(Language)'=='VB'">
<AssemblyInfoRelativePath>My Project\AssemblyInfo$(DefaultLanguageSourceExtension)</AssemblyInfoRelativePath>
<AssemblyAttributePrefix>&lt;Assembly:</AssemblyAttributePrefix>
<AssemblyAttributeSuffix>&gt;</AssemblyAttributeSuffix>
</PropertyGroup>
<Error Condition="'$(AssemblyVersion)'!='' and '$([System.Text.RegularExpressions.Regex]::IsMatch(&quot;$(AssemblyVersion)&quot;,&quot;^\d+\.\d+\.\d+\.\d+$&quot;))' == false"
Text="AssemblyVersion '$(AssemblyVersion)' must be of the form 'n.n.n.n' where 'n' is a positive integer."/>
<Message Text="Setting assembly version to '$(AssemblyVersion)'" Condition="'$(AssemblyVersion)'!=''"/>
<Message Text="Setting assembly file version to '$(AssemblyFileVersion)'" Condition="'$(AssemblyFileVersion)'!=''"/>
<Message Text="Setting assembly informational version to '$(AssemblyInformationalVersion)'" Condition="'$(AssemblyInformationalVersion)'!=''"/>
<ReadLinesFromFile File="$(AssemblyInfoRelativePath)">
<Output TaskParameter="Lines" ItemName="AssemblyInfoLines" />
</ReadLinesFromFile>
<ItemGroup>
<_AssemblyInfoLines_Pass1 Include="@(AssemblyInfoLines)" Condition="'$(AssemblyVersion)'=='' or '$([System.Text.RegularExpressions.Regex]::IsMatch(&quot;%(Identity)&quot;,&quot;(?i)AssemblyVersion&quot;))' == false" />
<_AssemblyInfoLines_Pass2 Include="@(_AssemblyInfoLines_Pass1)" Condition="'$(AssemblyFileVersion)'=='' or '$([System.Text.RegularExpressions.Regex]::IsMatch(&quot;%(Identity)&quot;,&quot;(?i)AssemblyFileVersion&quot;))' == false" />
<_AssemblyInfoLines_Pass3 Include="@(_AssemblyInfoLines_Pass2)" Condition="'$(AssemblyInformationalVersion)'=='' or '$([System.Text.RegularExpressions.Regex]::IsMatch(&quot;%(Identity)&quot;,&quot;(?i)AssemblyInformationalVersion&quot;))' == false" />
<NewAssemblyInfoLines Include="@(_AssemblyInfoLines_Pass3)" />
<NewAssemblyInfoLines Include="$(AssemblyAttributePrefix) AssemblyVersion(&quot;$(AssemblyVersion)&quot;)$(AssemblyAttributeSuffix)" Condition="'$(AssemblyVersion)'!=''" />
<NewAssemblyInfoLines Include="$(AssemblyAttributePrefix) AssemblyFileVersion(&quot;$(AssemblyFileVersion)&quot;)$(AssemblyAttributeSuffix)" Condition="'$(AssemblyFileVersion)'!=''" />
<NewAssemblyInfoLines Include="$(AssemblyAttributePrefix) AssemblyInformationalVersion(&quot;$(AssemblyInformationalVersion)&quot;)$(AssemblyAttributeSuffix)" Condition="'$(AssemblyInformationalVersion)'!=''" />
</ItemGroup>
<WriteLinesToFile File="$(VersionedAssemblyInfoPath)"
Lines="@(NewAssemblyInfoLines)"
Overwrite="true" />
<ItemGroup>
<Compile Remove="$(AssemblyInfoRelativePath)" />
<Compile Include="$(VersionedAssemblyInfoPath)" />
<FileWrites Include="$(VersionedAssemblyInfoPath)" />
</ItemGroup>
</Target>
</Project>
@fsimonazzi
Copy link

You might want to create this .versioned.cs file in the obj folder (IntermediateOutputPath) instead of the source tree to work better with source control tools, and also add the file to the FileWrites item so that the Clean target can get rid of it (http://msdn.microsoft.com/en-us/magazine/dd419659.aspx#id0090093).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment