Bhavya

Code -> Break -> Fix -> Blog

VS Templates : Create Visual Studio Project Template with pre-installed NuGet Packages using VS SDK

6 Comments

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:

New Project Dialog

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.

File List

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.

AddedNewFilesToTemplate                     ModifiedCSProj

5. Also modify the .vstemplate to reflect the changes (Adding SampleModel.cs and SampleService.cs and Deleting Class1.cs entry)

VSTemplate

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

eg: http://nuget.org/api/v2/package/Newtonsoft.json/6.0.8

2. Add the .nupkg file to your project

NuGetAdditionToProject

3. Modify .vstemplate and add an entry for .nupkg file.

NuGetAdditionToVSTemplate

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.

WizardExtension

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.

NewProjectDialog

7. Click OK. And a new project will be created with the NuGet Package pre-installed.

NewProjectStructure

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.

VSIX Packages

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.

VSIXProjectSelectionDialog

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.

NewAssetDialog

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.

VSIXInstallerDialog

6. Open a new instance of Visual Studio and goto Tools->Extensions and Updates Dialog-box. Under Installed->Templates you can see the result.

ExtensionAndUpdatesDialog

7. You will also get the template which is part of the extension in the New Project dialog.

NewProjectDialog

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.

~BS

Twitter Logo

Advertisements

6 thoughts on “VS Templates : Create Visual Studio Project Template with pre-installed NuGet Packages using VS SDK

  1. Pingback: VS Templates : User to select NuGet packeges to be pre-installed for Project Template | Bhavya

  2. Excellent article. Thanks! This other article along with this one helped me create a multi-project solution template for my team. “How to: Create Multi-Project Templates” https://msdn.microsoft.com/en-us/library/ms185308.aspx

    Liked by 1 person

  3. Excellent.. It was very helpful for me.

    Like

  4. If the NuGet package to be pre-installed depends on other NuGet packages, all the .nupkg of the dependencies and their dependencies must be included in the VSIX right? I find this extremely inconvenient.

    Like

    • Yes – all the .nupkg needs to be available. Please elaborate the inconvenience.

      Like

      • Elaborating the inconvenience:

        1- Manually resolving the dependency tree to download each and evey NuGet dependency can take hours or days if the dependency tree is big. Why would I do that manually if NuGet already knows my dependency tree?
        2- The time it takes to creates a new project from a project template that contains embedded NuGet packages is too long. It’s better to move the download to build-time and not project creation time (because that’s expected to take longer).
        3- The VSIX is bigger and bigger for no reason. All what the VSIX needs to know theoretically is the package id and version of the dependency. The NuGet are available in the NuGet gallery and copying them is pointless.
        – The visual studio gallery will save so much wasted disk space if the same NuGet packages weren’t embedded in millions of VSIX but only their id and version instead.

        Does that sound inconvenient?

        Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s