One of the items that I had been struggling with was the ability to create a new project that did some pretty specific items when it was “unfolded” as they say in the GAT world.  I wanted to use a custom recipe instead of a template link.  After researching around a little with one of my favorite tools reflector, I wrote out some basic code for an action that creates a project.  Please use as you see fit.

 

public class CreateNewProject: Action

    {

        private const string PATH_SEPARATOR = @”\”;

 

        private string _ProjectName;

        [Input]

        public string ProjectName

        {

            get { return _ProjectName; }

            set

            {

                _ProjectName = value;

            }

        }

 

        private string _ProjectNamespace;

        [Input]

        public string ProjectNamespace

        {

            get

            {

                return _ProjectNamespace;

            }

            set

            {

                _ProjectNamespace = value;

            }

        }

 

        private Project _VSProject;

        [Output]

        public Project VSProject

        {

            get { return _VSProject; }

            set { _VSProject = value; }

        }

 

        private string _Template;

 

        public string Template

        {

            get { return _Template; }

            set

            {

                _Template = value;

            }

        }

 

        private string GetTemplateDirectory()

        {

            string tempString = ReflectionHelper.GetAssemblyPath(Assembly.GetExecutingAssembly());

            return tempString.Substring(0,tempString.LastIndexOf(PATH_SEPARATOR)) + PATH_SEPARATOR + @”Templates” ;

        }

        public override void Execute()

        {

            Template = @”Projects\BaseProject\Project.vstemplate”;

            string templateFile = GetTemplateDirectory() + PATH_SEPARATOR + Template;

            string destination = GetSolutionDirectory() + PATH_SEPARATOR + @”\src” ;

 

                base.GetService<DTE>(true).Solution.AddFromTemplate(templateFile, destination, ProjectNamespace + “.” + ProjectName, false);

 

        }

 

        private string GetSolutionDirectory()

        {

            string temporaryPath = (string)base.GetService<DTE>(true).Solution.Properties.Item(“Path”).Value;

            temporaryPath = temporaryPath.Substring(0, temporaryPath.LastIndexOf(PATH_SEPARATOR));

            return temporaryPath;           

        }

 

 

Items in the code to note:

  1. When a GAT package is deployed, it contains a dll and the Templates directory folder relative to the dll.  This way I can use the ReflectionHelper class, located inside the RecipeFramework to get the assembly path for the current assembly.
  2. Notice also that I have hard coded the Template property in the execute method.  This was just for testing purposes.  will change to become an input to the action.

 

If anyone wants the full source of this with tests and comments, let me know.

 

Advertisements