Archive for April, 2013

Friday, April 5th, 2013

This picks up with the code generation series. See other posts:

In this post we will be using project templates for code generation. Project templates are great when you are going to create many projects with similar boilerplate type code. For example if there is a plugin architecture and each plugin has one (or many) interfaces that it must implement.

In order to get started create a new project inside a solution. Write all the boilerplate code that is needed. Use the Template Parameters in the boilerplate code to make replacements. Note: it’s okay if the project doesn’t compile after adding template parameters. Make sure to setup any project specific build events, assembly references, output paths, etc. inside the project.


/*************************
* File: $safeprojectname$Plugin.cs
* Author: $username$
* Created: $time$
************************/

using Main;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace My.Plugins.Plugin.$safeprojectname$
{
///

/// Plugin: $projectname$
///

public class $safeprojectname$Plugin : IPluginInterface
{
///

/// Does work for the plugin
///

void IPluginInterface.DoWork()
{
// TODO: This MUST be implemented
throw new NotImplementedException();
}

///

/// User visible description of $safeprojectname$
///

///
string IPluginInterface.GetDefaultInfo()
{
return "Plugin ($safeprojectname$) created by $username$";
}

///

/// Returns true when the plugin is ready to do work
///

///
bool IPluginInterface.ReadyToGo()
{
return false;
}
}
}

After the boilerplate code is in go to File > Export Template…

ExportTemplate

This will bring up the “Export Template Wizard”. Make sure to select the project with the boilerplate code and click next.

ExportTemplateWizard1

The next page of the wizard allows some customization of the project name. Make sure to automatically import into visual studio.

ExportTemplateWizard2

That it. Write code once, reuse multiple times. To test out the new project template go to New > Project in Visual Studio. Since the project was automatically imported the new project template is available.

NewProject

The new project will have the file(s) specified with all the template parameters filled in:


/*************************
* File: My_Plugin_Template1Plugin.cs
* Author: Sirchris
* Created: 3/28/2013 6:52:06 AM
************************/

using Main;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace My.Plugins.Plugin.My_Plugin_Template1
{
///

/// Plugin: My Plugin Template1
///

public class My_Plugin_Template1Plugin : IPluginInterface
{
///

/// Does work for the plugin
///

void IPluginInterface.DoWork()
{
// TODO: This MUST be implemented
throw new NotImplementedException();
}

///

/// User visible description of My_Plugin_Template1
///

///
string IPluginInterface.GetDefaultInfo()
{
return "Plugin (My_Plugin_Template1) created by Sirchris";
}

///

/// Returns true when the plugin is ready to do work
///

///
bool IPluginInterface.ReadyToGo()
{
return false;
}
}
}

That’s a quick bear minimum of what can be done with project templates. Project templates can package up any set of files. In the exported template created look at the vstemplate file. There you can start to get a feel for some of the additional items that could be accomplished with templates. Make sure to look at the VSTemplate section on the MSDN.

Here is the vstemplate file created from this simple example




My Plugin Template
This is to create plugins faster
CSharp


1000
true
My Plugin Template
true
Enabled
true
__TemplateIcon.ico



Class1.cs

AssemblyInfo.cs




This is one of the simplest (and powerful) ways to get started with code generation. Keep them mind next time creating multiple project that are similar in nature. There are still two more examples of code generation coming up.