Kim Johnson
Kim Johnson

Reputation: 1227

VS Project Template with Public NuGet Packages

The NuGet docs describe two possible repositories for package files defined within a VS template: 1) within the VSIX, or 2) within the template. There's also the third option of the registry for "installed" packages.

We have custom project templates which will use publicly available NuGet packages which are not already defined in the registry. It will bloat the VSIX and/or templates too much to include the nupkg files too. Is there some hidden option or trick to allow the repository to instead point to the package source(s) as defined by the Visual Studio user?

Upvotes: 3

Views: 1256

Answers (1)

Kim Johnson
Kim Johnson

Reputation: 1227

Since I didn't find any easy "trick", I wrote a custom project wizard per this discussion - http://nuget.codeplex.com/discussions/385955.

The package sources can be found via the PackageSourceProvider, which you can import via MEF along with the PackageInstaller. You then create a repository from the package sources, and pass it to the installer:

var factory = NuGet.PackageRepositoryFactory.Default;
var packageSources = PackageSourceProvider.LoadPackageSources().Select(s => s.Source).ToList();
var repository = new AggregateRepository(factory, packageSources, true);
NuGetPackageInstaller.InstallPackage(repository, project, package.Id, package.Version, false, false);

Calling InstallPackage directly also allows you to install dependent packages and assembly references, both of which are disabled by the standard NuGet VSIX package installer.

I'm guessing standard NuGet doesn't allow the dependencies to be installed for security reasons, but in our case we're using our own VSIX, our templates, and our NuGet packages, with a few additional known dependencies, so the license check has been made long before the template is actually used.

Upvotes: 2

Related Questions