<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using EnvDTE;
using Microsoft.VisualStudio.ExtensionsExplorer;
using NuGet.VisualStudio;
using NuGetConsole.Host.PowerShellProvider;

namespace NuGet.Dialog.Providers {
    /// <summary>
    /// IVsExtensionsProvider implementation responsible for gathering
    /// a list of installed packages which will be shown in the Add Package dialog.
    /// </summary>
    internal class SolutionInstalledProvider : InstalledProvider {

        private readonly ISolutionManager _solutionManager;
        private readonly IProjectSelectorService _projectSelectorService;

        public SolutionInstalledProvider(
            IVsPackageManager packageManager,
            IPackageRepository localRepository,
            ResourceDictionary resources,
            ProviderServices providerServices,
            IProgressProvider progressProvider,
            ISolutionManager solutionManager)
            : base(packageManager, null, localRepository, resources, providerServices, progressProvider, solutionManager) {

            _solutionManager = solutionManager;
            _projectSelectorService = providerServices.ProjectSelector;
        }

        public override bool CanExecute(PackageItem item) {
            return true;
        }

        protected override void FillRootNodes() {
            var allNode = new SimpleTreeNode(
                this,
                Resources.Dialog_RootNodeAll,
                RootNode,
                LocalRepository,
                collapseVersion: false);
            RootNode.Nodes.Add(allNode);
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage(
            "Microsoft.Design",
            "CA1031:DoNotCatchGeneralExceptionTypes",
            Justification = "We don't want one failed project to affect the other projects.")]
        protected override bool ExecuteCore(PackageItem item) {
            IPackage package = item.PackageIdentity;

            // because we are not removing dependencies, we don't need to walk the graph to search for script files
            bool hasScript = package.HasPowerShellScript();
            if (hasScript && !RegistryHelper.CheckIfPowerShell2Installed()) {
                throw new InvalidOperationException(Resources.Dialog_PackageHasPSScript);
            }

            // treat solution-level packages specially
            if (!PackageManager.IsProjectLevel(item.PackageIdentity)) {
                ShowProgressWindow();
                try {
                    RegisterPackageOperationEvents(PackageManager, null);
                    PackageManager.UninstallPackage(
                        null,
                        item.PackageIdentity.Id,
                        item.PackageIdentity.Version,
                        forceRemove: false,
                        removeDependencies: false,
                        logger: this);
                }
                finally {
                    UnregisterPackageOperationEvents(PackageManager, null);
                }
                return true;
            }

            // display the Manage dialog to allow user to pick projects to install/uninstall
            IEnumerable<Project> selectedProjects = _projectSelectorService.ShowProjectSelectorWindow(
                Resources.Dialog_InstalledSolutionInstruction,
                // Selector function to return the initial checkbox state for a Project.
                // We check a project by default if it has the current package installed.
                project => PackageManager.GetProjectManager(project).IsInstalled(package),
                ignored => true);

            if (selectedProjects == null) {
                // user presses Cancel button on the Solution dialog
                return false;
            }

            ShowProgressWindow();

            var selectedProjectsSet = new HashSet<Project>(selectedProjects);

            // now install or uninstall the package depending on the checked states.
            foreach (Project project in _solutionManager.GetProjects()) {
                try {
                    if (selectedProjectsSet.Contains(project)) {
                        // if the project is checked, install package into it  
                        InstallPackageToProject(project, item);
                    }
                    else {
                        // if the project is unchecked, uninstall package from it
                        UninstallPackageFromProject(project, item);
                    }
                }
                catch (Exception ex) {
                    AddFailedProject(project, ex);
                }
            }

            return true;
        }

        public override IVsExtension CreateExtension(IPackage package) {
            string commandText = PackageManager.IsProjectLevel(package) ?
                Resources.Dialog_SolutionManageButton :
                Resources.Dialog_UninstallButton;

            return new PackageItem(this, package, GetReferenceProjects(package)) {
                CommandName = commandText
            };
        }

