After the various business matters (Napster, Kazaa...) which shook the File sharing generation, an unexpected sponsor contributes to its legitimity (or legality). Microsoft, by offering a new model for defining templates for projects and items in Visual Studio 2005, opens the "zip and share" to your code artefacts.
Share your code templates with friends or colleagues. And we can think that soon we'll find a specific support of these files on IPOD.
But the fantastic "Export template ..." feature is not without weaknesses : the first is the lack of a graphic editor and the second is that no source control support is available. A graphic editor could be so useful, especially when you want to update the item template (adding a wizard...) that is generated by the export template wizard. And you would certainly like to have a better deployment mechanism. For example a checkbox, allowing to open the generated item template in Visual Studio, will be a great solution.
While waiting for possible improvements, and if wizards don't make you stupid, you can sublimate the definition of an item template. Let us note that an item template is simply a zipped set of files containing at least one metadata file (*.vstemplate) and a code file.
Which tools can you use to industrialize the definition of an item template ? What is the best xml editor ? What is the name of this fantastic deployment tool available on the .NET plateform ? We'll use the XML editor provided by default by Visual Studio and MSBuild.
Indeed, a first stage would be to define a project template which would allow the definition of vstemplate file and which would generate item template from the reading of this file. The actions in order to create this promising project template would be the following :
- Define a custom project template.
- Create a specific msbuild target file. This file will be imported by the csproj file of project template defined above.
- Implement specific msbuild tasks making it possible to zip a set of files, to get registry information.
- Insert the calls to these tasks when the famous build target is executed.
- Copy the generated item template to the relevant user's personal data folder.
Creating custom MSBuild tasks
We create at least two tasks : one is to gather registry information and the other is to copy the generated template to the relevant user's personal data folder.
MSBuild offers many tasks by default, unfortunately it is far from being sufficient. But don't panic, Microsoft Research UK has already launched a project which regroups a hundred tasks : .NET SDC Solution Build & Deployment Process & Tools. For what we want to do, we'll spare you the adoption of this project. The definition of an item template is based on the creation of a vstemplate file. We'll suppose that we'll create a simple item template. From a vstemplate file, we can know the path of the image file that serves as the icon for this template and especially the path of the file that is included in this template. The first msbuild task will consist in getting these paths (in order to zip these files to create the item template). The second task will be to get the path of the relevant user's personal data folder.
Get information from the vstemplate file.
To define an item template, it is necessary to zip the vstemplate file and the files that it references.
The path of these files are provided by the vstemplate file. This file is an xml file. Its schema file is available locally at [Program Files]\Microsoft Visual Studio 8\Xml\Schemas\1033\vstemplate.xsd. The installation path of your favorite IDE can be obtained from a data value named "ProductDir" of the registry key named "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\8.0\Setup\VS". Of course, the Visual Studio Template Schema Reference is available online on the msdn site, too.
It is therefore very simple to get the requested information (image file path, code file path...) in using the XPathNavigator class.
Download source code.
Below, you'll find a rapid preview of the GetVSTemplateInfo msbuild task proposed above.
Get the path of the relevant user's personal data folder.
The item templates are saved in a specic user's personal data folder. You can get its path from the registry key named "HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\8.0". One of data values of this key (UserItemTemplatesLocation) is the desired path.
Download source code.
Below, you'll find again a rapid preview of the msbuild task proposed above.
Zip the vstemplate file and the files that it references.
It is necessary to zip the vstemplate file and the files that it references to create an item template. Before developping such a msbuild task, I propose you to read, if it's not yet done, my previous post about the zip compression with the .NET framework : Your zip is open.
Don't forget that the two previous build tasks have been provided by the .NET SDC Solution Build & Deployment Process & Tools. And for those who are not to found off gotdotnet user sample, they can join the MSBuild Community Tasks Project. Personally, compared to the tasks listed previously, I prefer the code produced by our english friends.
Copy the item template zip file to the relevant user's personal data folder
In order for your IDE to use your new item template, you must copy it to the folder (the path has been got in a previous task). MSBuild provides a such task : Copy Task.
I chose to merge the GetVSTemplateInfo task, the zip task and the copy task in a same task that I named "DeployItemTemplate". This (bad ?) choice allowed me to produce a msbuild target file more quickly.
Download source code.
Again, below you'll find a preview of the implementation of this msbuild task.
Define a custom project template.
Now, we have all the msbuild tasks required in order to define our project template. This project template will be based on specific msbuild target file that its csproj file will import.
What are the traps to avoid ?
- Trust your msbuild target file.
- Create an file tree structure for the project template.
Creating a msbuild target file.
We're going to declare a specific msbuild target which will be executed when the project is built. This target will call the tasks that have been previously presented.
The declaration of this target is done in a specific msbuild target file. If you don't want to face a recurrent warning message when you use your project template, you must trust the custom msbuild target file.
To do this, it's necessary to add a specific string value in the registry key named "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\8.0\MSBuild\SafeImports". The value data of this string value must be the path of the msbuild target file, no rule for the value name.
Create an arborescence for the project template.
There must be one and only one vstemplate file by template. So, if you want to add by default to your project template a sample vstemplate file, it cannot be at the same level as the project template.
Download full source code (tasks, file target ...).
Conclusion
Of course, I'm convinced that Microsoft will add quickly a checkbox allowing to open the generated item template in Visual Studio and to modify it and to put it under source control and to wash it ....
If your nights are too long, and the trackmania nations servers don't respond, it is possible to add to our project template a custom item template (with wizards) that create vstemplate file.
I did not want to waste my time to create a specific msi package to install the project template, I definitely lost some time when creating a video demonstrating this project template in action.
Download demonstration video.