October 27, 2006

dotNET Project Template and Addin Creation

Project and Item Templates
Create Reusable Project and Item Templates for the development team.

Project Template Creation
Creation of a project or item template is pretty straight forward.

Use the File->Export Template to create the template, the wizard will ask you which Project you wish to base your Template on, this will result in the contents of that Project being added to the Template.
Note that depending on the type of Project you select will determine where the Template appears in the File->New Project dialog.
i.e. If you base your Template on a Website Project in VB then the Template will appear in the File-New Website and under the VB section in the File->New Project dialog.


Create a project like the one found at
http://msdn.microsoft.com/msdnmag/issues/06/01/CodeTemplates/default.aspx.

The Template is just a zip file with an extra xml file within, the xml file describes what to add to the project when a user uses File->New Project.

If you wish to generate Project Templates at build time from another project it can be done through MSBuild. I've written a description of this in the MSBuild Post, ZipProject is the MSBuild task.


Editing the Contents of the Template File
The File->Export Template will create the template for you. When the user uses this template they are not prompted for anthing besides the Project name, what if you wish for the user to supply some additional information when creating a new project using your template? It's not very easy...
Basically you have to create a seperate class which is a type of Form, it's an implementation of IWizard. The assembly generate needs to be registered in the GAC and then a reference to this Assembly added to your Template file, see this msdn link:
http://msdn2.microsoft.com/en-us/library/ms185301.aspx.
Note that one of the steps is the Signing the Assembly, use the VS feature in the Project Properties for this. And then run C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0>gacutil /i
C:\temp\WizIDE\WizIDE\bin\Debug\WizIDE.dll

Now we have installed the assembly. After the assembly is installed you should use the File->Export Template. After that youwill need to edit the XML file, so unzip the file at and edit the xml file Add the assembly name details which were generated with the Project->Properties->Signing feature, to see what the value is used gacutil again
C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0>gacutil /l WizIDE

And add the result to the xml file after , that is outside of TemplateContent, like the following:
WizIDE, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=1d20aa67bdcd2085,
processorArchitecture=
IDE.IWizardImplementation

Now Rezip and open a new project of this type. If you change the assemble functionality then maybe a reinstall is required C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0>gacutil /uf WizIDE


Web Application Template Creation
The template is generated in the same manner as the project template, select your website project and use File->Export Template.
As with the project template, templates can be created at build time from your project using MSBuild, see my MSBuild post, ZipProject is the MSBuild task, you'll see that the Web application template is a bit more difficult to configure from MSBuild, there are alot of workarounds to include subfolders in the resultant zip file.


String Substitution in Templates
Project Template also provide the ability to substitute strings for strings in the files included in the Template.
Your .template file which is created for you (and included in the .zip file) contains a list of all files in the Template. It also contains some properties of each file including a boolean which tells the Template installer to substitute certain strings when the Template is used.
e.g.
(open tag)
ProjectItem ReplaceParameters="true" TargetFileName="app.aspx">app.aspx
app.aspx.cs
$safeprojectname$ gets replaced with the name you give to your project.
e.g.
The original name of your project was "Server".
You generate a Project Template from Server.
The Template Generator sets all the file entries in the template file to have substitution on.
The Template Generator sets replaces all entries of the word "Server" in all of those file with the placeholder
$safeprojectname$.
You create a new Project using the Template and name it "MyNewProject".
When you create a Project with the Template the placeholder
$safeprojectname$ is replaced with the name of your project.

in the asp.aspx.cs file
using MyNameSpace.Server; is replaced with MyNameSpace.MyNewProject;

Original app.aspx.cs in Project named "Server".
using System.Collections.Generic;
using MyNameSpace.Runtime;
using MyNameSpace.Server;

The app.aspx.cs generated and add to the Template"Server".
using System.Collections.Generic;
using MyNameSpace.Runtime;
using MyNameSpace.
$safeprojectname$;

The new app.aspx.cs in new Project named "MyNewProject".
using System.Collections.Generic;
using MyNameSpace.Runtime;
using MyNameSpace.
MyNewProject;


http://msdn2.microsoft.com/en-us/library/eehb4faa(VS.80).aspx

Other properties which can be substituted are:
msdn Template Parameters.
msdn (
Modifying or Creating a Visual Studio Project Template).

Addin Creation
Addin Creation To create custom controls, menu items etc use the Addin. There is an Addin Project in VS, create a new Addin Project from scratch and compile. To register the new addin choose Tools->Addin Manager, select the new Addin from the list and restart VS, the Addin Tools items now appear in the menu. The Addin’s .addin file has been copied to
C:\Documents and Settings\\My Documents\Visual Studio 2005\Addins

http://msdn2.microsoft.com/en-us/library/80493a3w.aspx
Addin examples can be downloaded from
microsoft


Item Template Creation
In order to add a custom item to the New Item command we have to create another template and Item Template. I have developed this in particular for the Entity creation.
It is virtually identical in makeup to the Project Template but with a different form for data entry. The installation process is the same as the Project Template earlier except this time you create an Item Template instead of a Project Template when using File->Export Template.

Summary
When you create a Template the template .zip file gets copied into 3 locations where VS will detect them
The original location:
C:\Documents and Settings\me\My Documents\Visual Studio 2005\My Exported Templates
the Visual Studio Project Template location (This is an optional step in the Export Template Wizard):
C:\Documents and Settings\me\My Documents\Visual Studio 2005\Templates\ProjectTemplates
and also a cache location
C:\Documents and Settings\me\Application Data\Microsoft\VisualStudio\8.0\ProjectTemplatesCache
The Cache location may cause confusion, if you no longer want to use the Template and decide to delete it you must also delete from the Cache location.


Problem here when referencing the DLLs, because we have made the Templates “Strongly Named” we cannot reference the “Weakly Named” assemblies. Solution?? Currently there are 3 projects. WizIDE which creates a Project Template, a dll, WizIDE.dll which is added to the Project Template as the Zip file WizIDETemplate.zip. IDEAddin, creates a DLL, IDEAddin.dll, this is is installed through the VS Addin Manager, it is added as an .addin file to the
required, presently it adds a new item to the Project menu which does nothing right now. EventItemTemplate, creates a DLL, EventItemTemplate.dll, this is installed in the same manner as the Project Template at C:\Documents and Settings\\My Documents\Visual Studio 2005\Addins.
Creates the extra menu items which maybe
C:\Documents and Settings\\My Documents\Visual Studio 2005\Templates\ItemTemplates\EventItemTemplate.zip
.
This provides the functionality to create new Entity through a wizard, New Item dialog now contains another item type, EntityItemTemplate which launches a wizard and prompts for Entity parameters.




Installing the Project and Item Templates
If you create your Project and Item Templates using MSBuild from your source projects using ZipProject and ZipItem then the templates will be installed for you by MSBuild into the correct location (note a copy of these files can be found in your source projects obj/Debug or whatever directory). But of course you'll want to install these templates on a clients machine using an installer of something equivelant., the solution is to copy the files into the Visual Studio installation from your installer using a Custom Action (the custom action is a project you create, it inherits installer and can be called from the installer). You'll also need to reset the Visual Studio Cache, this can be done from the Custom Task code too

System.Diagnostics.ProcessStartInfo ps = new System.Diagnostics.ProcessStartInfo(Path.Combine(vsDevenv, "devenv.exe"));
ps.Arguments = @"/setup";
System.Diagnostics.Process.Start(ps).WaitForExit(10000);//give the process a max of 10sec to finish

No comments: