private static async Task PromptToInstallAsync(IEnumerable <Project> projects)
        {
            await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

            string msg = $"One or more VSIX projects in this solution aren't using the Microsoft.VisualStudio.SDK.Analyzers NuGet package.\r\rDo you wish to install it now?";

            MessageBoxResult answer = MessageBox.Show(msg, Vsix.Name, MessageBoxButton.OKCancel, MessageBoxImage.Question);

            if (answer == MessageBoxResult.OK)
            {
                _dte.StatusBar.Text = $"Installing {_nugetId}...";
                IVsPackageInstaller2 installer = _componentModel.GetService <IVsPackageInstaller2>();

                try
                {
                    foreach (Project project in projects)
                    {
                        installer.InstallLatestPackage(null, project, _nugetId, false, false);
                    }
                }
                catch (Exception ex)
                {
                    _dte.StatusBar.Text = ex.Message;
                }

                _dte.StatusBar.Text = $"VSSDK Analyzers installed successfully";
            }
        }
Example #2
0
        /// <inheritdoc/>
        public override void Execute(Project project)
        {
            var componentModel = (IComponentModel)this.Package.GetService(typeof(SComponentModel));
            IVsPackageInstaller2 packageInstaller = componentModel.GetService <IVsPackageInstaller2>();

            packageInstaller.InstallLatestPackage(null, project, SlowCheetahNuGetManager.PackageName, false, false);

            this.Successor.Execute(project);
        }
        public PackageServices()
        {
            var componentModel = (IComponentModel)Package.GetGlobalService(typeof(SComponentModel));

            _PkgInstaller   = componentModel.GetService <IVsPackageInstaller2>();
            _PkgService     = componentModel.GetService <IVsPackageInstallerServices>();
            _PkgRepos       = componentModel.GetService <IVsPackageSourceProvider>();
            _PkgUninstaller = componentModel.GetService <IVsPackageUninstaller>();
            _PkgRestorer    = componentModel.GetService <IVsPackageRestorer>();
        }
        private void BackgroundInstallSlowCheetah(Project project)
        {
            string projName    = project.UniqueName;
            bool   needInstall = true;

            lock (this.syncObject)
            {
                needInstall = this.installTasks.Add(projName);
            }

            if (needInstall)
            {
                if (this.HasUserAcceptedWarningMessage(Resources.Resources.NugetInstall_Title, Resources.Resources.NugetInstall_Text))
                {
                    // Gets the general output pane to inform user of installation
                    IVsOutputWindowPane outputWindow = (IVsOutputWindowPane)this.package.GetService(typeof(SVsGeneralOutputWindowPane));
                    outputWindow?.OutputString(string.Format(Resources.Resources.NugetInstall_InstallingOutput, project.Name) + Environment.NewLine);

                    // Uninstalls the older version (if present) and installs latest package
                    var componentModel = (IComponentModel)this.package.GetService(typeof(SComponentModel));
                    IVsPackageInstaller2 packageInstaller = componentModel.GetService <IVsPackageInstaller2>();

                    TPL.Task.Run(() =>
                    {
                        string outputMessage = Resources.Resources.NugetInstall_FinishedOutput;
                        try
                        {
                            packageInstaller.InstallLatestPackage(null, project, PackageName, false, false);
                        }
                        catch
                        {
                            outputMessage = Resources.Resources.NugetInstall_ErrorOutput;
                            throw;
                        }
                        finally
                        {
                            lock (this.syncObject)
                            {
                                this.installTasks.Remove(projName);
                            }

                            ThreadHelper.Generic.BeginInvoke(() => outputWindow?.OutputString(string.Format(outputMessage, project.Name) + Environment.NewLine));
                        }
                    });
                }
                else
                {
                    lock (this.syncObject)
                    {
                        // If the user refuses to install, the task should not be added
                        this.installTasks.Remove(projName);
                    }
                }
            }
        }
 public PackageServicesProxy(
     IVsPackageInstallerServices packageInstallerServices,
     IVsPackageInstaller2 packageInstaller,
     IVsPackageUninstaller packageUninstaller,
     IVsPackageSourceProvider packageSourceProvider)
 {
     _packageInstallerServices = packageInstallerServices;
     _packageInstaller         = packageInstaller;
     _packageUninstaller       = packageUninstaller;
     _packageSourceProvider    = packageSourceProvider;
 }
Example #6
0
        private void InstallPackageAsyncTest(object sender, EventArgs e)
        {
            EnvDTE.DTE           dte        = ServiceLocator.GetInstance <EnvDTE.DTE>();
            IVsPackageInstaller2 installer2 = ServiceLocator.GetInstance <IVsPackageInstaller>() as IVsPackageInstaller2;

            foreach (EnvDTE.Project project in dte.Solution.Projects)
            {
                _task = installer2.InstallPackageAsync(project, new string[] { "https://api.nuget.org/v2/" }, "dotnetrdf", "[1.0.0, )", false, CancellationToken.None);
                return;
            }
        }
