Many times when you are adding a new item to a project you probably find yourself changing the generated item to line up with your coding or company styles. Often this includes a copyright, perhaps the ordering of members in a type or even completely replacing the item with something else, like an enum. Visual Studio is designed to be extensible. Item templates allow you to create your own templates and have them available in VS so you don’t have to keep making the same changes over and over again. In VS 2017 templates are even easier to create than before. I will discuss how easy it is to add new templates in this article.
T4 Templates Update for Visual Studio 2015
A while back I posted a series of articles on how to use T4 and a custom VS extension to simplify some common code like application settings, WCF clients and environmental transforms. With the release of Visual Studio 2015 I had to update my own extension and templates so I wanted to posted a follow up article on the changes that need to be made to allow the extension to work with Visual Studio 2015. As part of the update I added some functionality to the app settings template. Before continuing be sure to download the previous version of the series (or simply download the final version below).
BuildVer Update
UPDATE: A new update (v2.1) that resolves a few issues:
- Character set conflicts when using non-ASCII characters
- Updated documentation on how to integrate version.rci without having VS delete it the next time you open the resource designer
- Fix for parameters that are built from the product version
I recently had the need to update my BuildVer tool to use in newer projects. BuildVer is an incremental versioning tool that can be used for automated builds. It was originally written to allow us to use the Win32 VERSIONINFO resource but have it updated for each build. Later functionality was added to support generating standard .NET assembly info files. While the existing tool was sufficient I decided that it was time to update the code and, at the same time, add some new features.
The original code was written over a decade ago in C++. Later it was updated for .NET support and to use MFC types. The new tool is completely written in C#. The original version could generate 1 or 2 files (a managed and unmanaged file) and an optional text file. Some support was added for specifying the build information on the command line. This was sufficient to meet the needs of the time.
v2 supports generating any number of output files with their corresponding template files. The tool works with any text file. Additionally the configuration file has been expanded to allow for specifying arbitrary parameter name-value sets that can be used in template files. There are a couple of pre-defined sets for versioning, company information, etc. Any of the parameter values can be overridden by the command line. This allows for more flexibility in the tool while keeping the command line options concise.
The readme.txt file contains all the details. The attached file contains the source code and a v4 version of the tool along with sample templates for C# and C++. Feel free to use the tool as you see fit. One note however, some of the code in the tool (specifically the command line processor) is copyright Carlson Engineering, Inc. and may not be reused without explicit permission. You can compile and use the tool itself without permission.
UpdateUserType (Visual Studio Addin)
Here is another addin I wrote for VS2008 and then updated for VS2010. In C++ you can define your own custom keywords and VS will color them differently if you say so (ToolsOptions -> Fonts and Colors). There are a couple of problems with the VS solution.
- You have to edit the UserType.dat file.
- You have to restart VS.
- You have only one set of keywords for all of C++.
The second and third problems are the killers here. As you are developing code you will often find a set of keywords you’ll want to add. To do so you’ll have to open the .dat file, add the keywords and then restart VS. Even worse is that if you are working on an MFC project then you’ll likely want some MFC keywords but if you switch to Win32 then you want a different set of keywords. To resolve this problem you’ll have to keep multiple .dat files that you can swap in and out. Enter UpdateUserType.
UpdateUserType does a couple of things. Firstly it allows you to separate your keywords into different files (i.e. C++, MFC, Win32). Secondly UpdateUserType can merge all these files into a single .dat file. Thirdly the addin will detect when the .dat file changes and request VS to refresh its keyword list.
Using the addin UI you can add a list of .txt files to be monitored. Whenever one of these files is modified the addin merges them all into a new .dat file. This allows you to separate keywords by area and enable only the keywords appropriate for your current project. My personal technique is to keep one or more of them open in a separate editor so that I can add new keywords as I go through code. Saving the files in the editor causes the .dat file to be regenerated.
When the .dat file changes the addin requests VS to refresh the keyword list. When this happens VS will read the .dat file and begin coloring the keywords again. This feature allows you to add keywords on the fly and have VS begin coloring them almost instantiately.
Attached is the source and binaries for the VS2010 version of this addin. Additionally I’ve included some starter .txt files that you can use. A caveat is in order. The .XML file where the addin stores the files to be merged supports relative or absolute paths. However the UI and the code only supports absolute paths. Therefore if you use the UI to change the file list then the paths are converted to absolute paths.
To install the addin copy the contents of the Bin directory to your %My Documents%Visual Studio 2010Addins directory. You might need to enable the addin via ToolsAddin Manager. If the addin does not show up (it’ll appear under the Tools menu) then you might need to force a re-registration.
CodeHTMLer (Visual Studio Addin)
CodeHTMLer is a utility written by Weshaggard and available here. The utility allows you to convert code to HTML for insertion into a website or blog. It happens to be the tool I use for code on this site. I know, I know. There are many tools already available for this but I prefer CodeHTMLer for several reasons.
- It supports multiple languages.
- The HTML is partially configurable via the XML file it loads.
- Some code elements can be ignored rather than having to generate style information for everything.
- Can use either existing CSS styles or inline styles.
- It is fast.
The only issue I had with it was that I wanted to use it inside Visual Studio. Being a developer I did the logical thing and wrote an addin for it. The addin was originally written for VS2008 and the updated for VS2010. The attached code runs under VS2010. The addin wraps CodeHTMLer and adds the following features.
- Copy as HTML is exposed as an option in the editor.
- A UI is added to allow you to change the options in the XML file to control how HTML is generated.
- Uses the VS code model to determine which language definition to use for formatting so you can format VB one way and C# another, for example.
I only ran into a couple of problems with CodeHTMLer. These mainly revolved around the manner in which it expected to be used. I had to modify (and annotate) the areas I changed. Furthermore I needed a few additional options for controlling formatting so I extended the language definition code to add a few extra attributes along with the serialization logic. So the version that is included here is a modified version of the one available from CodePlex. As a final note the attached language definition file is modified to support how VS names languages. The original version did not use the same name for C# as VS.
IMPORTANT: I don’t take credit for CodeHTMLer or any of its source. I only take credit for the addin code. Please support the original author for CodeHTMLer.
The current project is for VS2010 but you can recompile the code if you want a VS2008 version. To use this code extract the binaries to your addin directory (%My Documents%Visual Studio 2010Addins). In VS you probably need to enable the loading of the addin via the ToolsAddin Manager command. If it does not show up then you might need to modify the addin file to force a re-registration. The addin shows up as an item in the Tools menu and as a context menu option when you right-click in the code area.