In this article, I will talk about creating a VS Project Template with pre-installed Nugets and deploying it through VSIX package.
I will start with creating a Visual Studio Project Template. Next will add Nugets to it and at the end with the help of VSIX will deploy the template.
Create VS Project Template using Visual Studio SDK
Download the Visual Studio SDK from here. VS SDK contains templates for creating Item/Project templates as seen below:
1. Select C# Project Template from the “Add New Project” dialog. Give appropriate name to your project and Click OK.
2. Generated project will contain a .vstemplate, a .csproj, a .class and an AssemblyInfo.cs file. Of these .vstemplate file, .csproj file and AssemblyInfo.cs files are mandatory. The developer should not delete them but he/she can modify these files.
3. Add new files or modify existing files as per your requirement. Use parameter substitutions where applicable in the newly added files. For more information about Parameter Substitutions see here.
4. Make sure to modify .csproj so that it contains the newly added files or remove the deleted files.
In the figure below, I have removed Class.cs and added 2 new files SampleModel.cs and SampleService.cs. And modified .csproj accordingly.
5. Also modify the .vstemplate to reflect the changes (Adding SampleModel.cs and SampleService.cs and Deleting Class1.cs entry)
6. Update the Build Action property of each newly added file to None.
7. If required, modify the .ico file to set a new icon for the template.
8. Build the Project, go to /bin folder (debug/release) to get the generated compressed file which will contains the project template.
9. Copy the compressed file (.zip) to the local ProjectTemplate folder from where Visual Studio will pick up the custom templates.
Project Template with Pre-Installed Nuget Package(s)
In the previous step, we have created a ProjectTemplate. Now let’s modify the template so that it has pre-installed NuGet package(s).
1. Download the NuGet package(s) file .nupkg from nuget.org using the format http://nuget.org/api/v2/package/PACKAGE_ID/VERSION
2. Add the .nupkg file to your project
3. Modify .vstemplate and add an entry for .nupkg file.
4. Add a reference of the NuGet template Wizard to .vstemplate by adding WizardExtension element. And also the list of NuGet packages that needs to be installed using WizardData element. repository attribute of packages element can have two values template or extension. For now we will use template, I will cover extension later in the series. repository=”template” will look for .nupkg file in the created template (.zip file). You can also add more than one package element under packages element to install more NuGet packages.
5. Build the project and copy the .zip file to the VS template location.
6. Open new instance of Visual Studio and select “New Project”. Select the custom template you created.
7. Click OK. And a new project will be created with the NuGet Package pre-installed.
In this approach the developer who will be using the template will have to explicitly update the package and the template will only install the version that is part of it.
Recommended way of deploying the custom project template is through a VSIX package. Because with Visual Studio Extension Manager the end-user can easily get your custom project template, also VS Extension Manager helps push your template update too.
1. Add a new project to the already created Solution above. And select VSIX Project template. Give appropriate name to the project and Click OK.
2. Add the already created Project Template to VSIX. So open source.extension.vsixmanifest file. Goto Assets tab and Click New button to add a new asset. Select Type as Microsoft.VisualStudio.ProjectTemplate and select your Project.
3. Make changes in the Metadata tab if required. Like giving proper description, icon, license details etc.
4. Build the VSIX project to get the output i.e. .vsix file in the output directory.
5. Double-Click on the .vsix file to install the extension.
6. Open a new instance of Visual Studio and goto Tools->Extensions and Updates Dialog-box. Under Installed->Templates you can see the result.
7. You will also get the template which is part of the extension in the New Project dialog.
In the next blog, I will talk about giving the end-user an option to select the NuGet package(s) that they want to pre-install from a list. Also I will talk about how to create solution template.
Let me know your thoughts.