Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add Same Solution Project References when exporting Templates in Visual Studio?

I have a solution that contains several projects, lets call them ProjectA and ProjectB. I need ProjectA to have a reference to ProjectB, however when I "Export Template" I can't seem to keep the reference. What I am trying to do is to have it create the reference for me when I create a new project and then automatically name it to what I entered.

like image 895
bkorzynski Avatar asked Jul 05 '12 21:07

bkorzynski


1 Answers

This is slightly out of scope for pure Visual Studio templates.

Visual Studio templates supports replacements parameters for templates, so what you could do is:

  • Inside your ProjectA.zip template file you will find ProjectA.csproj, which is the template for the project
  • In ProjectA.csproj you will find an <ItemGroup> containing <Reference> entries
  • Among them should be an entry like <Reference Include="ProjectB">

When you create a project from your ProjectB-template, it's name will be user-specified, so you want to replace ProjectB with a replacement parameter like $Foo$.

This is where it gets out of scope for pure templates: How do you tell visual studio to replace $Foo$? And what would the replacement be?

Your only choice here it to use a custom wizard, which either looks for the ProjectB-type project or asks the user for it.

Here's an outline for how to get the template and custom wizard to get along:

  • Create a VSIX package project.
  • Be sure to add the [ProvideBindingPath] attribute to your package. This is required to get the custom wizard registered (otherwise you would need to install it to the GAC, so VS can find it)
  • Create your wizard, it should implement Microsoft.VisualStudio.TemplateWizard.IWizard
  • Add your template to the solution and add it as a ProjectTemplate asset in your package's vsixmanifest

In the .vstemplate file of your template, add the following snippet below the <TemplateContent> block:

<WizardExtension>
  <Assembly>Your.Packages.Assembly</Assembly>
  <FullClassName>Your.Wizard</FullClassName>
</WizardExtension>

That will bring up your wizard when the project is created. You could, in the RunStarted method bring up a dialog which asks for the reference or you could use the automationObject parameter (which actually is a EnvDTE.DTE instance) to try and find the ProjectB project and supply it's name in the replacementsDictionary.

like image 143
M.Stramm Avatar answered Nov 03 '22 00:11

M.Stramm