Skip to content

Instantly share code, notes, and snippets.

@bezzad
Created July 18, 2016 13:06
Show Gist options
  • Save bezzad/7d44abf0465d790c3cc87341cef8980d to your computer and use it in GitHub Desktop.
Save bezzad/7d44abf0465d790c3cc87341cef8980d to your computer and use it in GitHub Desktop.
Auto .net project version updater by TextTemplete (.tt)
<#@ template language="C#" hostspecific="true" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Text.RegularExpressions" #>
<#@ output extension=".txt" #>
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.42000
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
//
// This file is a T4 Text Templete for change automatically this
// project files version.
//
// Affected files:
//
// 1. Assembly Version
// </auto-generated>
//------------------------------------------------------------------------------
<#
string path = Host.ResolvePath("..");
var AssemblyFilePath = Path.Combine(path, "Properties\\AssemblyInfo.cs");
//========================================== Assembly File ==================================================
string newVersion = null;
if(File.Exists(AssemblyFilePath))
{
var AssemblyFileData = File.ReadAllText(AssemblyFilePath);
var asmVerPattern = @"(\[assembly: [\w\.]*Version\("")(?<asmVer>[\d+\.\d+]*)(""\)\])";
var match = Regex.Match(AssemblyFileData, asmVerPattern);
var gAsmVer = match.Groups["asmVer"].Value;
var oldVersion = new Version(gAsmVer);
// Compile new version [major.minor.build] = 2.4.99
if (oldVersion.Build >= 99)
{
newVersion = oldVersion.Minor >= 99
? new Version(oldVersion.Major + 1, 0, 0).ToString()
: new Version(oldVersion.Major, oldVersion.Minor + 1, 0).ToString();
}
else
{
newVersion = new Version(oldVersion.Major, oldVersion.Minor, oldVersion.Build + 1).ToString();
}
//
// Set new version in target files
AssemblyFileData = Regex.Replace(AssemblyFileData, asmVerPattern, m => m.Groups[1].Value + newVersion + m.Groups[2].Value);
File.WriteAllText(AssemblyFilePath, AssemblyFileData);
}
//========================================== Assembly File ==================================================
//===========================================================================================================
#>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment