Michael Flanakin's Web Log

Comments and complaints on software and technology in general

  Home  |   Contact  |   Syndication    |   Login
  159 Posts | 18 Stories | 78 Comments | 530 Trackbacks

News

This weblog is no longer being maintained. For the latest, check out www.michaelflanakin.com!

Article Categories

Archives

Post Categories

Image Galleries

Miscellaneous

Download UpdateVersion

I've been looking at NAnt pretty hard lately. The biggest problem that I've had so far is creating and updating version numbers. NAntContrib has a <version> task that will read in and update a version number specified in a text file, but that doesn't quite give me what I'm looking for. NAnt 0.85 is supposed to come with a method for reading assembly the version, but again, this falls pretty short of what I'm looking for.

After looking around, I found a pretty good solution: UpdateVersion. Currently in version 1.2, this tool will read through your AssemblyInfo.cs (or .vb) file and update the version number based on specified criteria. The tool was pretty nice. I didn't quite like the code, but it got the job done. It can read through any input file (or stream) and write output to the same or another file or even the output stream. There was a “pinning” option (-p or --pin) to specify a particular version number, but that didn't work. I fixed that and extended it a little bit. I modified the code to allow any pinned version (i.e. 1.0, 1.0.0, or 1.0.0.0). Whatever you don't specify will be generated based on the other options you specify. I also added a write version (-w or --write) option to write the version number (and only the version number) to the output stream. This allows a user to catch the new version that updates the AssemblyInfo.cs file(s).

I didn't end up using the -w option that I setup because of the order of events that needed to happen in my build script. So, I just decided to update the version manually and use the pinning option to update the project assemblies. I probably could've used the <version> task included in the NAntContrib project, but there were two reasons I didn't: (1) I'm using one separate properties file and don't want to have to deal with another one for the version number; and, more importantly, (2) when I updated to a 0.85 nightly build (8/12/2004), NAnt wouldn't recognize my NAntContrib tasks.

First, I've specified a “startdate” and “build.version” property to represent the project start date and custom major.minor version number. Second, I run the following build script to update the “build.version” property.

 

<script language="C#">
  <imports>
    <import name="System.Globalization" />
    <import name="System.Threading" />
  </imports>
  <code><![CDATA[
    public static void ScriptMain(Project project) {
      Version version = new Version(project.Properties["build.version"]);
      int major = version.Major;
      int minor = version.Minor;
      int build = version.Build;
      int revision = version.Revision;
      if (build == -1) {
        DateTime start = Convert.ToDateTime(project.Properties["startdate"]);
        Calendar calendar = Thread.CurrentThread.CurrentCulture.Calendar;
        int months = ((calendar.GetYear(DateTime.Today) 
          - calendar.GetYear(start)) * 12) 
          + calendar.GetMonth(DateTime.Today) 
          - calendar.GetMonth(start);
        int day = DateTime.Now.Day;
        build = (months * 100) + day;
      }
      if (revision == -1) {
        TimeSpan difference = DateTime.Now.Subtract(DateTime.Today);
        revision = (int)(difference.TotalSeconds / 10);
      }
      version = new Version(major, minor, build, revision);
      project.Properties["build.version"] = version.ToString();
    }
  ]]></code>
</script>

 

I hope this helps anyone else looking for a version numbering system for NAnt. After I updated the version number, everything else was pretty easy - I looped thru each AssemblyInfo.cs file in the solution directory and updated the versions using my modified UpdateVersion.

If anyone is interested in the UpdateVersion code, let me know. I sent an email to Matt about getting it updated, but haven't heard from him. I'll give it a few days.

Update (4/4/2008): Changed download link to CodePlex project

posted on Monday, August 16, 2004 2:04 AM

Feedback

# re: Versioning with NAnt 8/24/2004 4:42 PM kenny goers
Hi,

