Skip to content

Instantly share code, notes, and snippets.

@drewchapin
Last active February 8, 2018 18:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drewchapin/8887b92f0aaf8b51f421993dae030ed0 to your computer and use it in GitHub Desktop.
Save drewchapin/8887b92f0aaf8b51f421993dae030ed0 to your computer and use it in GitHub Desktop.
// Add this to pre-build event
// "%CommonProgramFiles(x86)%\microsoft shared\TextTemplating\$(VisualStudioVersion)\TextTransform.exe" -a !!BuildConfiguration!$(Configuration) "$(ProjectDir)Properties\AssemblyInfo.tt"
//
<#@ template debug="true" hostspecific="true" language="C#" #>
<#@ output extension=".cs" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Text.RegularExpressions" #>
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Resources;
// General Information
[assembly: AssemblyTitle("Project Title")]
[assembly: AssemblyDescription("Description")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Company")]
[assembly: AssemblyProduct("Product")]
[assembly: AssemblyCopyright("Copyright © 2018 Company Name")]
[assembly: AssemblyTrademark("Trademark")]
[assembly: AssemblyCulture("")]
// Version information
<#
string output = File.ReadAllText(this.Host.ResolvePath("AssemblyInfo.cs"));
Regex pattern = new Regex("^\\[assembly: AssemblyVersion\\(\"(?<major>\\d+)\\.(?<minor>\\d+)\\.(?<build>\\d+)(\\.[0-9\\*]+)?\"\\)\\]",RegexOptions.Multiline);
MatchCollection matches = pattern.Matches(output);
if( matches.Count == 1 )
{
major = Convert.ToInt32(matches[0].Groups["major"].Value);
minor = Convert.ToInt32(matches[0].Groups["minor"].Value);
build = Convert.ToInt32(matches[0].Groups["build"].Value);
if( this.Host.ResolveParameterValue("-","-","BuildConfiguration") == "Release" )
{
minor++;
build = 0;
}
else
build++;
}
else
WriteLine("// AssemblyInfo.tt could not find existing AssemblyVersion(). Using default values.");
#>
[assembly: AssemblyVersion("<#= this.major #>.<#= this.minor #>.<#= this.build #>")]
[assembly: AssemblyFileVersion("<#= this.major #>.<#= this.minor #>.<#= this.build #>")]
[assembly: NeutralResourcesLanguageAttribute( "en-US" )]
<#+
public int major { get; set; }
public int minor { get; set; }
public int build { get; set; }
#>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment