This repository demonstrates how to build a VSIX to include a template engine template pack for use in Visual Studio 16.8 and later.
This which includes building the template package, the stub vstemplate file and the VSIX
msbuild.exe TemplateSample.sln /t:Restore;Build
The folllowing sections describe the steps needed to create an installable VSIX for installation into Visual Studio 2019 16.3 or later.
The first step is to create the .NET Core Template Engine template and package it into a nupkg for deployment. You can read more details about template creation here.
Notable pieces of information in the sample template package which will be needed for later steps are the groupIdentity
and the language
and type
tags:
"tags": {
"language": "C#",
"type": "project"
},
"groupIdentity": "MyCompany.Common.Console",
With the generated nupkg, you could now install this template pack into the command line .NET Core CLI via dotnet new -i template.nupkg
and invoke it via dotnet new MyCompany.Common.Console
.
Another thing to note is that the nupkg file structure is important. You can have one or more templates in a template pack, but they need to be in indidual of a folder named "content" at the package root.
The last step is to create the VSIX installer
In this sample, we add a project reference to the vstemplate project previously created. Note, that in newer versions (2022 and later) of Visual Studio, you can skip creating the vstemplate and adding it to the VSIX, as Visual Studio will automatically discover templates packs installed. However, if you wish to continue to invoke through a vstemplate or use a custom wizard, you need to add the same metadata to an ide.host.json as shown here. This will cause Visual Studio to still install the template so it can be invoked from a vstemplate file, but won't list the template itself - only the vstemplate will be shown.
{
"unsupportedHosts": [
{ "id": "vs" }
]
}
We need to add the generated nupkg to the VSIX. In order to do this, we need to update the VSIX project file and manually add the following target. Note that this step is needed even if the vstemplate(s) are removed from the project. This will cause the nupkg to be pckaged in a ProjectTemplates subfolder under our package installation path.
<Target Name="PreCreateVsixContainer" BeforeTargets="GetVsixSourceItems">
<ItemGroup>
<_TemplatePackage Include="..\templatepackages\TemplatePackage\bin\$(Configuration)\MyCompany.SampleTemplates.*.nupkg" />
</ItemGroup>
<Error Text="No template files found." Condition="@(_TemplatePackage->Count()) == 0" />
<ItemGroup>
<VSIXSourceItem Include="@(_TemplatePackage)">
<VSIXSubPath>ProjectTemplates\</VSIXSubPath>
</VSIXSourceItem>
</ItemGroup>
</Target>
Additionally, we need to add the following pkgdef to the VSIX which tells the template discovery where to find the template nupkg.
[$RootKey$\TemplateEngine\Templates\MyCompany.SampleTemplates\1.0.0]
"InstalledPath"="$PackageFolder$\ProjectTemplates"
Also add the following line to the source.extensions.vsixmanifest:
<Asset Type="Microsoft.VisualStudio.VsPackage" d:Source="File" Path="Templates.pkgdef" />