I've been going through and "fixing" a NAnt task to read and update version task, based on the UpdateVersion code. I wouldn't mind getting the changes you made to the 1.2 version if you don't mind. I'll share the NAnt task when I'm done.

_Kenny.

# re: Versioning with NAnt 8/24/2004 5:41 PM Michael Flanakin
Give me your email address and I'll send you the code.

# re: Versioning with NAnt 8/25/2004 8:52 AM kenny goers
I tried posting a response to you, but that seemed to be screwed up yesterday and I didn't want to post it in a public forum, but here is obfuscated.

kennyg AT magenic DOT com

_Kenny.

# re: Versioning with NAnt 9/17/2004 5:20 PM Avtar Batth
Can you you please send me updated code for UpdateVersion
@ avtar.batth@rabsoft.com
or avtar.batth@gmail.com

Thanks,
Avtar

# re: Versioning with NAnt 12/1/2004 4:20 PM Martin MacPherson
Michael,
Could you pls send send me a copy of the updated UpdateVersion code. (martin@metapixel.co.uk)

Cheers,
Martin

# re: Versioning with NAnt 1/17/2005 7:11 AM Sunil
Hi Michael,

could you pls forward me too the latest source code of UpdateVersion to sunilunnithan@gmail.com

Thanks,

Sunil

# re: Versioning with NAnt 1/24/2005 10:25 AM Vamshi
Hi Michael,

Pls send me the latest code to update the version number in the AssemblyInfo.cs. I have multiple such files in my project where i need to increment the version number for every build.

my email id: brvamshi@yahoo.com

thanks in advance,
vamshi

# re: Versioning with NAnt 2/17/2005 2:38 PM Raj
Hi Michael:
Please send me the latest code to update the version number in the AssemblyInfo.cs.

My email id: pangaraj@gmail.com

Thanks,
Raj

# re: Versioning with NAnt 3/22/2005 3:29 PM Chris H
I have a lame question, I'm having trouble deciding which projects in my solution actually neede to be built, and therefore which AssemblyInfo.VB files actually need to be incremented.

Am i missing something? if i use NAnt's <Solution> task it ALWAYS buidls all projects in the solution, not a problem but how do i know which projects have actually changed?

any help is appreciated
choffman@sti-k12.com

# re: Versioning with NAnt 3/23/2005 7:32 AM Michael Flanakin
I'd suggest that you go ahead and build all of the projects. This is usually the practice you'll want to take when doing release builds, anyway. If you're using this Nant file all the time, then obviously an incremental build would be more efficient. I haven't played with Nant enough to really give you any advice in this arena. The only thing I can think of would be to check the dates of all the files and build the project if any of the files in its directory have been modified since the assembly modify date. This could take a while to analyze files, tho, depending on how big your project is. I'm sure there is a better way, but this is all I know of for now. Best of luck. If you do find a solution, let me know!

# re: Versioning with NAnt 4/14/2005 6:41 PM Sandy Turnbull
Hi Michael
Please could you send the latest code to update the version number in the AssemblyInfo.cs.

My email: sandy@soft.co.za

Thanks v much :)
Sandy



# re: Versioning with NAnt 4/27/2005 5:12 PM Chris H
Again with the lame question. We strong name all of our assemblies. So what we dont want is to update the version for each and every build only the projects that have actually changed. How do i determine this? and update the assemblyinfo.VB file (Were a vb shop). I get how to update the file, just not WHEN.
HELP!!!!

# re: Versioning with NAnt 4/27/2005 8:21 PM Michael Pittman
Could I get the fixed version of UpdateVersion too? Many thanks.

mpittman@3vr.com

# re: Versioning with NAnt 4/30/2005 4:48 AM Yogesh
plz specify how can i modify version of dll

such as if i want to have it 1.1.3.5

# re: Versioning with NAnt 5/3/2005 8:27 AM Michael Flanakin
Chris,
Again, you'll need to check the change dates of the different projects. If they've changed, then you'll do the build and/or update the version numbers. This is a very easy check. I believe it's: directory::last-changed-date(dirname). Just compare that to the date of the previously built assembly - file::last-changed-date(assemblyfile). Double check the Nant docs for the function names, but I believe these are correct.

Others,
I will get the UpdateVersion out to those who've requested it later this week. I'm sorry, but I'm very busy with work and school this week.

# re: Versioning with NAnt 5/4/2005 6:55 AM Mark Lindell
Michael could you be so kind as to send me your code updates to UpdateVersion? (viperx77@yahoo.com) I found that the -p option was not implemented as well. Perhaps you could post the code to somewhere permenant.

Thanks,

Mark Lindell

# re: Versioning with NAnt 5/5/2005 10:55 AM Steven Pack
Michael, I'm after the UpdateVersion with -p implmented.

Could you please send it steven_john_pack@*NO SPAM*yahoo.com.au?

Thanks

# re: Versioning with NAnt 5/17/2005 3:12 PM Ole Lytjohan
I'd like that updated version aswell, if offer still stands :)

lytjohan@gmail.com

# re: Versioning with NAnt 5/20/2005 11:22 AM Jorgen Noah
Please send me the "fixed" UpdateVersion (exe and source) with pinning and the NAnt task if possible.

Thank you very much.

jnoah@rbc.org

# re: Versioning with NAnt 6/9/2005 1:51 AM Vijaya Ragavan
Hi Michael
Please could you send the latest code to update the version number in the AssemblyInfo.cs.

my e-id : vjragavan@gmail.com

# re: Versioning with NAnt 6/10/2005 10:00 AM Steve
Michael,

a copy of the latest version would grately help our development efforts. Could you please send it to steve.luys[AT]gmail[dot]com ?

many thanks,

Steve.

# Alternative to updating version number in EACH AssemblyInfo 6/12/2005 12:32 AM Danny
I've got a method that removes the need to update each AssemblyInfo.cs file if you're interested.

Use a CommonAssemblyInfo.cs file that lives in the solution root and is linked to each project that you want to share the info with. You can remove the version attribute in each individual AssemblyInfo.cs file and just have it existing in the CommonAssemblyInfo.cs file.

This way, there is only ever 1 file to update, removing the need to iterate through each file as you say you are currently doing.

# re: Alternative to updating version number in EACH AssemblyInfo 6/21/2005 11:36 AM Dan
Danny,

I am also doing what you suggested with the following in my project files:

<File
RelPath = "VersionInfo.cs"
Link = "..\VersionInfo.cs"
SubType = "Code"
BuildAction = "Compile"
/>

But I ran into one glitch: do you know of a way to make this work with a web project? It would seem the relative path in the Link attribute doesn't work when the project is a virtual directory.

Dan


# re: Versioning with NAnt 6/26/2005 10:40 PM Sandy
Michael, I'm after the UpdateVersion with -p implemented. to sandyandkim@hotmail.com please...

Cheers

# re: Versioning with NAnt 7/27/2005 9:26 AM Aram
Could you also send me the modified UpdateVersion source and if possible the nant task? aram AT umail.ucsb.edu

-Thanks

# re: Versioning with NAnt 8/1/2005 6:01 PM Paul
I think what would be great is if you POSTED the UpdateVersion source code because everyone wants it :-)

# re: Versioning with NAnt 8/4/2005 4:24 AM Michael Flanakin
I didn't post it because there's too much. Sorry.

# re: Versioning with NAnt 8/4/2005 4:26 AM Thibaut Barrère
I wrote a small nant task which updates the AssemblyInfo files for both C# and managed c++.


# re: Versioning with NAnt 8/9/2005 4:13 AM Prasad
Can any one send me the latest code to update the version number in the AssemblyInfo.cs
to
nblprasad@rediffmail.com

# re: Versioning with NAnt 8/30/2005 9:47 AM Daniel
could u send the code to "danielhalan AT hotmail DOT COM"

# re: Versioning with NAnt 9/20/2005 8:57 AM Scott Heffron
Please could you send the latest code to update the version number in the AssemblyInfo.cs. Sorry for the late posting.

sheffron@browz.com

# re: Versioning with NAnt 9/22/2005 1:36 AM Dan
Could you please send a copy of your UpdateVersion source (and exe if possible) to dandev478 AT yahoo.com

Much appreciated!

# re: Versioning with NAnt 9/29/2005 9:07 AM Omar
Great article, what I was looking for though was how to version using the Assemblyinfo.cs file before the Assembly is built, and I found a way:

<script language="C#">
<code>
<namespaceimports>
<import name="System.IO" />
<import name="System.Text" />
<import name="System.Diagnostics" />
<import name="System.Text.RegularExpressions" />

</namespaceimports><![CDATA[
public static void ScriptMain(Project project) {
string result = null;
if(!File.Exists(".\\business\\src\\MfpServices\\AssemblyInfo.cs"))
throw new ArgumentException("File does not exist.", "filename");

using(FileStream stream = File.OpenRead( ".\\business\\src\\MfpServices\\AssemblyInfo.cs"))
{
StreamReader reader = new StreamReader(stream, Encoding.Default, true);
result = reader.ReadToEnd();
}

Regex AssemblyVersionRegex = new Regex("AssemblyVersion(?:Attribute)?\\(\\s*?\"(?<version>(?<major>[0-9]+)\\.(?<minor>[0-9]+)\\.(?<build>[0-9]+)\\.(?<revision>[0-9]+))\"\\s*?\\)");

Match match = AssemblyVersionRegex.Match(result);
string inputVersion = match.Groups["version"].Value;

Version version = new Version(inputVersion);
int build = version.Build +1;
int Major = version.Major;
int Minor = version.Minor;
int rev = version.Revision;

Version bVersion = new Version(Major,Minor,build,rev);

project.Properties["Version"] = bVersion.ToString();
}
]]></code>
</script>
</target>

Hope this helps anyone.

Omar





# re: Versioning with NAnt 9/30/2005 7:35 AM Michael Flanakin
You can use UpdateVersion to update the AssemblyInfo file. You just have to do that before your build and you should be good to go. That's exactly what UpdateVersion is for.

# re: Versioning with NAnt 11/9/2005 8:45 AM ricva
please send the updateversion code to

Richard.VanderArk@nfsmt.com


Thanks

Ric

# re: Versioning with NAnt 12/7/2005 6:48 AM roy
Hi,

Please send the code to

royassaly@gmail.com

Thanks you!

Roy

# re: Versioning with NAnt 12/11/2005 8:36 AM Nirp
Can you please send me the fixed version to :
nirpi@spymac.com ?
Thanks,
Nir.

# re: Versioning with NAnt 12/13/2005 10:13 AM Nir Pinhasov
Since no reply was made here...
I fixed the pin option myself and added support for specifying additional characters on the pin address to support fixed field and incremental to the existing one (useful for those who needs it to be pinned only for some of the fields).
I'll be happy to send it to anyone who needs it.
Nir.

Nir.pinhasov@oberon-media.com

# re: Versioning with NAnt 12/20/2005 6:29 AM canthe
hi, can you pls send me the latest updateversion with pin switch to:
canthe@gmail.com ?


# re: Versioning with NAnt 12/24/2005 9:22 PM Manohar
Please send me the copy of UpdateVersion to u_n_manohar@hotmail.com.

Appreciate your help

Manohar

# re: Versioning with NAnt 1/19/2006 5:45 PM Alex
I need to use -p command option to specifies my own Version. So please send me the code. Thanks.
longnguyenhoang@gmail.com

# re: Versioning with NAnt 2/20/2006 6:57 AM fndtn357
could you please send me the updated version? thanks.
fndtn357@gmail.com

# re: Versioning with NAnt 3/12/2006 7:08 AM Rafat
i need it too please.......... any send me the code to

rafatbl@gmail.com

thanks

# re: Versioning with NAnt 5/30/2006 4:55 PM snordr502
could you please send me the copy of UpdateVersion. thanks,

snordr502@gmail.com

# re: Versioning with NAnt 10/4/2006 11:46 AM thebavarian
If I can have a copy of it as well would be greatly appreciate it.
Thanks in advance !
them3boy@hotmail.com

# re: Versioning with NAnt 10/17/2006 3:27 PM Jerin
CAn u pls tell me why the -p doesn't work? I have tried it many times.

jerinkl@yahoo.com

# re: Versioning with NAnt 12/8/2006 11:23 AM JB
I have downloaded the latest version of UpdateVersion and the pin functionality does not seem be working.

Does anyone have the exe where this functionality does work.If so could you please email it to

jbhiwandiwala@gmail.com.


# re: Versioning with NAnt 12/13/2006 3:50 PM sepehr sadeghi
here's some code i wrote (quick and dirty) to update the version from a text file.

(I posted this , but didn't seem to stick)

<tstamp/>
<target name="version" description="updates the version from the Build.ver file">
<script language="C#">
<code>
<namespaceimports>
<import name="System.IO" />
<import name="System.Text" />
<import name="System.Diagnostics" />
<import name="System.Text.RegularExpressions" />

</namespaceimports><![CDATA[

public static void ScriptMain(Project project) {
string result = null;
string versionResult = null;
string assemblyInfoFile = ".\\Properties\\AssemblyInfo.cs";
string versionFile = "..\\..\\Build.ver";

if(!File.Exists( assemblyInfoFile ))
throw new ArgumentException("File does not exist.", "filename");

using(FileStream stream = File.OpenRead( assemblyInfoFile ))
{
StreamReader reader = new StreamReader(stream, Encoding.Default, true);
result = reader.ReadToEnd();
}

if(!File.Exists( versionFile ))
throw new ArgumentException("File does not exist.", "filename");

using(FileStream versionS = File.OpenRead( versionFile ))
{
StreamReader reader = new StreamReader( versionS, Encoding.Default, true);
versionResult = reader.ReadToEnd();
}

string searchStr = "AssemblyFileVersion";
int indexEnd = result.IndexOf( searchStr );
string subString = result.Substring(0,indexEnd+searchStr.Length);
string updatedVersion = subString + "(\"" + versionResult + "\")]";

FileStream file_stream;
StreamWriter stream_writer;
file_stream = new FileStream(assemblyInfoFile, FileMode.Create, FileAccess.Write);
stream_writer = new StreamWriter(file_stream);
stream_writer.Write(updatedVersion);
stream_writer.Flush();
stream_writer.Close();


//Version bVersion = new Version(Major,Minor,build,rev);
//project.Properties["Version"] = bVersion.ToString();
}
]]>
</code>
</script>
</target>


# Versioning with NAnt - the simple way 5/4/2007 8:19 PM Laboremus
Hi! There is a new task in NAntContrib which will help you sync your builds. There is a blog post on how to store the file under the CC.NET folder on the server and it will be shared between builds
http://lazyloading.blogspot.com/2007/05/updating-assembly-version-with.html

# re: Versioning with NAnt 6/14/2007 2:59 PM Steve Ogilvie
Please send me the latest UpdateVersion (exe and source).

Much appreciated!

steve

sogilvie[AT]msn[dot]com

# re: Versioning with NAnt 4/2/2008 10:01 AM Julien
Hello,

I'm very interrested in the new UpdateVersion that enables to precise how many number we want that constitute the version number (actually I'm looking only for 3 to get something like this: 2.2.3).
Could it be possible that you send it to me?

Thank you in advance !

Julien

Post Feedback

Title:
Name:
Email: (never displayed)
Url:
Comments: 
Please add 5 and 3 and type the answer here: