Wednesday, February 27, 2008

Step by Step building a project template and wizard in VS 2005

Project template is a template that contains classes,resource files, or any files that come together into a project. They usually use to accelerate development process by providing reusable projects and  items so the developer can remove repetitive task to create projects from scratch.

Project template can use wizard to get input from user. Developer only implements IWizard interface and simple Windows Form.

This step by step assume that you  know how to use Visual Studio 2005, .NET 2.0 Configuration, and Windows Explorer.

Step 1, Create class library project as template project

First thing to do builds a blank projects with classes or any files that you want and give parameters do you like. The parameters can be reserved or custom; and case sensitive. For reserve and custom template you can see in the MSDN.  For the sample, I use 1 class and 1 text file.

Sample Class File : Class1.cs

using System;
using System.Collections.Generic;
using System.Text;

namespace $safeprojectname$
{
/// <summary>
/// This class is build at $time$
/// By: $usercreated$
/// </summary>
public class Class1
{
}
}


Sample text file : readme.txt



Project name : $projectname$

Motivation : $motivation$



Remember, that project above cannot compile because of the parameters.



On the sample above, there are 5 template parameters; $safeprojectname$, $time$, and $projectname$ are reserved template parameters and, $usercreated$ and  $motivation$ are custom template parameters.



After all class is ready, just export the template using menu File --> Export Template and choose project template. When it finish, just unload it from Visual Studio 2005. Look the result in C:\Documents and Settings\"username"\My Documents\Visual Studio 2005\My Exported Templates, when User name is your current user folder in my document.



Step 2, Create Windows Application project



In the Form1, You can add textbox as an input in the Form designer, give a nice name to the control like txtProjectName; and button to accept it.



Add a property as a collection of parameters.



private Dictionary<string,string> parameters;
public Dictionary<string,string> Parameters
{
get { return parameters; }
}



In the button click, added all value from textbox into collection.



private void btnOK_Click(object sender, EventArgs e)
{
parameters = new Dictionary<string, string>();
Parameters.Add("$projectname$", this.txtProjectName.Text);
Parameters.Add("$usercreated$", this.txtUserCreated.Text);
Parameters.Add("$safeprojectname$", this.txtSafeProjectName.Text);
Parameters.Add("$time$", this.txtTime.Text);
Parameters.Add("$motivation$", this.txtMotivation.Text);
this.DialogResult = DialogResult.OK
}



Just compile and run it, and try it if there is error.



Step 3, Create Class Library Project.



Add reference of EnvDTE and Microsoft.VisualStudio.TemplateWizardInterface.dll, System.Drawing and  System.Windows.Forms. Rename the Class1 to a better name like WizardImplementation and implement IWizard interface.



Copy the Form1 above into this projects.



The code in the WizardImplementation.cs



namespace Class.Wizard
{
public class WizardImplementation:IWizard
{
#region IWizard Members

void IWizard.BeforeOpeningFile(ProjectItem projectItem)
{
}

void IWizard.ProjectFinishedGenerating(Project project)
{
}

void IWizard.ProjectItemFinishedGenerating(ProjectItem projectItem)
{
}

void IWizard.RunFinished()
{
MessageBox.Show("Finish", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

void IWizard.RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams)
{
try
{
Form1 form = new Form1();
DialogResult dlg = form.ShowDialog();
if (dlg == DialogResult.OK)
{
foreach (KeyValuePair<string, string> pair in form.Parameters)
{
if (!replacementsDictionary.ContainsKey(pair.Key))
replacementsDictionary.Add(pair.Key, pair.Value);
else
replacementsDictionary[pair.Key] = pair.Value;
}
}
form.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error : "+ex.Message,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
}

}

bool IWizard.ShouldAddProjectItem(string filePath)
{
return true;
}

#endregion
}
}



Sign the assembly using strong name. You can add it from the project properties, find the tab signing and create a new strong name key file if you don;t have one. Compile and make it sure there is no compile error.



Step 4, Create Setup Project



Open File System views, it will show 2 panel, left panel is the folder and right panel is the files. Goto left panel and add special folder --> Global Assembly Cache folder. Click the GAC folder and right click to choose add Project output. Choose the wizard project from step 3. Exclude all output/assembly/files except the primary output. Build the setup project and try to install it.



Step 5, Write the assembly in the GAC Information



Open .NET Framework 2.0 Configuration, and choose Assembly cache from the tree view, view the list and search for the assembly of the wizard. Open the assembly properties like sample below :

image





Write all information above the note and just close it.



Step 6, Rewrite vstemplate file



Find the file from Step 1, usually in the C:\Documents and Settings\"username"\My Documents\Visual Studio 2005\My Exported Templates. Extract the zip files and edit the .vstemplate



Add WizardExtension to :



<VSTemplate Version="2.0.0" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" Type="Project">


  <TemplateData>


    ...


  </TemplateData>


  <TemplateContent>


    ...


  </TemplateContent>


  <WizardExtension>

    <Assembly>Quick.Template.Wizard, Version=1.0.0.0, Culture=neutral, PublicKeyToken=3542a3b786264129, Custom=null</Assembly>


    <FullClassName>Quick.Template.Wizard.WizardImplementation</FullClassName>


  </WizardExtension>


</VSTemplate>



I just use the information from properties above to fill the assembly and full class name. Save the vstemplate file.



Select the extracted files, right click --> choose Sent to -->Compressed(zipped) folder.



Move the result zip files to C:\Documents and Settings\Nicko\My Documents\Visual Studio 2005\Templates\ProjectTemplates\Visual C#



Step 7, Use the template



From the solutions, add new project and choose your template project from Visual C# My Templates. You can see that the form that implement wizard will shown.



kick it on DotNetKicks.com

No comments: