visual-studiovsixvs-extensibilityvisual-studio-templatesvsixmanifest

Is there a way to add conditions in vsTemplates? or a way to generate output projects based on user selections


I have a requirement to generate a custom projects template. I am showing the wizard (using vsix project) to the user which has three check boxes in which user can select one or many of them, based on the selection the number of projects need to be created.

Suppose user choose one, then output solution must have one project. Suppose user choose any two, then output solution must have those two projects.

At Present irrespective of selection three output projects are created. The root vsTemplate file looks like below, Is there a way achieve my requirement?

<ProjectCollection>
      <ProjectTemplateLink ProjectName="Cat">
        CatProject\Cat.vstemplate
      </ProjectTemplateLink>
      <ProjectTemplateLink ProjectName="Dog">
        DogProject\Dog.vstemplate
      </ProjectTemplateLink>
      <ProjectTemplateLink ProjectName="Lion">
        LionProject\Lion.vstemplate
      </ProjectTemplateLink>
</ProjectCollection>

I tried two ways of doing it 1)At run time changing the root vsTemplate file accordingly , but always output project is created based on the vsTemplate file which is already in zip folder in bin directory. 2)using Development tool environment automation object , adding or removing projects programatically in ProjectItemFinished method of IWizard interface ,Example: Dte.Solution.AddFromTemplate( vsTemplatePath, NewProjectName) The above line creates a new project but folder structure is missed, and it is difficult to tweak it.

Is there any easy way to do like this?

<ProjectCollection>
      if(cat is selected)
      <ProjectTemplateLink ProjectName="Cat">
        CatProject\Cat.vstemplate
       </ProjectTemplateLink>
        end if
      if(Dog is selected)
      <ProjectTemplateLink ProjectName="Dog">
        DogProject\Dog.vstemplate
      </ProjectTemplateLink>
      end if
      if(Lion is selected)
      <ProjectTemplateLink ProjectName="Lion">
        LionProject\Lion.vstemplate
      </ProjectTemplateLink>
      end if
</ProjectCollection>

Solution

  • I made it work using the second approach I tried. Here is the complete solution. Remember when we add new vstemplate file to the project build action type must be "vstemplate" , to tell soln.GetProjectTemplate to look for vstemplate type of file, otherwise it may throw an error.

    In this way we can conditionally create projects.

    enter image description here

     using EnvDTE;
      using EnvDTE80;  
    
      DTE dte;
    
    public void ProjectFinishedGenerating(Project project)
            {
                //Accessing DTE(Development tool environment) object should only be done on main thread, otherwise throw exception
                ThreadHelper.ThrowIfNotOnUIThread();
                Solution2 soln = (Solution2)dte.Solution;
                if (firstForm.IsCatSelected)
                {                
                    dte.Solution.AddFromTemplate(soln.GetProjectTemplate("Cat.vstemplate", "CSharp"), destinationDirectory + "\\Cat", "Cat");               
                }
                if (firstForm.IsDogSelected)
                {
                    dte.Solution.AddFromTemplate(soln.GetProjectTemplate("Dog.vstemplate", "CSharp"), destinationDirectory + "\\Dog", "Dog");
                }
            }
    
    public void RunStarted(object automationObject,
                Dictionary<string, string> replacementsDictionary,
                WizardRunKind runKind, object[] customParams)
            {
                //Accessing DTE(Development tool environment) object should only be done on main thread, otherwise throw exception
                ThreadHelper.ThrowIfNotOnUIThread();
                destinationDirectory = replacementsDictionary["$destinationdirectory$"];
    
                dte = automationObject as DTE;
              }