Example #7
0
        private void Execute(object sender, EventArgs e)
        {
            ProjectItem item = VsHelpers.DTE.SelectedItems.Item(1).ProjectItem;

            try
            {
                var dependencies = Dependencies.FromConfigFile(item.FileNames[1]);
                IEnumerable <string> packageIds = dependencies.Providers.Select(p => p.NuGetPackageId).Distinct();

                if (!_isPackageInstalled)
                {
                    if (!UserWantsToInstall())
                    {
                        return;
                    }

                    System.Threading.Tasks.Task.Run(() =>
                    {
                        Logger.LogEvent("Installing NuGet package containing MSBuild target...", LogLevel.Status);

                        foreach (string packageId in packageIds)
                        {
                            IVsPackageInstaller2 installer = _componentModel.GetService <IVsPackageInstaller2>();
                            installer.InstallLatestPackage(null, item.ContainingProject, packageId, true, false);
                        }

                        Telemetry.TrackUserTask("InstallNugetPackage");
                        Logger.LogEvent("NuGet package installed", LogLevel.Status);
                    });
                }
                else
                {
                    System.Threading.Tasks.Task.Run(() =>
                    {
                        Logger.LogEvent("Uninstalling NuGet package...", LogLevel.Status);

                        foreach (string packageId in packageIds)
                        {
                            IVsPackageUninstaller uninstaller = _componentModel.GetService <IVsPackageUninstaller>();
                            uninstaller.UninstallPackage(item.ContainingProject, packageId, false);
                        }

                        Telemetry.TrackUserTask("UninstallNugetPackage");
                        Logger.LogEvent("NuGet package uninstalled", LogLevel.Status);
                    });
                }
            }
            catch (Exception ex)
            {
                Telemetry.TrackException(nameof(RestoreOnBuildCommand), ex);
                Logger.LogEvent("Error installing NuGet package", LogLevel.Status);
            }
        }
        private void UpdateSlowCheetah(Project project)
        {
            // This is done on the UI thread because changes are made to the project file,
            // causing it to be reloaded. To avoid conflicts with NuGet installation,
            // the update is done sequentially
            if (this.HasUserAcceptedWarningMessage(Resources.Resources.NugetUpdate_Title, Resources.Resources.NugetUpdate_Text))
            {
                // Creates dialog informing the user to wait for the installation to finish
                IVsThreadedWaitDialogFactory twdFactory = this.package.GetService(typeof(SVsThreadedWaitDialogFactory)) as IVsThreadedWaitDialogFactory;
                IVsThreadedWaitDialog2       dialog     = null;
                twdFactory?.CreateInstance(out dialog);

                string title = Resources.Resources.NugetUpdate_WaitTitle;
                string text  = Resources.Resources.NugetUpdate_WaitText;
                dialog?.StartWaitDialog(title, text, null, null, null, 0, false, true);
                try
                {
                    // Installs the latest version of the SlowCheetah NuGet package
                    var componentModel = (IComponentModel)this.package.GetService(typeof(SComponentModel));
                    if (this.IsSlowCheetahInstalled(project))
                    {
                        IVsPackageUninstaller packageUninstaller = componentModel.GetService <IVsPackageUninstaller>();
                        packageUninstaller.UninstallPackage(project, PackageName, true);
                    }

                    IVsPackageInstaller2 packageInstaller = componentModel.GetService <IVsPackageInstaller2>();
                    packageInstaller.InstallLatestPackage(null, project, PackageName, false, false);

                    project.Save();
                    ProjectRootElement projectRoot = ProjectRootElement.Open(project.FullName);
                    foreach (ProjectPropertyGroupElement propertyGroup in projectRoot.PropertyGroups.Where(pg => pg.Label.Equals("SlowCheetah")))
                    {
                        projectRoot.RemoveChild(propertyGroup);
                    }

                    foreach (ProjectImportElement import in projectRoot.Imports.Where(i => i.Label == "SlowCheetah" || i.Project == "$(SlowCheetahTargets)"))
                    {
                        projectRoot.RemoveChild(import);
                    }

                    projectRoot.Save();
                }
                finally
                {
                    // Closes the wait dialog. If the dialog failed, does nothing
                    int canceled;
                    dialog?.EndWaitDialog(out canceled);
                }
            }
        }
        private void Execute(object sender, EventArgs e)
        {
            ProjectItem item = VsHelpers.DTE.SelectedItems.Item(1).ProjectItem;

            try
            {
                if (!_isPackageInstalled)
                {
                    if (!UserWantsToInstall())
                    {
                        return;
                    }

                    System.Threading.Tasks.Task.Run(() =>
                    {
                        Logger.LogEvent("Installing NuGet package containing MSBuild target...", LogLevel.Status);

                        IVsPackageInstaller2 installer = _componentModel.GetService <IVsPackageInstaller2>();
                        installer.InstallLatestPackage(null, item.ContainingProject, Constants.NuGetPackageId, true, false);

                        Telemetry.TrackUserTask("InstallNugetPackage");
                        Logger.LogEvent("NuGet package installed", LogLevel.Status);
                    });
                }
                else
                {
                    System.Threading.Tasks.Task.Run(() =>
                    {
                        Logger.LogEvent("Uninstalling NuGet package...", LogLevel.Status);

                        IVsPackageUninstaller uninstaller = _componentModel.GetService <IVsPackageUninstaller>();
                        uninstaller.UninstallPackage(item.ContainingProject, Constants.NuGetPackageId, false);

                        Telemetry.TrackUserTask("UninstallNugetPackage");
                        Logger.LogEvent("NuGet package uninstalled", LogLevel.Status);
                    });
                }
            }
            catch (Exception ex)
            {
                Telemetry.TrackException(nameof(RestoreOnBuildCommand), ex);
                Logger.LogEvent("Error installing NuGet package", LogLevel.Status);
            }
        }
Example #10
0
        public void ProjectFinishedGenerating(Project project)
        {
            // grab reference of project since we need it
            this._currentProject = project;

            // if this isn't a stub, go and grab the NuGet packages
            if (!this._sitecoreConfiguration.IsStub)
            {
                using (var serviceProvider = new ServiceProvider((IServiceProvider)project.DTE))
                {
                    var componentModel             = (IComponentModel)serviceProvider.GetService(typeof(SComponentModel));
                    IVsPackageInstaller2 installer =
                        componentModel.GetService <IVsPackageInstaller2>();

                    foreach (var package in this._sitecoreConfiguration.NuGetConfiguration.Packages)
                    {
                        installer.InstallPackage(null, project, package.Name, package.Version, false);
                    }
                }
            }
        }
Example #11
0
        private async Task ExecuteAsync(object sender, EventArgs e)
        {
            ProjectItem projectItem = await VsHelpers.GetSelectedItemAsync();

            Project project = await VsHelpers.GetProjectOfSelectedItemAsync();

            try
            {
                var dependencies = _dependenciesFactory.FromConfigFile(projectItem.FileNames[1]);
                IEnumerable <string> packageIds = dependencies.Providers
                                                  .Where(p => p.NuGetPackageId != null)
                                                  .Select(p => p.NuGetPackageId)
                                                  .Distinct();

                if (!_isPackageInstalled)
                {
                    if (!UserWantsToInstall())
                    {
                        return;
                    }

                    await Task.Run(() =>
                    {
                        Logger.LogEvent(Resources.Text.Nuget_InstallingPackage, LogLevel.Status);

                        try
                        {
                            foreach (string packageId in packageIds)
                            {
                                IVsPackageInstaller2 installer = _componentModel.GetService <IVsPackageInstaller2>();
                                installer.InstallLatestPackage(null, project, packageId, true, false);
                            }

                            Telemetry.TrackUserTask("Install-NugetPackage");
                            Logger.LogEvent(Resources.Text.Nuget_PackageInstalled, LogLevel.Status);
                        }
                        catch (Exception ex)
                        {
                            Telemetry.TrackException(nameof(RestoreOnBuildCommand), ex);
                            Logger.LogEvent(Resources.Text.Nuget_PackageFailedToInstall, LogLevel.Status);
                        }
                    });
                }
                else
                {
                    await Task.Run(() =>
                    {
                        Logger.LogEvent(Resources.Text.Nuget_UninstallingPackage, LogLevel.Status);

                        try
                        {
                            foreach (string packageId in packageIds)
                            {
                                IVsPackageUninstaller uninstaller = _componentModel.GetService <IVsPackageUninstaller>();
                                uninstaller.UninstallPackage(project, packageId, false);
                            }

                            Telemetry.TrackUserTask("Uninstall-NugetPackage");
                            Logger.LogEvent(Resources.Text.Nuget_PackageUninstalled, LogLevel.Status);
                        }
                        catch (Exception ex)
                        {
                            Telemetry.TrackException(nameof(RestoreOnBuildCommand), ex);
                            Logger.LogEvent(Resources.Text.Nuget_PackageFailedToUninstall, LogLevel.Status);
                        }
                    });
                }
            }
            catch (Exception ex)
            {
                Telemetry.TrackException(nameof(RestoreOnBuildCommand), ex);
                Logger.LogEvent(Resources.Text.Nuget_PackageFailedToInstall, LogLevel.Status);
            }
        }
 public PackageServicesProxy(
     IVsPackageInstallerServices packageInstallerServices,
     IVsPackageInstaller2 packageInstaller,
     IVsPackageUninstaller packageUninstaller,
     IVsPackageSourceProvider packageSourceProvider)
 {
     _packageInstallerServices = packageInstallerServices;
     _packageInstaller = packageInstaller;
     _packageUninstaller = packageUninstaller;
     _packageSourceProvider = packageSourceProvider;
 }