Skip to content

Instantly share code, notes, and snippets.

@dsoprea
Last active October 17, 2016 17:31
Show Gist options
  • Save dsoprea/8b33cce4cb8b8b8d710e271861fcbc48 to your computer and use it in GitHub Desktop.
Save dsoprea/8b33cce4cb8b8b8d710e271861fcbc48 to your computer and use it in GitHub Desktop.
build.targets file for injecting version from a text-file to the AssemblyInfo.cs file: http://wp.me/p3Uuaw-Hj
<?xml version="1.0" encoding="utf-8" ?>
<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- Inject a version from a text-file into AssemblyVersion.cs . We do this
so that it's easier for the application to know its own version [by
reading the text file].
-->
<Import Project="$(ProjectDir)..\packages\MSBuildTasks.1.5.0.196\tools\MSBuild.Community.Tasks.Targets" />
<Target Name="InjectVersion" BeforeTargets="BeforeBuild">
<!-- Read the version from our text file. This appears to automatically
trim (probably per line). This is located in the project root so
that we copy the file to the output-path rather than establishing
a whole Properties/ directory in the output path.
-->
<ReadLinesFromFile File="$(ProjectDir)Properties\executable.version">
<Output TaskParameter="Lines" PropertyName="ExecutableVersion" />
</ReadLinesFromFile>
<!-- Print it to the build output whether we're in debug-mode or not. -->
<Message Importance="High" Text="Executable version is [$(ExecutableVersion)]"/>
<!-- Copy our template file to the output file. -->
<Copy SourceFiles="$(ProjectDir)Properties/AssemblyInfo.cs.use_this" DestinationFiles="$(ProjectDir)Properties/AssemblyInfo.cs"/>
<!-- Do an RX replace of the version on to the token. -->
<ItemGroup>
<WriteFiles Include='$(ProjectDir)Properties/AssemblyInfo.cs' />
</ItemGroup>
<FileUpdate
Files="@(WriteFiles)"
Regex="__EXECUTABLE_VERSION__"
ReplacementText="$(ExecutableVersion)"
/>
<!-- Replace the cautionary note about how to use the file with one
saying that any changes will be lost (if made to the output file).
-->
<FileUpdate
Files="@(WriteFiles)"
Regex="// TEMPLATE:.+"
ReplacementText="// THIS FILE IS GENERATED! Apply any changes to 'AssemblyInfo.cs.use_this', instead."
/>
</Target>
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment