Example #1
0
        protected override void Initialize()
        {
            base.Initialize();
            FixNuGetReferencesCommand.Initialize(this);

            var componentModel = (IComponentModel)GetService(typeof(SComponentModel));

            var installerEvents = componentModel.GetService <IVsPackageInstallerEvents>();

            var dte = (DTE)ServiceProvider.GlobalProvider.GetService(typeof(DTE));

            // Order of events when installing a package:
            // 1. PackageInstalling
            // 2. PackageInstalled
            // 3. PackageReferenceAdded
            // Order of events when uninstalling a package:
            // 1. PackageUninstalling
            // 2. PackageReferenceRemoved
            // 3. PackageUninstalled

            var installFinished = Observable.FromEvent <VsPackageEventHandler, IVsPackageMetadata>(
                h => new VsPackageEventHandler(h),
                h => installerEvents.PackageReferenceAdded += h,
                h => installerEvents.PackageReferenceAdded -= h);

            _InstallSubscription = installFinished
                                   .Where(x => x.InstallPath.StartsWith(dte.GetSolutionDir()))
                                   .ObserveOn(SynchronizationContext.Current)
                                   .Subscribe(package =>
            {
                foreach (var project in dte.Solution.GetAllProjects())
                {
                    Logger.Info($"===== Fix paths for NuGet package {package.Id} in project {project.Name} ======");
                    try
                    {
                        NuGet.FixPackagePathsAndSaveProject(project, package, dte.GetSolutionDir());
                    }
                    catch (Exception ex)
                    {
                        Logger.Error($"Unexpected error: {ex}");
                    }
                }
            });

            var uninstallStarted = Observable.FromEvent <VsPackageEventHandler, IVsPackageMetadata>(
                h => new VsPackageEventHandler(h),
                h => installerEvents.PackageUninstalling += h,
                h => installerEvents.PackageUninstalling -= h);

            _UninstallSubscription = uninstallStarted
                                     .Where(x => x.InstallPath.StartsWith(dte.GetSolutionDir()))
                                     .ObserveOn(SynchronizationContext.Current)
                                     .Subscribe(package =>
            {
                foreach (var project in dte.Solution.GetAllProjects())
                {
                    Logger.Info($"===== Restore paths for NuGet package {package.Id} in project {project.Name} ======");
                    try
                    {
                        NuGet.RevertPackagePaths(project, package, dte.GetSolutionDir());
                    }
                    catch (Exception ex)
                    {
                        Logger.Error($"Unexpected error: {ex}");
                    }
                }
            });
        }
Example #2
0
 public static void Initialize(Package package)
 {
     Instance = new FixNuGetReferencesCommand(package);
 }