Click here to Skip to main content
15,886,067 members
Articles / Programming Languages / C# 4.0

Autoincrement Version in Visual Studio

Rate me:
Please Sign up or sign in to vote.
4.60/5 (13 votes)
12 May 2010CPOL 68.7K   814   56  
Autoincrement version in Visual Studio
using System;
using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;

namespace vbinc
{
    class Program
    {
        private static int _incMajor = 0;
        private static int _incMinor = 0;
        private static int _incRevno = 0;
        private static int _incBuild = 0;

        private static bool _bshow;
        private static bool _bassemply;
        private static bool _bfile;

        private const string Pattern1 = 
                    @"\[assembly\: AssemblyVersion\(""(\d{1,})\.(\d{1,})\.(\d{1,})\.(\d{1,})""\)\]";
        private const string Pattern2 =
                    @"\[assembly\: AssemblyFileVersion\(""(\d{1,})\.(\d{1,})\.(\d{1,})\.(\d{1,})""\)\]";

        private const string VersionFile = @"..\..\Properties\AssemblyInfo.cs";
        private const string BackupFile = @"..\..\Properties\AssemblyInfo.~cs";

        static void Main(string[] args)
        {
            try
            {
                if(!ParseCmdLine(args))
                {
                    ShowUsage();
                    return;
                };

                string str = File.ReadAllText(VersionFile);
                string result;
                

                if (_bassemply || _bshow) result = GetResult(Pattern1, str, "AssemblyVersion");
                else result = str;
                if (_bfile || _bshow) result = GetResult(Pattern2, result, "AssemblyFileVersion");

                if (!_bshow)
                {
                    File.Delete(BackupFile);
                    File.Copy(VersionFile, BackupFile);
                    File.WriteAllText(VersionFile, result);
                }
                forDebug();
            }
            catch (Exception ex)
            {
                Console.WriteLine("ERR: " + ex.Message);
            }
        }

        [Conditional("DEBUG")]
        private static void forDebug()
        {
            Console.ReadKey();
        }

        public static bool ParseCmdLine(string[] args)
        {
            if (args.Length == 0) return false;
            string astr;

            foreach (string arg in args)
            {
                if(arg.StartsWith("-"))
                {
                    astr = arg.ToLower();
                    if (astr.Contains("a")) _bassemply = true;
                    if (astr.Contains("f")) _bfile = true;
                    if (astr.Contains("1")) _incMajor = 1;
                    if (astr.Contains("2")) _incMinor = 1;
                    if (astr.Contains("3")) _incBuild = 1;
                    if (astr.Contains("4")) _incRevno = 1;
                    if (astr.Contains("c")) _bshow = true;
                }

                if (arg.ToLower().StartsWith("-h")) return false;

                if (_bshow)
                {
                    _incMajor = 0;
                    _incMinor = 0;
                    _incBuild = 0;
                    _incRevno = 0;
                }
            }
            return true;
        }

        public static void ShowUsage()
        {
            Console.WriteLine("vbinc v{0}", Process.GetCurrentProcess().MainModule.FileVersionInfo.FileVersion);

            Console.WriteLine("Autoincrement version in c# projects. by vdasus.com");
            Console.WriteLine("");
            Console.WriteLine("usage: vbinc -[af1234]|[-c]|[-h]");
            Console.WriteLine("Options");
            Console.WriteLine("\t-a Increment assembly version");
            Console.WriteLine("\t-f Increment file version");
            Console.WriteLine("\t-1 Increment +1.x.x.x");
            Console.WriteLine("\t-2 Increment x.+1.x.x");
            Console.WriteLine("\t-3 Increment x.x.+1.x");
            Console.WriteLine("\t-4 Increment x.x.x.+1");
            Console.WriteLine("\t-c Just show current version");
            Console.WriteLine("\t-h this help screen.");
            Console.WriteLine("");
            Console.WriteLine("Example: vbinc -af34");
            Console.WriteLine("Increments assembly and file build and revision numbers.");

            Console.WriteLine("");

            Environment.Exit(0);
        }

        private static string GetResult(string pattern, string str, string app)
        {

            Regex r = new Regex(pattern);
            Match m = r.Match(str);

            string rz = string.Format("[assembly: {4}(\"{0}.{1}.{2}.{3}\")]"
                , GetInc(m.Groups[1].Value, _incMajor)
                , GetInc(m.Groups[2].Value, _incMinor)
                , GetInc(m.Groups[3].Value, _incBuild)
                , GetInc(m.Groups[4].Value, _incRevno)
                , app);

            Console.WriteLine(rz);

            return r.Replace(str, rz);
        }

        private static int GetInc(string aVer, int adoInc)
        {
            return Convert.ToInt32(aVer) + adoInc;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Lithuania Lithuania
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions