简体   繁体   English

带有VSIX Installer和Nuget包的Visual Studio 2012的多项目/解决方案模板

[英]Multi Project / Solution Templates for Visual Studio 2012 with VSIX Installer and Nuget Packages

I would like to have a multi-project template that will create sub projects, and will install the nuget dependencies as well as have a vsix installer that will install this template. 我想有一个多项目模板,它将创建子项目,并将安装nuget依赖项以及安装此模板的vsix安装程序。

Problems with methods already tried 方法问题已经尝试过

Multi project template without VSIX, no nuget 没有VSIX的多项目模板,没有nuget

I was able to create a multi project template, and it worked fine, except when it came time to including nuget references, it did not work. 我能够创建一个多项目模板,它工作正常,除非是时候包括nuget引用,它不起作用。 There was no easy/elegant support for nuget that did not involve VSIX. 对于没有涉及VSIX的nuget,没有简单/优雅的支持。

Multi project template with VSIX, without a root template 具有VSIX的多项目模板,没有根模板

So I tried using VSIX, and it seemed very straightforward. 所以我尝试使用VSIX,看起来非常简单。 Add a template project as a zip or from solution, build the vsix installer. 将模板项目添加为zip或解决方案,构建vsix安装程序。 But when I added the second project template, to create a multi project template, built it, installed the template and tried to create a project I realised that it does not support a master-sub project setup, where a master project creates sub projects. 但是当我添加第二个项目模板,创建一个多项目模板,构建它,安装模板并尝试创建一个项目时,我意识到它不支持master-sub项目设置,其中一个主项目创建子项目。 This was a big problem because developers would have to create each project in dependency order. 这是一个很大的问题,因为开发人员必须按依赖顺序创建每个项目。

I finally have the setup working. 我终于让设置工作了。 It took me many days of experimenting and patching together different MSDN articles and blogs to figure this out. 我花了很多天的时间尝试和修补不同的MSDN文章和博客来解决这个问题。

Combining Multi project template (with root template) and VSIX 结合多项目模板(与根模板)和VSIX

The end result is 最终的结果是

  • Visual Studio 2012 VSIX project, which holds a packages folder that has all the nuget dependencies. Visual Studio 2012 VSIX项目,它包含具有所有nuget依赖项的packages文件夹。
  • A Zip file with the root/master template which links the multiple projects together that are also in the zip file in their corresponding folders. 带有根/主模板的Zip文件,它将多个项目链接在一起,这些项目也位于相应文件夹的zip文件中。
  • A built vsix installer which will install a single project template that has sub projects. 一个内置的vsix安装程序,它将安装一个包含子项目的项目模板。
  • This will allow you to maintain a template from a project that continues to grow, with very little effort, using the File-Export Template feature. 这将允许您使用文件 - 导出模板功能从项目中维护模板,该模板可以轻松地继续增长。
  • Uses auto generated and customizable parameters to rename your template namespaces and files to match your new project name. 使用自动生成和可自定义的参数重命名模板名称空间和文件以匹配新项目名称。

When creating a project with this template, it automatically creates multiple sub-projects, yet, the sub-projects are not (but can be) available as templates in the new project menu themselves. 使用此模板创建项目时,它会自动创建多个子项目,但子项目不是(但可以)作为新项目菜单中的模板。 This is great if you are trying to template something like an enterprise design that is dependent on many projects working together, but you don't want the user to be able to create these sub projects, or you want them to be available in a different project template area/category. 如果您尝试模拟依赖于许多项目一起工作的企业设计,但是您不希望用户能够创建这些子项目,或者您希望它们可以在不同的项目中使用,那么这非常有用。项目模板区域/类别。

Steps: 脚步:

  1. Create a folder “MyTemplate” 创建一个文件夹“MyTemplate”
  2. Create a root.vstemplate file 创建一个root.vstemplate文件
  3. Add the following to the file: 将以下内容添加到文件中:

     <WizardExtension> <Assembly>NuGet.VisualStudio.Interop, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</Assembly> <FullClassName>NuGet.VisualStudio.TemplateWizard</FullClassName> </WizardExtension> <WizardData> <packages repository="extension" repositoryId="fill this in"> <package id="Newtonsoft.Json" version="5.0.6" /> </packages> </WizardData> 
  4. Open your first project you wish to use as a “sub project” template, export template from File->Export Template. 打开您希望用作“子项目”模板的第一个项目,从文件 - >导出模板导出模板。

  5. Extract the zip contents to your MyTemplate folder, so you end up with MyTemplate/Project1 将zip内容解压缩到MyTemplate文件夹,最终得到MyTemplate / Project1
  6. Open up the Project1.vstemplate from what you just extracted. 从刚提取的内容中打开Project1.vstemplate。
  7. and paste in the following below the TemplateContent block (for example if you want to add a Json nuget package) 并在下面的TemplateContent块中粘贴(例如,如果你想添加一个Json nuget包)

     <WizardExtension> <Assembly>NuGet.VisualStudio.Interop, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</Assembly> <FullClassName>NuGet.VisualStudio.TemplateWizard</FullClassName> </WizardExtension> <WizardData> <packages repository="extension" repositoryId="fill this in"> <package id="Newtonsoft.Json" version="5.0.6" /> </packages> </WizardData> 
  8. Zip up your entire root folder MyTemplate, so you end up with MyTemplate.zip which has the contents of the MyTemplate folder. 压缩整个根文件夹MyTemplate,最终得到MyTemplate.zip,其中包含MyTemplate文件夹的内容。

  9. Create a VSIX project (you will need Visual Studio 2012 SDK to have this template) from the Extensibility template category under C#. 从C#下的可扩展性模板类别创建一个VSIX项目(您需要Visual Studio 2012 SDK才能拥有此模板)。
  10. Create a packages folder 创建一个包文件夹
  11. Add the nuget package (.nupkg file only) you referenced in your sub projects vstemplate to the packages folder and in the files properties, set its build action to content, and Include in VSIX to true. 将您在子项目vstemplate中引用的nuget包(仅限.nupkg文件)添加到packages文件夹和files属性中,将其构建操作设置为content,并将VSIX中包含为true。 In my case I added Newtonsoft.Json.5.0.6.nupkg. 在我的情况下,我添加了Newtonsoft.Json.5.0.6.nupkg。
  12. In the source.extension.vsixmanifest set the Author name 在source.extension.vsixmanifest中设置作者名称
  13. In the Assets tab, click new and select project template from the drop down, then file on file-system in the next drop down. 在Assets选项卡中,单击new并从下拉列表中选择项目模板,然后在下一个下拉列表中文件系统上的文件。
  14. Browse and select the zip you created, hit Ok. 浏览并选择您创建的zip,点击确定。 This will create a ProjectTemplates Folder with the zip. 这将创建一个带有zip的ProjectTemplates文件夹。
  15. Copy the Product Id from the top of the source.extension.vsixmanifest. 从source.extension.vsixmanifest的顶部复制Product Id。
  16. Open up the zip with a program like WinRar that allows you to save changes directly to the zip (the default windows zip app does not allow this). 使用像WinRar这样的程序打开zip,允许您直接将更改保存到zip(默认的Windows zip应用程序不允许这样做)。
  17. Paste in the Product id into the repositoryid section 将Product id粘贴到repositoryid部分
  18. Save the changes to the zip file (make sure you go back to WinRar if thats what you are using, as it will have detected the changes and will ask you if you wish to update) 将更改保存到zip文件(确保你回到WinRar,如果你正在使用它,因为它会检测到更改,并会询问你是否要更新)
  19. Build the solution and go to the bin/debug folder to run the vsix installer 构建解决方案并转到bin / debug文件夹以运行vsix安装程序
  20. Create your project 创建您的项目

Optional Step to have your master project rename your files and namespaces 可选步骤让您的主项目重命名您的文件和名称空间

  • Open each sub project vstemplate file, and add $safeprojectname$ to the DefaultName Element. 打开每个子项目vstemplate文件,并将$ safeprojectname $添加到DefaultName元素。 eg 例如
<DefaultName>$safeprojectname$.Domain</DefaultName>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM