65.9K
CodeProject is changing. Read more.
Home

Incrementing AssemblyVersion revision number on each build

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0 vote)

Sep 8, 2011

CPOL

1 min read

viewsIcon

6242

Some people remarked that the Visual-Studio-generated build number actually contains a hidden timestamp, which, I think, is far from being intuitive. I wrote the following snippet to do something similar but is easier to understand.Beside changing the version number, we can even write build...

Some people remarked that the Visual-Studio-generated build number actually contains a hidden timestamp, which, I think, is far from being intuitive. I wrote the following snippet to do something similar but is easier to understand. Beside changing the version number, we can even write build date/time into the AssemblyDescription attribute. Thus it is very easy to figure out the build date when we review the properties of the assembly file. The following code requires an AssemblyDescription attribute with the date format (and an optional time format, which will be overwritten) preexisting in the AssemblyInfo.cs file. The required code snippet is:
[assembly: AssemblyDescription ("Some description about the application, build on 2000-00-00 00:00")]
The code to overwrite the description attribute is:
function pz (i) {
	return (i < 10) ? "0"+i : ""+i;
}
// change the timestamp in the AssemblyDescription attribute
r = /\[assembly:\s*(System\.Reflection\.)?AssemblyDescription(?:Attribute)?\s*\(\s*"([^\d]*)(\d{4}-\d{1,2}-\d{1,2})( \d{1,2}:\d{1,2}(?::\d{1,2})?)?([^\d\"]*)"\s*\)\s*\]/g;
a = r.exec(t);
if (a != null && a.length > 4) {
	var d = new Date();
	v = "[assembly: "+a[1]+"AssemblyDescription (\""+a[2]+d.getFullYear()+"-"+(d.getMonth()+1)+"-"+d.getDate()+" "+pz(d.getHours())+":"+pz(d.getMinutes())+a[5]+"\")]";
	t = t.replace(r, v);
	WScript.StdOut.WriteLine ("Assembly description changed: " + a[0] + "->" + v);
}
Note: The (\d{4}-\d{1,2}-\d{1,2}) part of the regular expression matches the "yyyy-MM-dd" part in the attribute content. The ( \d{1,2}:\d{1,2}(?::\d{1,2})?)? part matches the " HH:mm:ss" part (":ss" is optional). You may want to rewrite them to match your needs. Of course, you have to modify the "getFullYear()...." blah blah part to match the pattern of the regular expression, otherwise the rewritten attribute won't get matched the next time. The above code should be placed before the
s.Position = 0;
line in the tip in order to work. If you are to use this snippet, you might want to use it in the pre-build event rather than the post-build event.
Incrementing AssemblyVersion revision number on each build - CodeProject