Create a Visual Studio Project Template that pulls NuGet references from online feed
Asked Answered
C

2

11

I'm creating a Visual Studio Project Template and bundling it inside of a VS Extension. I need Projects created from the Template to reference ~20 NuGet packages.

Is it possible to have the references resolved from nuget.org rather than having to include all of the references inside the VSIX?

The NuGet documentation on Visual Studio Templates provides instructions on how to add packages inside the VSIX, but it requires the file be stored locally on disk and the .nupkg is bundles inside the vsix:

Add your nupkg files as custom extension content in your source.extension.vsixmanifest file. If you're using the 2.0 schema it should look like this:

<Asset Type="Moq.4.0.10827.nupkg" d:Source="File" 
    Path="Packages\Moq.4.0.10827.nupkg" d:VsixSubPath="Packages" />

Question already asked

I know a similar question was asked (Creating a Visual Studio Project Template that already includes a Nuget Package Reference?) and answered (not possible), but this was asked in 2011.

5 years later, is it still not possible?

Costard answered 21/3, 2016 at 23:38 Comment(6)
I am doing two templates right now. One uses NPM and one uses NuGet. The NuGet one is so much harder... I may bag that one and just try to get by with just the NPM based one.Hagood
Why not script the NuGet installation of the 20 packages and paste that in a Readme.Txt ?Ubana
@GerardoGrignoli - feels hackish. Plus there will be those that don't figure it out or have problems getting the extra step to work.Hagood
@Hagood Im about to agree with Gerardo. I assume you have made a wizzard. In the RunFinished-Method you could manually install/upgrade your packages. That isnt really hackish, since there is no other solution.Validate
@Validate - Hmmm, I did not know that could be done in code. I assumed @Gerardo was saying to leave it up to the consumer of the template. If it can be automatically done by the template (in a RunFinished) then that is really all I am going for. But I don't know of a way to programatically do a restore of nuget packages....Hagood
@Hagood - Thx for the bounty. I was never able to get a solution to this, I manually added every NuGet package I need to the template. If I recall correctly, NuGet is not invoked by the VS Project Template engine so any dependencies of a NuGet will NOT be auto-added.Costard
V
7

Since there is still no Built-In functionality to Install/Upgrade packages from online Repo, here is a small workaround wich might help:

Prerequisites

First, install the NuGet.VisualStudio nuget package into your project. You get that from here

When installed, the package will automatically set the Embed Interop Types property of the assembly reference to True. The reason it does so is to make your code resilient against version changes when users update to newer versions of NuGet.

For the same reason, you must NOT use any other types besides the above interfaces in your code. You must NOT reference any other NuGet assemblies either, including NuGet.Core.dll.

After setting up all that stuff, you can do the following in your RunFinished-Method:

var componentModel = (IComponentModel) Package.GetGlobalService(typeof(SComponentModel));
IVsPackageInstallerServices installerServices = 
     componentModel.GetService<IVsPackageInstallerServices>();

if (!installerServices.IsPackageInstalled(project, "Newtonsoft.Json")) {
     var installer = componentModel.GetService<IVsPackageInstaller>();
     installer.InstallPackage(
         "All", 
         project, 
         "Newtonsoft.Json", 
         (System.Version) null, 
         false);
}

Note

That example shows based on Newtonsoft.Json how you can install a package. For sure you can choose the projects targeting the installation. Also you can determine the Version to be installed.

It seems a bit uncomfortable, but unfortunately there is no other way around.

Usings

using Microsoft.VisualStudio.ComponentModelHost;
using Microsoft.VisualStudio.Shell;
using NuGet.VisualStudio;

Let me know if that helps!

Validate answered 25/8, 2016 at 9:28 Comment(0)
B
1

Yes, you can create a nuget package and add those other packages as its dependencies. Then when you download that package it will get all its dependencies and add to your project.

Byzantium answered 23/8, 2016 at 0:56 Comment(4)
I am looking to create a visual studio project template, not a NuGet package.Hagood
Yes, I understand, you create the template and add that package you just created to that template.Byzantium
Sorry, your problem is you can't add a nuget package to the created template. My mistake. Let me try to find out.Byzantium
@Daniel you can add a NuGet package to the template, but you need to manually download the .nupkg and added it as a resource to the project template. NuGet doesn't auto download packages when the VS Project Template is used.Costard

© 2022 - 2024 — McMap. All rights reserved.