2013-07-11

How to embed TFS revision number into C# WCF project

Once in a while there is a need to see whether the code changes where deployed in one or another environment. I.e. does QA box runs prod or latest dev version/branch. Or to make it worth between few developers who using shared deployment environment where it is uncertain what files each developer is working on. The answer would be in service which returns the revision of module folder and changed files set. Manual modification is quite painful and only answer is the automated build processing.

The idea is simple and could be applicable with any kind of project and version control. The pre-build step should run the script which get history for project folder, preserving only last record. Than run status check to expose files modified after check-out. The output is embedded into rendered service code.
Sample for WCF service generated from TFS output:

set path=%path%;%1%
set outFile=%~dp0ClassFiles\Revision.cs
echo using System.ServiceModel.Web;using System.ServiceModel;using System.ServiceModel.Activation; >%outFile%
echo namespace BSA_EventPlanner.ClassFiles >>%outFile%
echo { [ServiceContract][System.ServiceModel.Activation.AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] >>%outFile%
echo  public class Revision >>%outFile%
echo  { [WebInvoke(UriTemplate = "/", Method = "GET", ResponseFormat = WebMessageFormat.Xml)] >>%outFile%
echo   public string EntitieList() >>%outFile%
echo   {  var s = @^" >>%outFile%
tf history %~dp0 /stopafter:1  /recursive /format:brief /noprompt >>%outFile%
tf status %~dp0 /recursive >>%outFile%
echo ^";return s;}}}  >>%outFile%

The rendered code will look like:
using System.ServiceModel.Web;
using System.ServiceModel;
using System.ServiceModel.Activation; 
namespace BSA_EventPlanner.ClassFiles 
{ [ServiceContract]
        [System.ServiceModel.Activation.AspNetCompatibilityRequirements(RequirementsMode =
 AspNetCompatibilityRequirementsMode.Allowed)] 
 public class Revision 
 { [WebInvoke(UriTemplate = "/", Method = "GET", ResponseFormat = 
                WebMessageFormat.Xml)] 
  public string EntitieList() 
  {  var s = @" 
";return s;}}}  

The string is empty if TFS is not available, otherwise it will be filled with revision and modified files list.
Do not forget to add the WCF routing and ClassFiles\Revision.cs file to project.

Happy coding!


No comments:

Post a Comment