<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>FatLemon &#187; SubVersion</title>
	<atom:link href="http://www.fatlemon.co.uk/tag/subversion/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.fatlemon.co.uk</link>
	<description>Contemplations on software development and human factors, by Andrew Freemantle</description>
	<lastBuildDate>Sun, 29 Jan 2012 13:45:36 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>WTV: Automatic date-based version numbering for .Net with WhenTheVersion</title>
		<link>http://www.fatlemon.co.uk/2011/11/wtv-automatic-date-based-version-numbering-for-net-with-whentheversion/</link>
		<comments>http://www.fatlemon.co.uk/2011/11/wtv-automatic-date-based-version-numbering-for-net-with-whentheversion/#comments</comments>
		<pubDate>Sat, 05 Nov 2011 22:00:12 +0000</pubDate>
		<dc:creator>Andrew Freemantle</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[SubVersion]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[WTV]]></category>

		<guid isPermaLink="false">http://www.fatlemon.co.uk/?p=101</guid>
		<description><![CDATA[There&#8217;s a trend towards using the software build date as part of the software version number, you know the sort of thing I mean: &#8216;2011.11.4.xyz&#8216;, like in the footer over at StackOverflow.com, or MyLetts.com.. Wouldn&#8217;t it be great if we could get Visual Studio to do this for us automatically on build?  I thought so [...]]]></description>
			<content:encoded><![CDATA[<p>There&#8217;s a trend towards using the software build date as part of the software version number, you know the sort of thing I mean: &#8216;<span class="inline-code">2011.11.4.xyz</span>&#8216;, like in the footer over at <a title="StackOverflow.com - Q&amp;A for Programmers" href="http://www.stackoverflow.com">StackOverflow.com</a>, or <a title="MyLetts.com - Lettings management for private landlords" href="http://www.myletts.com">MyLetts.com</a>..</p>
<p><a href="http://www.fatlemon.co.uk/wp-content/uploads/wtv-date-version-example-myletts-com1.png"><img class="alignnone size-full wp-image-214" title="WTV Date-version example: www.MyLetts.com" src="http://www.fatlemon.co.uk/wp-content/uploads/wtv-date-version-example-myletts-com1.png" alt="" width="337" height="113" /></a></p>
<p>Wouldn&#8217;t it be great if we could get Visual Studio to do this for us automatically on build?  I thought so too, so I&#8217;ve written a little app to do it <img src='http://www.fatlemon.co.uk/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h4>Getting set up with &#8220;When The Version&#8221;</h4>
<ol>
<li>Go get a copy of &#8220;When The Version&#8221; (<span class="inline-code">WTV.exe</span>) from <a title="WhenTheVersion - Download page at GitHub" href="https://github.com/AndrewFreemantle/When-The-Version/downloads">the GitHub download page</a> (or you can <a title="When The Version - Source on GitHub" href="https://github.com/AndrewFreemantle/When-The-Version">build it from the source</a> if you prefer)</li>
<li>Drop it somewhere on your PC</li>
<li>Now, in your Visual Studio project, expand the Properties and duplicate the <span class="inline-code">AssemblyInfo.cs</span> / <span class="inline-code">.vb</span> file &#8211; I rename the duplicate to be <span class="inline-code">AssemblyInfo.Template.cs</span>:</li>
</ol>
<p><a href="http://www.fatlemon.co.uk/wp-content/uploads/wtv-date-version-solution-explorer.png"><img class="size-full wp-image-212 alignnone" title="AssemblyInfo.Template.cs - Solution Explorer" src="http://www.fatlemon.co.uk/wp-content/uploads/wtv-date-version-solution-explorer.png" alt="" width="288" height="148" /></a></p>
<ol start="4">
<li>Edit the <span class="inline-code">AssemblyInfo.Template.cs</span> file, putting in the placeholders for the date parts like so:</li>
</ol>
<pre>

// Version information for an assembly consists of the following four values:
//
//      Major Version
//      Minor Version
//      Build Number
//      Revision
//
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("{YYYY}.{MM}.{DD}.0")]
[assembly: AssemblyFileVersion("{YYYY}.{MM}.{DD}.0")]
</pre>
<p>The following values are currently supported:</p>
<ul>
<li><span class="inline-code">{YYYY}</span> &#8211; the year</li>
<li><span class="inline-code">{MM}</span> &#8211; the month</li>
<li><span class="inline-code">{DD}</span> &#8211; the day</li>
<li><span class="inline-code">{SVN}</span> &#8211; SubVersion revision number (more on this a bit later)</li>
</ul>
<p>&nbsp;</p>
<ol start="5">
<li>Now, open up the Project Properties &gt; Build Events, and add the following Pre-build event command line (on a single line):</li>
</ol>
<pre>

"C:\Path\To\wtv.exe"
  "$(ProjectDir)Properties\AssemblyInfo.Template.cs"
  "$(ProjectDir)Properties\AssemblyInfo.cs"
</pre>
<p>&nbsp;</p>
<ol start="6">
<li>Finally, set the Build Action for the new <span class="inline-code">AssemblyInfo.Template.cs</span> file to &#8216;None&#8217; and Build!</li>
</ol>
<p>&nbsp;</p>
<p>If you get stuck, pop open a Command Prompt and run <span class="inline-code">WTV.exe</span> without any parameters:</p>
<p><a href="http://www.fatlemon.co.uk/wp-content/uploads/wtv-date-version-command-prompt-usage1.png"><img class="alignnone size-full wp-image-223" title="WTV: Command prompt usage" src="http://www.fatlemon.co.uk/wp-content/uploads/wtv-date-version-command-prompt-usage1.png" alt="" width="632" height="342" /></a></p>
<h4>SubVersion support</h4>
<p>If you&#8217;re using SubVersion for your source control, <span class="inline-code">WTV</span> can also put the projects SVN revision number into the application version.</p>
<p><em><strong>There&#8217;s only one caveat</strong></em> &#8211; .Net Application version number values are limited to a maximum of 32,767 (i.e. they&#8217;re Int16&#8242;s). Therefore, if your SVN revision is higher <span class="inline-code">WTV</span> will only use the last 4 digits: e.g.: 32768 -&gt; 2768.</p>
<p>To use the SVN revision number, simply add the <span class="inline-code">{SVN}</span> placeholder to your <span class="inline-code">AssemblyInfo.Template.cs</span> file like so:</p>
<pre>

[assembly: AssemblyVersion("{YYYY}.{MM}.{DD}.{SVN}")]
[assembly: AssemblyFileVersion("{YYYY}.{MM}.{DD}.{SVN}")]
</pre>
<p>&nbsp;</p>
<p>.. and then add a couple of extra parameters to the Pre-build event command line so <span class="inline-code">WTV</span> knows where <span class="inline-code">SubWCRev.exe</span> is, something like this (again, on a single line):</p>
<pre>

"C:\Path\To\WTV.exe"
  "$(ProjectDir)Properties\AssemblyInfo.Template.cs"
  "$(ProjectDir)Properties\AssemblyInfo.cs"
  "C:\Program Files\TortoiseSVN\bin\SubWCRev.exe"
  "$(SolutionDir)."
</pre>
<p>&nbsp;<br />
And because the <span class="inline-code">AssemblyInfo.cs</span> is generated, you can remove it from source control (but don&#8217;t remove it from your project!)</p>
<p>&nbsp;</p>
<h4>Finally</h4>
<p>Comments and suggestions are welcome here or on the <a title="WhenTheVersion - Issue tracker at GitHub" href="https://github.com/AndrewFreemantle/When-The-Version/issues">projects issue tracker over at GitHub</a>. I hope you find it useful  <img src='http://www.fatlemon.co.uk/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><script src="/wp-content/syntax-highlighter/Scripts/shCore.js"></script> <script src="/wp-content/syntax-highlighter/Scripts/shBrushVb.js"></script></p>
<p><script type="text/javascript">// <![CDATA[
  dp.SyntaxHighlighter.ClipboardSwf = '/wp-content/syntax-highlighter/Scripts/clipboard.swf'; dp.SyntaxHighlighter.HighlightAll('code');
// ]]&gt;</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.fatlemon.co.uk/2011/11/wtv-automatic-date-based-version-numbering-for-net-with-whentheversion/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Automatic SVN Revision Numbering in ASP.Net MVC</title>
		<link>http://www.fatlemon.co.uk/2008/12/automatic-svn-revision-numbering-in-aspnet-mvc/</link>
		<comments>http://www.fatlemon.co.uk/2008/12/automatic-svn-revision-numbering-in-aspnet-mvc/#comments</comments>
		<pubDate>Sun, 14 Dec 2008 15:00:30 +0000</pubDate>
		<dc:creator>Andrew Freemantle</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[SubVersion]]></category>

		<guid isPermaLink="false">http://www.fatlemon.co.uk/?p=20</guid>
		<description><![CDATA[Update: I&#8217;ve written an open source .Net build tool that can also put the highest SVN revision into a file automatically on build &#8211; check it out! While working on an internal ASP.Net MVC project at work, I wondered if it was possible to get the SubVersion (SVN) repository number to be automatically updated on [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Update:</strong> I&#8217;ve written an <a title="WTV: Automatic date-based version numbering for .Net with WhenTheVersion" href="http://www.fatlemon.co.uk/2011/11/wtv-automatic-date-based-version-numbering-for-net-with-whentheversion/">open source .Net build tool that can also put the highest SVN revision into a file automatically on build</a> &#8211; check it out!</p>
<hr />
<p>While working on an internal <a title="ASP.Net MVC Homepage" href="http://www.asp.net/mvc" target="_self">ASP.Net MVC</a> project at work, I wondered if it was possible to get the SubVersion (SVN) repository number to be automatically updated on the webpages I was creating every time I did a build or check-in.</p>
<p>The footer at the bottom of the <a title="StackOverflow" href="http://www.stackoverflow.com" target="_self">StackOverflow.com</a> site is what we&#8217;re trying to do here (though I suspect after listening to <a title="StackOverflow Podcast 17" href="https://stackoverflow.fogbugz.com/default.asp?W24212" target="_self">their podcasts</a> that they&#8217;ve got theirs updated using custom build tasks in <a title="Cruise Control.Net" href="http://confluence.public.thoughtworks.org/display/CCNET" target="_self">CruiseControl.Net</a>)..</p>
<p><img src="/images/svn-stackoverflow.png" alt="StackOverflow.com - SVN revision" /></p>
<p>After a couple of hours of investigation, trial and error, here&#8217;s one way to do it.</p>
<p>Before we begin, we&#8217;ll need <a title="Tortoise SVN Client" href="http://tortoisesvn.tigris.org/">TortoiseSVN</a> installed, and a local working copy of your ASP.Net MVC project checked out.</p>
<p>There are essentially 3 steps..</p>
<h3>1. Create a <a title="Html Helpers 'How Do I' video - ASP.Net MVC Website" href="http://www.asp.net/learn/mvc-videos/video-402.aspx" target="_self">Html Helper</a></h3>
<p>This greatly simplifies the return of the SVN number as a string which is then trivial to place in our View Page or footer.</p>
<p>Create the following file:</p>
<pre>
Imports System.Runtime.CompilerServices

Public Module HtmlHelpers

    &lt;extension()&gt; _
    Public Function SVNRevision(ByVal helper As HtmlHelper) As String
        Dim svnFile As IO.StreamReader
        Dim svnRev As String = String.Empty
        Try
            svnFile = New IO.StreamReader(&quot;svn_rev.txt&quot;)
            svnRev = svnFile.ReadLine()
            svnRev = svnRev.Replace(&quot;&quot;&quot;&quot;, &quot;&quot;)
            svnFile.Close()
        Catch FnFex As IO.FileNotFoundException
            &#039;swallow, but write out the file and location we tried to read..
            Trace.WriteLine(&quot;HtmlHelper.SVNRevision: Ex: [&quot; &amp; FnFex.Message &amp; &quot;]&quot;)
        Finally
            If svnRev.Length = 0 Then svnRev = &quot;-&quot;
        End Try

        Return svnRev
    End Function
End Module
</pre>
<h3>2. Create a Batch File and add it as a custom build task to the project</h3>
<p>Next we create a Batch File (.bat) in the root of our project, which will call the TortoiseSVN command to retrieve the SVN revision number from our working copy.<br />
We&#8217;ll call it &#8220;CreateSvnRevFile.bat&#8221;.  Here&#8217;s the contents:</p>
<pre>
@echo off
rem # CreateSvnRevFile.bat
rem # Check the Working Copy folder is passed in..
if /I [%1%]==[] goto usage else goto start
:start
rem # (Re)Create the base file so SubWCRev has something to replace
echo "$WCREV$" &gt; svn_rev.txt
rem # Ask SubWCRev to replace with the latest revision
rem #  from the current (working copy) directory
C:\Progra~1\TortoiseSVN\bin\SubWCRev.exe %1 svn_rev.txt svn_rev.txt
rem # Copy the file for local hosting and testing purposes..
copy svn_rev.txt "C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE" /Y
del svn_rev.txt /Q
goto end

:usage
echo Usage: %0 SvnWorkingCopyDir

:end
rem exit 0
</pre>
<p>Then we need to add a custom Build Event action to call the Batch File. (The Build Events dialog is in &#8220;My Project &gt; Compile &gt; Build Events&#8221;)</p>
<p><img src="/images/svn-buildevents.png" alt="My Project - Compile - Build Events" /></p>
<p>As shown above, we need to add the line to the &#8220;Post-build event command line:&#8221; box..</p>
<pre>
call "$(ProjectDir)CreateSvnRevFile.bat" "$(SolutionDir)\"
</pre>
<h3>3. Add the SVN revision number to our Page</h3>
<p>With all of that background plumbing in place, all we need to do now is add a couple of lines of code in the appropriate .aspx file wherever we want our SVN revision number to appear:</p>
<pre>
  ...
  &lt;%@ import Namespace=&quot;MyMvcProject&quot; %&gt;
  ...
  &lt;%=Html.SVNRevision()%&gt;
  ...
</pre>
<p>the first line imports our Project Namespace (which will obviously depend on your project &#8211; you may need to rebuild at this point before the =Html. autocomplete picks up the &#8216;SVNRevision()&#8217; HtmlHelper).</p>
<p>And we&#8217;re done:</p>
<p><img src="/images/svn-opsweb.png" alt="" /></p>
<h4>So, how does it work?</h4>
<p>The Post-build task CreateSvnRevFile.bat does most of the work by creating a temporary text file called svn_rev.txt containing the string &#8220;$WCREV$&#8221;, and then calls the TortoiseSVN command SubWCRev.exe on the text file to replace the string with the working copy revision number.  It&#8217;s worth noting that the revision number is held in the working copy .svn folder so this method doesn&#8217;t require a call to your SVN server to work.</p>
<p>The Html Helper code then simply tries to read the updated text file and returns the contents as a string, or it returns a dash (&#8220;-&#8221;) if there was a problem.</p>
<p>Obviously, if we&#8217;re using this on a high-traffic website or frequently referenced page then we&#8217;d want to cache the string rather than reading it from the file every time. We could do this by modifying the Html Helper to read the file once into a variable, which we could do by wrapping the actual file reading up in a singleton object, for example.</p>
<p>I hope you find it useful  <img src='http://www.fatlemon.co.uk/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.fatlemon.co.uk/2008/12/automatic-svn-revision-numbering-in-aspnet-mvc/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

