Skip to content

Instantly share code, notes, and snippets.

@lionicsheriff
Last active August 29, 2015 14:03
Show Gist options
  • Save lionicsheriff/5ac2d3cce5e2b9efb437 to your computer and use it in GitHub Desktop.
Save lionicsheriff/5ac2d3cce5e2b9efb437 to your computer and use it in GitHub Desktop.
The start of a t4 template to automatically update the version (based off the TFS revision). It was supposed to be able to work out of the box without any dependencies past VS Professional, but it is currently unable to update on build.
<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Diagnostics" #>
<# #if false
* WTF
This is arguably the world's best automatic version generator for visual studio and not a terrible idea at all.
* How to use
Edit AssemblyInfo.cs and comment out the AssemblyVersion, and AssemblyFileVersion lines
Add this file to your project
Do some magic to your build process and add a text transform step (probably will need to pass the correct cwd in as I suspect Host.ResolvePath won't work)
#endif #>
<#
// "Properties"
int RevisionNumber;
// Find the project directory
var cwd = Host.ResolvePath(""); // where this template is located
Directory.SetCurrentDirectory(cwd);
// Find the TFS cli tool (it is in the VS install folder)
// assume that the newest visual studio install is the latest
var vsLocation = System.IO.Directory.GetDirectories(@"C:\Program Files (x86)", "Microsoft Visual Studio *")
.OrderByDescending(s => (new DirectoryInfo(s).CreationTimeUtc))
.First();
// and grab the tfs cli tool from it
var tfLocation = Directory.GetFiles(vsLocation, "tf.exe", SearchOption.AllDirectories).First();
var tfArgs = @"history /version:T /stopafter:1 /format:brief /recursive /noprompt ."; // latest change made to any file in the project dir
var tfProcess = new Process();
tfProcess.StartInfo.RedirectStandardOutput = true;
tfProcess.StartInfo.FileName = tfLocation;
tfProcess.StartInfo.Arguments = tfArgs;
tfProcess.StartInfo.UseShellExecute = false;
tfProcess.Start();
var tfResult = tfProcess.StandardOutput.ReadToEnd();
tfProcess.WaitForExit();
// extract the revision number from the third line (first field)
// sample tfs output:
// Changeset User Date Comment
// --------- ----------------- ---------- ----------------------------------------
// 5943 Matthew Goodall 2014-07-02 blah blah blah
RevisionNumber = int.Parse(tfResult.Split(new string[]{ System.Environment.NewLine }, StringSplitOptions.None)[2].Split(new char[]{' '})[0]);
#>
using System.Reflection;
// CWD: <#= cwd #>
// Command: <#= tfLocation #> <#= tfArgs #>
/*
<#= tfResult #>
*/
[assembly: AssemblyVersion("1.0.1.<#= RevisionNumber #>")]
[assembly: AssemblyFileVersion("1.0.1.<#= RevisionNumber #>")]
<#@ output extension=".cs" #>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment