Skip to content

Instantly share code, notes, and snippets.

Last active October 17, 2016 17:31
What would you like to do?
build.targets file for injecting version from a text-file to the AssemblyInfo.cs file:
<?xml version="1.0" encoding="utf-8" ?>
<Project ToolsVersion="12.0" xmlns="">
<!-- 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.\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" />
<!-- 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. -->
<WriteFiles Include='$(ProjectDir)Properties/AssemblyInfo.cs' />
<!-- 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).
Regex="// TEMPLATE:.+"
ReplacementText="// THIS FILE IS GENERATED! Apply any changes to 'AssemblyInfo.cs.use_this', instead."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment