Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
359 views
in Technique[技术] by (71.8m points)

Using CustomParameter with Visual Studio Multi-Project Template

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">
        InterfaceInterfaceTemplate.vstemplate
      </ProjectTemplateLink>
      <ProjectTemplateLink ProjectName="$safeprojectname$.Business">
        BusinessBusinessTemplate.vstemplate
      </ProjectTemplateLink>
    </ProjectCollection>
  </TemplateContent>
</VSTemplate>

InterfaceInterfaceTemplate.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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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">
  InterfaceInterfaceTemplate.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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...