Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 429
Philip Pittle Avatar asked Mar 21 '16 23:03

Philip Pittle


People also ask

How do I create a NuGet package in Visual Studio?

In Solution Explorer, right-click the project and choose Properties. In the Package tab, select Generate NuGet package on build. When you automatically generate the package, the time to pack increases the build time for your project.

What templates are available in Visual Studio?

Visual Studio templates. A number of predefined project and item templates are installed with Visual Studio. These templates, such as the ASP.NET Web Application and Class Library templates, are available to choose from when you create a new project.

What is the best way to build NuGet projects?

Unless you have a reason to choose otherwise, .NET Standard is the preferred target for NuGet packages, as it provides compatibility with the widest range of consuming projects. Right-click on the resulting project file and select Build to make sure the project was created properly.

Can I publish any NuGet package using the DotNet CLI?

Although you can publish any NuGet package using the nuget.exe CLI, some of the steps in this article are specific to SDK-style projects and the dotnet CLI. The nuget.exe CLI is used for non-SDK-style projects (typically .NET Framework).


2 Answers

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!

like image 89
lokusking Avatar answered Oct 21 '22 05:10

lokusking


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.

like image 24
Daniel Tran Avatar answered Oct 21 '22 05:10

Daniel Tran