Click here to Skip to main content
15,880,796 members
Articles / Programming Languages / C#

Autoincrement Build Version, Using VCS (bazaar plugin included)

Rate me:
Please Sign up or sign in to vote.
4.20/5 (3 votes)
27 May 2010CPOL3 min read 18.5K   160   7   3
Autoincrement build version with possibility of using VCS

Introduction

I decided to rewrite an autoincrement program. Second version. What's the differences? It does not depend on the environment (you can use with f.e. Visual Basic project, and indeed for all, which version is stored in a file. So a few words on the resulting utility, and then about the beauty of .NET 4. I'm a novice in .NET, and I'll be grateful for any advice.

Background

Utility is a program and plugin. You can easily write the plugins by yourself, following the example. I use the bazaar, which I highly recommend. For it is already written plugin.

You can use this utility to automatically increment version of assembly or file with each rebuild.

Using the Code

The standard version in .NET is major.minor.build.revision.

Each plugin of utility has its own one-letter identifier (not digital). This is done to simplify using.

  • I can write vbinc2.exe-v0001 - increase only revision.
  • I can write vbinc2.exe-v0011 - will increase and build and revision
  • I can write vbinc2.exe-v001-1 - will increase by 1 build, and revision will decrease by one.

Now about plugins. For instance, if I have near to EXE plugin for bazaar, with id equals to "b" - I can write: vbinc2.exe-v00b1. In this case, build will be taken from the plugin with id "b" (number of current revision), i.e. if the current revision is 5 - build will always be 5 (so far we do not commit to the next revision) and the revision will increase by 1.

You can write a couple of plugins as you like and use them as: vbinc2.exe-v00bc.

Then the build is taken from one plugin, and revision from another with id="c"...

So what do we have with the code ...First is organization of plugin subsystem. .NET 4 has inside built-in subsystem for working with plugins.

All that is necessary to describe the interface is to insert one attribute and a couple of options:

First - declaring interface:

C#
public interface IVbinc2Sdk
{
    string Id { get; }
    string Name { get; }
    string Description { get; }
    int GetData();
} 

Then implementing it (don't forget Export Attribute):

C#
[Export(typeof(IVbinc2Sdk))]
public class Vbinc2Bzr : IVbinc2Sdk
{
    private readonly string _id = "b";
    private readonly string _name = "Bazaar";
    private readonly string _desc = "Bazaar vcs plugin";
 
    private static readonly StringBuilder GetOutput = new StringBuilder("");
 
    public int GetData()
    {
        try

And main implementation:

C#
//Add two lines and reference to System.ComponentModel.Composition
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
//...
//Add Import attribute
    class Program
    {
        [ImportMany(typeof(IVbinc2Sdk))]
        protected IVbinc2Sdk[] Plugins { get; set; }
//...
//Main method
        static void Main(string[] args)
        {
                Program p = new Program();
                p.Run(args);
        }
        private void Run(string[] args)
        {
            Compose(); // and do what you want else         
        }
//Composing plugins from current directory 
        private void Compose()
        {
            var catPlugs = new AggregateCatalog();
            catPlugs.Catalogs.Add(new DirectoryCatalog(
                Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)));
            var contPlugs = new CompositionContainer(catPlugs);
            contPlugs.ComposeParts(this);
        }
 
//And plugins using:
        private void ShowPlugins()
        {
            foreach (IVbinc2Sdk plugin in Plugins)
            {
                Console.WriteLine("--- Plugin: {0} ---", plugin.Name);
                Console.WriteLine("{0}", plugin.Description);
                Console.WriteLine("id: {0}", plugin.Id);
                Console.WriteLine("");
            }
        }
 
//That's all! It's enough to do lines above and you have
//normal plugin subsystem. You can make another plugin, put it to 
//directory near exe and plugin automatically available in your
//projects

You can see the project with the full source code and bins in attached zip.

You can specify the key -n - then taken from the plugin number will be incremented ...

To view the help -h

To see examples -e

How to use: F.e. you can create a folder in the root of solution /_v/ and put in 3 files:

  • vbinc2bzr.dll
  • vbinc2sdk.dll
  • vbinc2.exe

Each projects pre-build actions:

if $(ConfigurationName) == Release $(SolutionDir)\_v\vbinc2.exe -v00b1 -n 

if $(ConfigurationName) == Release $(SolutionDir)\_v\vbinc2.exe -v00b1 -n 
	-r"\[assembly\: AssemblyFileVersion\(""(\d+)\.(\d+)\.(\d+)\.(\d+)""\)\]" 

That's all. Now after each rebuild of release version build will be taken from bazaar current revision, and .NET revision will be incremented.

Disadvantages

  • In regexp (if you write them yourself) - must be (\d+)\.(\d+)\.(\d+)\.(\d+). They are replaced by the version. Ugly ...
  • You have to write regexp for NOT C# projects. It's easy, but not everyone can outright. I strongly recommend to learn regexps ...
  • Written in a couple of hours, so no comments or properly thought-out.
  • In some configurations, bazaar plugin does not work on network drives (I think, plugin does not correctly take revision) - must try

Advantage

  • It's working...

Thank you for your time and sorry for my English.

History

  • 27th May, 2010: Initial post

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

 
GeneralThank you Pin
pv@dbpro.cz25-Feb-11 23:21
pv@dbpro.cz25-Feb-11 23:21 
Generalok article Pin
Donsw3-Jun-10 10:18
Donsw3-Jun-10 10:18 
well done. thanks for sharing.
cheers,
Donsw
My Recent Article : CDC - Change Data Capture

GeneralRe: ok article Pin
vdasus3-Jun-10 10:23
vdasus3-Jun-10 10:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.