Click here to Skip to main content
15,884,099 members
Articles / Programming Languages / Javascript
Alternative
Tip/Trick

Incrementing AssemblyVersion revision number on each build

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
3 Oct 2011CPOL1 min read 6.1K  
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:
C#
[assembly: AssemblyDescription ("Some description about the application, build on 2000-00-00 00:00")]

The code to overwrite the description attribute is:
JavaScript
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.

License

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


Written By
Technical Lead
China China
I am now programming applications for the Internet of Things.

Comments and Discussions

 
-- There are no messages in this forum --