Traceability in .NET–.tt files–add changeset – part 5 of 7
We wish to add , from the .tt file , the id of the last TFS checkin. For this purpose we will connect to TFS and we will investigate in the current project the latest change.
We will use the facility of .tt file to connect to the host and ask for various features ( such as TFS )
The .tt file code is:
001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | <#@ template debug= "false" hostspecific= "true" language= "C#" #> <#@ assembly name= "System.Core" #> <#@ assembly name= "Microsoft.VisualStudio.Shell.Interop.8.0" #> <#@ assembly name= "EnvDTE" #> <#@ assembly name= "EnvDTE80" #> <#@ assembly name= "Microsoft.VisualStudio.TeamFoundation.VersionControl" #> <#@ assembly name= "Microsoft.TeamFoundation.Client" #> <#@ assembly name= "Microsoft.TeamFoundation.Common" #> <#@ assembly name= "Microsoft.TeamFoundation" #> <#@ assembly name= "Microsoft.TeamFoundation.WorkItemTracking.Client" #> <#@ assembly name= "Microsoft.TeamFoundation.VersionControl.Client" #> <#@ assembly name= "Microsoft.TeamFoundation.ProjectManagement" #> <#@ import namespace = "Microsoft.TeamFoundation.Client" #> <#@ import namespace = "Microsoft.TeamFoundation.VersionControl.Client" #> <#@ import namespace = "System.Linq" #> <#@ import namespace = "System.Text" #> <#@ import namespace = "System.Text.RegularExpressions" #> <#@ import namespace = "System.Collections.Generic" #> <#@ import namespace = "EnvDTE" #> <#@ import namespace = "EnvDTE80" #> <#@ output extension= ".cs" #> <# DTE dte= null ; var serviceProvider = Host as IServiceProvider; if (serviceProvider != null ) { dte = serviceProvider.GetService( typeof (DTE)) as DTE; } if (dte == null ) { throw new Exception( "generate build number can only execute through the Visual Studio IDE" ); } ProjectItem projectItem = dte.Solution.FindProjectItem(Host.TemplateFile); int netVersion=0; var proj=projectItem.ContainingProject; var configmgr = proj.ConfigurationManager; var config = configmgr.ActiveConfiguration; string regex= @"^.+?Version=v(?<version>\-?\d+\.\d+).*?$" ; var options = RegexOptions.Multiline; string input= proj.Properties.Item( "TargetFrameworkMoniker" ).Value.ToString(); MatchCollection matches = Regex.Matches(input,regex,options); foreach (Match match in matches) { netVersion = ( int )( double .Parse(match.Groups[ "version" ].Value)*100); } string filePath = proj.FullName; string dirPath = System.IO.Path.GetDirectoryName(filePath); var wsInfo = Microsoft.TeamFoundation.VersionControl.Client.Workstation.Current.GetLocalWorkspaceInfo(filePath ); // Get the TeamProjectCollection and VersionControl server associated with the // WorkspaceInfo var tpc = new TfsTeamProjectCollection(wsInfo.ServerUri); var vcServer = tpc.GetService<VersionControlServer>(); // Now get the actual Workspace OM object var ws = vcServer.GetWorkspace(wsInfo); // We are interested in the current version of the workspace var versionSpec = VersionSpec.Latest; var historyParams = new QueryHistoryParameters(dirPath, RecursionType.Full); historyParams.ItemVersion = versionSpec; historyParams.VersionEnd = versionSpec; historyParams.MaxResults = 1; var changeset = vcServer.QueryHistory(historyParams).FirstOrDefault(); var dt = DateTime.Now; var userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name; userName = userName.Split( '\\' ).Last(); #> using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; // General Information about an assembly is controlled through the following // set of attributes. Change these attribute values to modify the information // associated with an assembly. [assembly: AssemblyTitle( "BuildTraceabilityDemo" )] [assembly: AssemblyDescription( "BuildDate,<#=dt.ToString(" yyyyMMdd_HHmmss ")#>\r\n<#=proj.Properties.Item(" TargetFrameworkMoniker ").Value.ToString()#>\r\nBuild by,<#=userName#>\r\nConfig,<#=config.ConfigurationName#>,\r\nChangeset,<#=changeset.ChangesetId#>" )] [assembly: AssemblyConfiguration( "" )] [assembly: AssemblyCompany( "" )] [assembly: AssemblyProduct( "BuildTraceabilityDemo" )] [assembly: AssemblyCopyright( "Copyright © <#= dt.Year#>" )] [assembly: AssemblyTrademark( "" )] [assembly: AssemblyCulture( "" )] // Setting ComVisible to false makes the types in this assembly not visible // to COM components. If you need to access a type in this assembly from // COM, set the ComVisible attribute to true on that type. [assembly: ComVisible( false )] // The following GUID is for the ID of the typelib if this project is exposed to COM [assembly: Guid( "75ff7863-cb83-4d9b-80de-4a0de2781918" )] // 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 Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] [assembly: AssemblyVersion( "1.0.0.0" )] [assembly: AssemblyFileVersion( "1.<#=dt.Year#>.<#=dt.Month#>.<#=dt.Day#>" )] |
Video : http://youtu.be/A_qSdVV93qk
Demo project here : https://traceabilitydemo.codeplex.com/releases/view/132231
Source code here : https://traceabilitydemo.codeplex.com/SourceControl/changeset/view/110446