Question

I am creating a custom VSTemplate for MVC 4 applications for my company that uses a wizard that is comparable to the wizard that appears when you create a new MVC4 application. I have one of two templates I would like to apply when the developer creates a new app of this type as shown here:

VS Project Wizard

Both of those entries correspond to templates that are defined inside my VSIX project under a folder called ProjectTemplates:

Solution Explorer

My question is, how do I apply the correct template when the wizard runs? I know how to create a vstemplate with multiple projects (using the ProjectCollection node in the vstemplate), but that's not really what I want to do since they will never be deployed together. I see that I can add both vstemplates as Assets to my vsixmanifest file, but I'm not really sure how to apply just one template conditionally.

Thanks!

Was it helpful?

Solution

You'll need to include the files for your "optional" template(s) in sub directories of the the "root" template folder but EXCLUDE them from the TemplateContent Element of the "root" template.

Your IWizard implementation needs to keep a reference to the EnvDTE.DTE object (the first parameter of RunStarted) and use it in the ProjectFinishedGenerating to add the projects to the solution using the template(s) that match what the user selected.

public class SelectTemplatesWizard : IWizard
{
    private EnvDTE.DTE _dte = null;

    private string _solutionDir = null;

    private string _templateDir = null;

    public void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams)
    {
        // Store the reference to the environment for later use
        _dte = automationObject as EnvDTE.DTE;

        /*
          The value of the item in the replacements dictionary for the key 
          $destinationdirectory$ is populated with the physical location of
          the directory (named based on the user entered project name) created
          sibling to the solution file.

          The solution directory will be this directories parent 
          when the Type attribute of the VSTemplate element is ProjectGroup
        */
        _solutionDir = System.IO.Path.GetDirectoryName(replacementsDictionary["$destinationdirectory$"]);

        // customParams[0] is a default custom param that contains the physical location of the template that is currently being applied
        _templateDir = System.IO.Path.GetDirectoryName(customParams[0] as string);
    }

    public void ProjectFinishedGenerating(Project project)
    {
        int userSelected = 1;
        string name= null, projectPath= null, templatePath = null;

        switch (userSelected)
        {
            case 0:
                {
                    name = "Angular";
                    projectPath = System.IO.Path.Combine(_solutionDir, "Angular");
                    templatePath = System.IO.Path.Combine(_templateDir , "Angular\Angular.vstemplate");
                }
                break;
            case 1:
                {
                    name = "MVC4";
                    projectPath = System.IO.Path.Combine(_solutionDir, "MVC4");
                    templatePath = System.IO.Path.Combine(_templateDir , "MVC4\MVC4.vstemplate");
                }
                break;
        }
        _dte.Solution.AddFromTemplate(templatePath, projectPath, name);
    }

    /* Other IWizard methods excluded for brevity */
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top