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:
public interface IVbinc2Sdk
{
string Id { get; }
string Name { get; }
string Description { get; }
int GetData();
}
Then implementing it (don't forget Export Attribute):
[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:
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
class Program
{
[ImportMany(typeof(IVbinc2Sdk))]
protected IVbinc2Sdk[] Plugins { get; set; }
static void Main(string[] args)
{
Program p = new Program();
p.Run(args);
}
private void Run(string[] args)
{
Compose();
}
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);
}
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("");
}
}
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
Thank you for your time and sorry for my English.
History
- 27th May, 2010: Initial post
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.