        protected override void OnExecuteCompleted(PackageItem item) {<commit_msg>Use project's unique name to check if projects are checked or unchecked in the solution installed provider. Work items: 1181<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using EnvDTE;
using Microsoft.VisualStudio.ExtensionsExplorer;
using NuGet.VisualStudio;
using NuGetConsole.Host.PowerShellProvider;

namespace NuGet.Dialog.Providers {
    /// <summary>
    /// IVsExtensionsProvider implementation responsible for gathering
    /// a list of installed packages which will be shown in the Add Package dialog.
    /// </summary>
    internal class SolutionInstalledProvider : InstalledProvider {

        private readonly ISolutionManager _solutionManager;
        private readonly IProjectSelectorService _projectSelectorService;

        public SolutionInstalledProvider(
            IVsPackageManager packageManager,
            IPackageRepository localRepository,
            ResourceDictionary resources,
            ProviderServices providerServices,
            IProgressProvider progressProvider,
            ISolutionManager solutionManager)
            : base(packageManager, null, localRepository, resources, providerServices, progressProvider, solutionManager) {

            _solutionManager = solutionManager;
            _projectSelectorService = providerServices.ProjectSelector;
        }

        public override bool CanExecute(PackageItem item) {
            return true;
        }

        protected override void FillRootNodes() {
            var allNode = new SimpleTreeNode(
                this,
                Resources.Dialog_RootNodeAll,
                RootNode,
                LocalRepository,
                collapseVersion: false);
            RootNode.Nodes.Add(allNode);
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage(
            "Microsoft.Design",
            "CA1031:DoNotCatchGeneralExceptionTypes",
            Justification = "We don't want one failed project to affect the other projects.")]
        protected override bool ExecuteCore(PackageItem item) {
            IPackage package = item.PackageIdentity;

            // because we are not removing dependencies, we don't need to walk the graph to search for script files
            bool hasScript = package.HasPowerShellScript();
            if (hasScript && !RegistryHelper.CheckIfPowerShell2Installed()) {
                throw new InvalidOperationException(Resources.Dialog_PackageHasPSScript);
            }

            // treat solution-level packages specially
            if (!PackageManager.IsProjectLevel(item.PackageIdentity)) {
                ShowProgressWindow();
                try {
                    RegisterPackageOperationEvents(PackageManager, null);
                    PackageManager.UninstallPackage(
                        null,
                        item.PackageIdentity.Id,
                        item.PackageIdentity.Version,
                        forceRemove: false,
                        removeDependencies: false,
                        logger: this);
                }
                finally {
                    UnregisterPackageOperationEvents(PackageManager, null);
                }
                return true;
            }

            // display the Manage dialog to allow user to pick projects to install/uninstall
            IEnumerable<Project> selectedProjects = _projectSelectorService.ShowProjectSelectorWindow(
                Resources.Dialog_InstalledSolutionInstruction,
                // Selector function to return the initial checkbox state for a Project.
                // We check a project by default if it has the current package installed.
                project => PackageManager.GetProjectManager(project).IsInstalled(package),
                ignored => true);

            if (selectedProjects == null) {
                // user presses Cancel button on the Solution dialog
                return false;
            }

            ShowProgressWindow();

            // bug #1181: Use HashSet<unique name> instead of HashSet<Project>.
            // in some rare cases, the project instance returned by GetProjects() may be different 
            // than the ones in selectedProjectSet.
            var selectedProjectsSet = new HashSet<string>(
                selectedProjects.Select(p => p.UniqueName), 
                StringComparer.OrdinalIgnoreCase);

            // now install or uninstall the package depending on the checked states.
            foreach (Project project in _solutionManager.GetProjects()) {
                try {
                    if (selectedProjectsSet.Contains(project.UniqueName)) {
                        // if the project is checked, install package into it  
                        InstallPackageToProject(project, item);
                    }
                    else {
                        // if the project is unchecked, uninstall package from it
                        UninstallPackageFromProject(project, item);
                    }
                }
                catch (Exception ex) {
                    AddFailedProject(project, ex);
                }
            }

            return true;
        }

        public override IVsExtension CreateExtension(IPackage package) {
            string commandText = PackageManager.IsProjectLevel(package) ?
                Resources.Dialog_SolutionManageButton :
                Resources.Dialog_UninstallButton;

            return new PackageItem(this, package, GetReferenceProjects(package)) {
                CommandName = commandText
            };
        }

        protected override void OnExecuteCompleted(PackageItem item) {
{'fraction_non_alphanumeric': 0.058232013895237224, 'fraction_numerical': 0.0016454886187037207, 'mean_word_length': 1.8549060542797495, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6274429', 'n_tokens_mistral': 2363, 'n_tokens_neox': 2204, 'n_words': 660}