bgeveritt
bgeveritt

Reputation: 303

Visual Studio Extension - Event after NuGet install/uninstall?

I'm developing a Visual Studio extension, and I'd like to execute some logic after a NuGet install or uninstall. Is there an event that I can monitor for this? I've tried the OnItemAdded and OnItemDeleted in the IVsHierarchy interface, but the issue here is that it will execute numerous times per install/uninstall due to the number of files that are being added or removed to the solution. I'd like to trigger the event after the NuGet process is complete.

Upvotes: 0

Views: 472

Answers (2)

Aashish Jain
Aashish Jain

Reputation: 46

It could be as simple as below code. Just make sure to use https://dotnet.myget.org/F/nuget-build/api/v3/index.json as Nuget Feed to get NuGet.VisualStudio latest package (v4.0.0-rc3-2137).

    [Import]
    private IVsPackageInstallerEvents packageInstallerEvents;

    [Import]
    private IVsPackageInstallerProjectEvents packageInstallerProjectEvents;

    private string currentBatchId;

    private Dictionary<string, string> packagesMetadata;

    public void BindNuGetPackageEvents()
    {
        packageInstallerProjectEvents.BatchStart += (projectMetadata) =>
        {
            // preserve current batch id or project name to compare with batch end event
            currentBatchId = projectMetadata.BatchId;
            Console.WriteLine("Current Project Name : " + projectMetadata.ProjectName);
        };

        packageInstallerEvents.PackageInstalled += (metadata) =>
        {
            // package being insalled in current project
            // Save package metadata to use at batch end event
            packagesMetadata.Add(metadata.Id, "installed");
        };

        packageInstallerEvents.PackageUninstalled += (metadata) =>
        {
            // package being uninstalled in current project
            // Save package metadata to use at batch end event
            packagesMetadata.Add(metadata.Id, "uninstalled");
        };

        packageInstallerProjectEvents.BatchEnd += (projectMetadata) =>
        {
            if (currentBatchId == projectMetadata.BatchId)
            {
                // Now you can use your packages metadata saved during packageinstalled or packageuninstalled events
                foreach (var packageName in packagesMetadata.Keys)
                {
                    Console.WriteLine(string.Format("Package {0} was {1}", packageName, packagesMetadata[packageName]));
                }
            }
        };
    }

Upvotes: 2

Aashish Jain
Aashish Jain

Reputation: 46

You can use NuGet API in Visual Studio and listen to NuGet events like PackageInstalling or PackageInstalled. But make sure you use these events in batch mode and also listens to BatchStart and BatchEnd from NuGet IVsPackageInstallerProjectEvents interface so that it doesn't degrade NuGet performance.

You can find more details about these APIs here at NuGet blogpost.

So ideally when you receive BatchStart event, you will hold on to execute IVsPackageInstallerEvents events like PackageInstalling, PackageInstalled, etc and apply these after you receive BatchEnd event.

Upvotes: 1

Related Questions