Thursday, October 21, 2010

SharpDevelop :-- Project Template

This blog is about creating custom project template and integrating it with the SharpDevelop Studio. Whenever we wanted to create a new project whether it is winform project , class library ,usercontrol etc ,we are presented with predefined project template in New Project Dialog Box. In Visual studio or VSTA the project templates are the msbuild project templates and are installed with the IDE . ShareDevelop project templates are the custom project template files similar to the VS templates with xml format. PropertyService of SharpDevelop contains all the info about the configurable and global properties. To get the path of the SharpDevelop project templates we can use PropertyService to find the path. PropertyService member DataDirectory gives us the location where project and file templates are stored .

So SharpDevelop project templates are stored at location :--

Path.Combine(PropertyService.DataDirectory, "templates", "project");

Under this path there are folders named "CSharp" and "VBNet" .These folder contains the respective project template for these languages. SharpDevelop project templates are xml file with extension ".xpt".

Lets examine the project template for example "FormsProject.xpt" .



<Template created="06/10/2001" templateType="MyTemplateCS">
<TemplateConfiguration>
<Name>MyTemplate</Name>
<Category>C#</Category>
<Subcategory>MyCatagory</Subcategory>
<Icon>C#.Project.Library</Icon>
<Description>Template for Workflow.</Description>
<SupportedTargetFrameworks>v2.0;v3.5Client</SupportedTargetFrameworks>
</TemplateConfiguration>
<Actions>
<Open filename="Main.cs" />
</Actions>
<Project language="C#">
<ProjectItems>
<Reference Include="System" />
<Reference Include="System.Data" />
</PROJECTITEMS/> language=C# name="Data_Buzz.cs" Library<> <> >
<![CDATA[
/* Our code goes here */
]]>
<</File>
</Files>
</Project>
</Template>



We can see that SharpDevelop Project Template is similar to the msbuild project template. There is opening node "Template" that has some attribute defining the general info about the template. Then comes the "TemplateConfiguration" that tells the template configuration :-

Name :- name of the template

Category :- the main folder under which to show this template in New Project Dialogbox.

SubCategory :- the folder under main folder under which to show this project template in New Project Dialog box.

Icon :- icon to show on the project template shown in New Project Dialogbox.
Supported target framework :- the .net target framework.

"Actions" node tells us which file to open up in ShareDevelop IDE when the project template is loaded. It defines the child node "Open" with attribute "filename" that defines the file to open in SharpDevelop workspace after the project is loaded. Then comes the node "Project" defining the attribute "language" .

Under Project node comes "ProjectItems" which contains the child nodes of type "reference". These nodes of type "reference" tells us what assemblies to add up as references to the project when it is loaded up in SharpDevelop IDE.

Project node also contain the child node "PropertyGroup" which contains the node about the information of the project type (exe or library) under node "OutputType".
In case of msbuild project templates we have code file (.cs or .vb) in separate files and info of these file are stored in project templates. But in SharpDevelop the code files to generate along with the project are defined inside the project template. "Project" node in project template defines the node "Files" which in turn contains the child nodes "File". "File" node contains the attributes name and language defiining the name and language of .cs or .vb file . Under "File" node there is CDATA section which contains all the code for file .

Now lets see how these templates are loaded in SharpDevelop. We can locate the New Project DialogBox under the base addin code (ICSharpCode.SharpDevelop.Project.Dialogs) . Base addin is the project (ICSharpCode.SharpDevelop.csproj) and New Project DialogBox is located at "\src\gui\dialogs". The constructor of New Project DialogBox contains the method "InitializeTemplates()" . In this method project templates are iterated and filled up in the listview of the NewProject DialogBox. The collection of project templates are fetched from the ProjectTemplate class's public property "ProjectTemplates". If we go inside the ProjectTemplate class and look at ProjectTemplates property we can see the get part of property returns ProjectTemplate Collection after calling the "UpdateTemplate()" method.

"UpdateTemplate()" method is responsible for creating the project template collection.It contains the code to load all the project templates from the DataDirectory and create in memory representation of project template xml files. So the instance of class ProjectTemplate is the runtime representation of the project template files residing in the DataDirectory.

ProjectTemplate class also contains the public property ProjectDescriptor , this property is of type "ProjectDiscriptor". Instance of ProjectDiscriptor class is also created by the Project template class and it contains the information about the files of the Project . ProjectDiscriptor class holds the in memory representation of the file node in the ProjectTemplate xml file.

So to create our own project template we have to create the ".xpt" xml project template file and place it in the DataDirectory under relative language folder (CSharp or VBNet). SharpDevelop will automatically load the project when the New Project DialogBox is opend.

If your project files are generated at runtime from other system and you have to create new project template based on them , then you have to create the project template at runtime and save it to the DataDirectory. Then you have to call the static method UpdateTemplate() of ProjectTemplate class to reload the Project template .

No comments:

Post a Comment