Last active
September 28, 2015 17:58
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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><Assembly:</AssemblyAttributePrefix> | |
<AssemblyAttributeSuffix>></AssemblyAttributeSuffix> | |
</PropertyGroup> | |
<Error Condition="'$(AssemblyVersion)'!='' and '$([System.Text.RegularExpressions.Regex]::IsMatch("$(AssemblyVersion)","^\d+\.\d+\.\d+\.\d+$"))' == 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("%(Identity)","(?i)AssemblyVersion"))' == false" /> | |
<_AssemblyInfoLines_Pass2 Include="@(_AssemblyInfoLines_Pass1)" Condition="'$(AssemblyFileVersion)'=='' or '$([System.Text.RegularExpressions.Regex]::IsMatch("%(Identity)","(?i)AssemblyFileVersion"))' == false" /> | |
<_AssemblyInfoLines_Pass3 Include="@(_AssemblyInfoLines_Pass2)" Condition="'$(AssemblyInformationalVersion)'=='' or '$([System.Text.RegularExpressions.Regex]::IsMatch("%(Identity)","(?i)AssemblyInformationalVersion"))' == false" /> | |
<NewAssemblyInfoLines Include="@(_AssemblyInfoLines_Pass3)" /> | |
<NewAssemblyInfoLines Include="$(AssemblyAttributePrefix) AssemblyVersion("$(AssemblyVersion)")$(AssemblyAttributeSuffix)" Condition="'$(AssemblyVersion)'!=''" /> | |
<NewAssemblyInfoLines Include="$(AssemblyAttributePrefix) AssemblyFileVersion("$(AssemblyFileVersion)")$(AssemblyAttributeSuffix)" Condition="'$(AssemblyFileVersion)'!=''" /> | |
<NewAssemblyInfoLines Include="$(AssemblyAttributePrefix) AssemblyInformationalVersion("$(AssemblyInformationalVersion)")$(AssemblyAttributeSuffix)" Condition="'$(AssemblyInformationalVersion)'!=''" /> | |
</ItemGroup> | |
<WriteLinesToFile File="$(VersionedAssemblyInfoPath)" | |
Lines="@(NewAssemblyInfoLines)" | |
Overwrite="true" /> | |
<ItemGroup> | |
<Compile Remove="$(AssemblyInfoRelativePath)" /> | |
<Compile Include="$(VersionedAssemblyInfoPath)" /> | |
<FileWrites Include="$(VersionedAssemblyInfoPath)" /> | |
</ItemGroup> | |
</Target> | |
</Project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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).