Using CustomParameter with Visual Studio Multi-Project Template
Asked Answered
W

2

38

I would like to be able to create a Multi-Project Template for a solution which contains the following two projects (where the interface should include a reference to the business layer).:

  • <proj_name>.interface
  • <proj_name>.business.

Normally, you can include $safeprojectname$, which has:

The name provided by the user in the New Project dialog box

However, as pointed out in the article Build a multi-project visual studio template

The problem here is that within each child template, $safeprojectname$ represents the current project name. There needs to be a way to pass in the root $safeprojectname$ from the parent template.

I'm trying to implement the solution suggested in this SO question VS 2010 Multi-Project Template: Inter-Project References, by using CustomParameters, but am running into trouble.

My Zipped Up Template Directory looks like this:

  • MultiTemplate.vstemplate
  • Interface
    • InterfaceTemplate.vstemplate
    • MyTemplate.Interface.csproj
  • Business
    • BusinessTemplate.vstemplate
    • MyTemplate.Business.csproj

You can download the Entire Directory, but here are some select snippets

MultiTemplate.vstemplate

<VSTemplate Version="3.0.0" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" Type="ProjectGroup">
  <TemplateData><!--Removed for brevity --></TemplateData>
  <TemplateContent>
    <CustomParameters>
      <CustomParameter Name="$SolutionName$" Value="$safeprojectname$"/>
    </CustomParameters>
    <ProjectCollection>
      <ProjectTemplateLink ProjectName="$safeprojectname$.Interface">
        Interface\InterfaceTemplate.vstemplate
      </ProjectTemplateLink>
      <ProjectTemplateLink ProjectName="$safeprojectname$.Business">
        Business\BusinessTemplate.vstemplate
      </ProjectTemplateLink>
    </ProjectCollection>
  </TemplateContent>
</VSTemplate>

Interface\InterfaceTemplate.vstemplate

<VSTemplate Version="3.0.0" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" Type="Project">
  <TemplateData><!--Removed for brevity --></TemplateData>
  <TemplateContent>
    <Project TargetFileName="MyTemplate.Interface.csproj" 
             File="MyTemplate.Interface.csproj"
             ReplaceParameters="true">
    </Project>
  </TemplateContent>
</VSTemplate>

MyTemplate.Interface.csproj

<ItemGroup>
  <ProjectReference Include="..\$SolutionName$.Business\$SolutionName$.Business.csproj">
    <Project>{E5511F75-5B9C-4816-B991-E09225661CF4}</Project>
    <Name>MyTemplate.Business</Name>
  </ProjectReference>
</ItemGroup>

The Problem

When I create a new project, the $SolutionName$ portion of the string does not get replaced. Instead it just stays the same.

Q: How can I properly pass this information from the Multi-Project Template to each of the child templates?

Bonus points if you can figure out how to replace the <name> tag value, as token replacements don't seem to work on it.

Winshell answered 29/10, 2013 at 15:40 Comment(4)
From my point of view (seeing that for my project the described method is functional) the only reason for VS not replacing the tokens may be the project type. Sadly I have little time right now to test this but I advise you to make a similar template with a Web project type and check if the tokens are replaced only for the Web project type...Opalopalesce
Would you consider changing your accepted answer? The original answer is hosted only on Codeplex, which will be shutting down later in 2017.Peirsen
As the author of the currently marked answer, i support changing it to the answer by @friggle. Using the new built in functionality is a better solution.Allanite
@TroyPalacino, yep, good call. Thanks for the nudge. Have some bounty points as an accept consolation prizeWinshell
P
82

Visual Studio 2013 Update 2 finally added a built-in way to do this with ProjectTemplateLink

The CopyParameters attribute will enable child access to all the variables of the parent template, with the prefix of ext_.

You don't even need CustomParameters for this. Change your ProjectTemplateLink to this:

<ProjectTemplateLink ProjectName="$safeprojectname$.Interface" CopyParameters="true">
  Interface\InterfaceTemplate.vstemplate
</ProjectTemplateLink>

And then you can achieve your goal in the child .csproj like so:

<ItemGroup>
  <ProjectReference Include="..\$ext_safeprojectname$.Business\$ext_safeprojectname$.Business.csproj">
    <Project>{E5511F75-5B9C-4816-B991-E09225661CF4}</Project>
    <Name>MyTemplate.Business</Name>
  </ProjectReference>
</ItemGroup>
Peirsen answered 4/6, 2014 at 17:5 Comment(3)
how would you rename files using templates?Tobacco
is it possible to allow cusotm parameters values to be passed down to child templates?Tobacco
@AlexGordon I just used custom parametrs and the $ext_mycustomparam$ worked.Absorber
A
3

Full Disclosure: I am the creator of the below mentioned project.

I solved the issue of sharing information between project templates in a multi-project template be creating an IWizard implementation called GlobalParams. It makes the information from the solution template level available to the child templates that also run the wizard by prefixing ALL solution level parameters with the word global and adding it to the child parameters.

If you used GlobalParams with the above template and the user entered "Test Project", the following would be true:

  • InterfaceTemplate.vstemplate could access
    • $safeprojectname$ = Test_Project.Interface
    • $globalsafeprojectname$ = Test_Project
  • BusinessTemplate.vstemplate could access
    • $safeprojectname$ = Test_Project.Business
    • $globalsafeprojectname$ = Test_Project

The above is an example of the safe project name but all parameters from the solution template are passed to child template See the GlobalParams Documentation for more information.

Allanite answered 20/11, 2013 at 17:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.