使用CustomParameter与Visual Studio多项目模板

38

我想创建一个多项目模板,用于包含以下两个项目的解决方案(其中接口应该包括对业务层的引用):

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

通常,您可以包括$safeprojectname$,其中包含:

用户在“新建项目”对话框中提供的名称

但是,正如文章Build a multi-project visual studio template中指出的那样:

问题在于,在每个子模板中,$safeprojectname$代表当前项目名称。需要一种方法将根$safeprojectname$从父模板传递过来。

我正在尝试实现这个SO问题VS 2010 Multi-Project Template: Inter-Project References中建议的解决方案,使用CustomParameters,但遇到了问题。

我的压缩模板目录如下:

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

您可以下载整个目录,但这里是一些选择的片段

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>

问题

当我创建一个新项目时,字符串的$SolutionName$部分没有被替换。 它只是保持不变。

Q: 如何正确地将此信息从多项目模板传递到每个子模板?

如果您能找出如何替换<name>标记值,那么就可以获得额外的奖励积分,因为标记替换似乎无法在其上起作用。


从我的角度来看(鉴于在我的项目中所述的方法是可行的),VS不替换令牌的唯一原因可能是项目类型。遗憾的是我现在没多少时间来测试这个问题,但我建议你使用 Web 项目类型创建一个类似的模板,并检查是否仅为 Web 项目类型替换令牌... - IvanL
2
你是否考虑更改你接受的答案?原始答案仅托管在Codeplex上,该网站将于2017年关闭。 - friggle
1
作为目前被标记回答的作者,我支持将它更改为@friggle的回答。使用新的内置功能是一个更好的解决方案。 - Troy
1
@TroyPalacino,没错,说得好。感谢你的提醒。作为接受安慰奖励,我给你一些赏金点数。 - KyleMit
2个回答

82

Visual Studio 2013更新2终于添加了一种内置方法来使用ProjectTemplateLink

CopyParameters属性将启用子访问父模板的所有变量,前缀为ext_

甚至不需要使用CustomParameters。将你的ProjectTemplateLink更改为以下内容:

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

然后你可以像这样在子 .csproj 中实现你的目标:

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

你会如何使用模板重命名文件? - Alex Gordon
能否允许将自定义参数值传递给子模板? - Alex Gordon
@AlexGordon,我刚刚使用了自定义参数,$ext_mycustomparam$ 起作用了。 - Rhyous

3

完全披露:我是下面提到的项目的创建者。

我通过创建一个名为GlobalParamsIWizard实现解决了在多项目模板中共享项目模板信息的问题。它通过在所有解决方案级别参数前缀加上单词global并将其添加到子参数中,使得来自解决方案模板级别的信息可用于运行该向导的子模板。

如果您在上述模板中使用GlobalParams并且用户输入了“Test Project”,则以下内容为真:

  • InterfaceTemplate.vstemplate 可以访问
    • $safeprojectname$ = Test_Project.Interface
    • $globalsafeprojectname$ = Test_Project
  • BusinessTemplate.vstemplate 可以访问
    • $safeprojectname$ = Test_Project.Business
    • $globalsafeprojectname$ = Test_Project

以上是安全项目名称的示例,但所有解决方案模板中的参数都会传递给子模板。有关更多信息,请参见GlobalParams Documentation


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接