Example #1
0
        private void MenuItemCallback(object sender, EventArgs e)
        {
            var componentModel    = (IComponentModel)ServiceProvider.GetService(typeof(SComponentModel));
            var installerServices = componentModel.GetService <IVsPackageInstallerServices>();

            var allPackages = installerServices.GetInstalledPackages().ToList();
            var packages    = allPackages.Where(p => !string.IsNullOrEmpty(p.InstallPath)).ToList();

            foreach (var p in allPackages.Except(packages))
            {
                Logger.Error($"Can't fix paths for {p.Id} because the install path is empty.");
            }

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

            foreach (var project in dte.Solution.GetAllProjects())
            {
                Logger.Info($"===== Fix paths for all NuGet packages in project {project.Name} ======");
                try
                {
                    NuGet.FixPackagePathsAndSaveProject(project, packages, dte.GetSolutionDir());
                }
                catch (Exception ex)
                {
                    Logger.Error($"Unexpected error: {ex}");
                }
            }
        }
Example #2
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}");
                    }
                }
            });
        }