Extending NuGet VS Extension

Mar 2, 2012 at 6:50 PM

Hi,

What I'd like to do is run a small action each time a NuGet package is downloaded, is it possible to hook into the existing VS extension?

My goal is to be able to transmit a key value pair of { project name, packageId-version }  to an API so I can track which of my projects are consuming which packages.

Developer
Mar 2, 2012 at 7:23 PM

There are events off of HttpClient that you could hook into. You're probably better off hooking into events such as PackageInstalled hanging off the VsPackageManager. We export this type as a MEF component so you should be able to use VS's ComponentModel to grab an instance.

Mar 4, 2012 at 12:27 AM
Edited Mar 4, 2012 at 12:29 AM
pranavkm wrote:

We export this type as a MEF component so you should be able to use VS's ComponentModel to grab an instance.


Thanks!

As far as loading the Nuget.PackageManager from VS, could you elaborate. In googling I came across this - http://nuget.codeplex.com/discussions/246688

which produced the code

var componentModel = (IComponentModel)Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(SComponentModel)); 
var pm = componentModel.GetService<NuGet.PackageManager>();
But I'm getting a ImportCardinalityMismatchException when I debug my add-in
on the .GetService execution
Developer
Mar 5, 2012 at 4:38 PM

Looking at our code base, I noticed that we don't ever expose the VsPackageManager instance via MEF. We expose a factory for it, but the dialog and the commands wouldn't be using any instance you create.

Mar 6, 2012 at 10:48 PM

So the only option left would be via HttpClient, to listen for outgoing messages with some install-package message?

Developer
Mar 6, 2012 at 11:13 PM

The problem with the HttpClient is it's hard to tell exactly when it's being used to install a package. You could certainly sniff out our calls to a server meant specifically for requesting files but we cache packages locally and reuse them if we know the cache isn't stale.