<commit_before>﻿using Microsoft.VisualStudio.ExtensionManager;
using System;
using System.Reflection;
using System.Text.RegularExpressions;

namespace VsixUtil
{
    public sealed class CommandRunner : MarshalByRefObject
    {
        internal readonly IVsExtensionManager _extensionManager;
        internal readonly InstalledVersion _installedVersion;
        internal readonly string _rootSuffix;

        internal CommandRunner(IConsoleContext consoleContext, IVsExtensionManager extensionManager,
            InstalledVersion installedVersion, string rootSuffix)
        {
            Console = consoleContext;
            _extensionManager = extensionManager;
            _installedVersion = installedVersion;
            _rootSuffix = rootSuffix;
        }

        IConsoleContext Console
        {
            get;
        }

        public void Run(ToolAction toolAction, string arg)
        {
            switch (toolAction)
            {
                case ToolAction.Install:
                    RunInstall(arg);
                    break;
                case ToolAction.Uninstall:
                    RunUninstall(arg);
                    break;
                case ToolAction.List:
                    RunList(arg);
                    break;
                case ToolAction.Help:
                    Program.PrintHelp(Console);
                    break;
                default:
                    throw new Exception(string.Format("Not implemented {0}", toolAction));
            }
        }

        private void RunInstall(string extensionPath)
        {
            try
            {
                Console.Write("{0} Install ... ", _installedVersion);
                var installableExtension = _extensionManager.CreateInstallableExtension(extensionPath);
                var identifier = installableExtension.Header.Identifier;
                UninstallSilent(identifier);

                var perMachine = false;
                var header = installableExtension.Header;
                if (header.AllUsers != perMachine)
                {
                    if (SetAllUsers(header, perMachine))
                    {
                        Console.Write(string.Format("NOTE: Changing `AllUsers` to {0} ... ", perMachine));
                    }
                    else
                    {
                        Console.Write(string.Format("WARNING: Couldn't change `AllUsers` to {0} ... ", perMachine));
                    }
                }

                _extensionManager.Install(installableExtension, perMachine);

                var installedExtension = _extensionManager.GetInstalledExtension(identifier);
                _extensionManager.Enable(installedExtension);
                Console.WriteLine("Succeeded");
            }
            catch (Exception ex)
            {
                Console.WriteLine("ERROR: {0}", ex.Message);
            }
        }

        private static bool SetAllUsers(IExtensionHeader header, bool allUsers)
        {
            var flags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public;
            var allUsersProperty = header.GetType().GetProperty("AllUsers", flags);
            if (allUsersProperty == null || !allUsersProperty.CanWrite)
            {
                return false;
            }

            allUsersProperty.SetValue(header, allUsers, null);
            return true;
        }

        private void RunUninstall(string identifier)
        {
            try
            {
                Console.Write("{0} Uninstall ... ", _installedVersion);
                var installedExtension = _extensionManager.GetInstalledExtension(identifier);
                if (installedExtension != null)
                {
                    _extensionManager.Uninstall(installedExtension);
                }

                Console.WriteLine("Succeeded");
            }
            catch (Exception ex)
            {
                Console.WriteLine("ERROR: {0}", ex.Message);
            }
        }

        private void UninstallSilent(string identifier, bool printError = false)
        {
            try
            {
                var installedExtension = _extensionManager.GetInstalledExtension(identifier);
                if (installedExtension != null)
                {
                    _extensionManager.Uninstall(installedExtension);
                }
            }
            catch
            {

            }
        }
<commit_msg>Don't throw when extension isn't installed<commit_after>﻿using Microsoft.VisualStudio.ExtensionManager;
using System;
using System.Reflection;
using System.Text.RegularExpressions;

namespace VsixUtil
{
    public sealed class CommandRunner : MarshalByRefObject
    {
        internal readonly IVsExtensionManager _extensionManager;
        internal readonly InstalledVersion _installedVersion;
        internal readonly string _rootSuffix;

        internal CommandRunner(IConsoleContext consoleContext, IVsExtensionManager extensionManager,
            InstalledVersion installedVersion, string rootSuffix)
        {
            Console = consoleContext;
            _extensionManager = extensionManager;
            _installedVersion = installedVersion;
            _rootSuffix = rootSuffix;
        }

        IConsoleContext Console
        {
            get;
        }

        public void Run(ToolAction toolAction, string arg)
        {
            switch (toolAction)
            {
                case ToolAction.Install:
                    RunInstall(arg);
                    break;
                case ToolAction.Uninstall:
                    RunUninstall(arg);
                    break;
                case ToolAction.List:
                    RunList(arg);
                    break;
                case ToolAction.Help:
                    Program.PrintHelp(Console);
                    break;
                default:
                    throw new Exception(string.Format("Not implemented {0}", toolAction));
            }
        }

        private void RunInstall(string extensionPath)
        {
            try
            {
                Console.Write("{0} Install ... ", _installedVersion);
                var installableExtension = _extensionManager.CreateInstallableExtension(extensionPath);
                var identifier = installableExtension.Header.Identifier;
                UninstallSilent(identifier);

                var perMachine = false;
                var header = installableExtension.Header;
                if (header.AllUsers != perMachine)
                {
                    if (SetAllUsers(header, perMachine))
                    {
                        Console.Write(string.Format("NOTE: Changing `AllUsers` to {0} ... ", perMachine));
                    }
                    else
                    {
                        Console.Write(string.Format("WARNING: Couldn't change `AllUsers` to {0} ... ", perMachine));
                    }
                }

                _extensionManager.Install(installableExtension, perMachine);

                var installedExtension = _extensionManager.GetInstalledExtension(identifier);
                _extensionManager.Enable(installedExtension);
                Console.WriteLine("Succeeded");
            }
            catch (Exception ex)
            {
                Console.WriteLine("ERROR: {0}", ex.Message);
            }
        }

        private static bool SetAllUsers(IExtensionHeader header, bool allUsers)
        {
            var flags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public;
            var allUsersProperty = header.GetType().GetProperty("AllUsers", flags);
            if (allUsersProperty == null || !allUsersProperty.CanWrite)
            {
                return false;
            }

            allUsersProperty.SetValue(header, allUsers, null);
            return true;
        }

        private void RunUninstall(string identifier)
        {
            try
            {
                Console.Write("{0} Uninstall ... ", _installedVersion);
                IInstalledExtension installedExtension;
                if (_extensionManager.TryGetInstalledExtension(identifier, out installedExtension))
                {
                    _extensionManager.Uninstall(installedExtension);
                    Console.WriteLine("Succeeded");
                }
                else
                {
                    Console.WriteLine("Not Installed");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("ERROR: {0}", ex.Message);
            }
        }

        private void UninstallSilent(string identifier, bool printError = false)
        {
            try
            {
                IInstalledExtension installedExtension;
                if (_extensionManager.TryGetInstalledExtension(identifier, out installedExtension))
                {
                    _extensionManager.Uninstall(installedExtension);
                }
            }
            catch
            {

            }
        }

{'fraction_non_alphanumeric': 0.07220454296060573, 'fraction_numerical': 0.0015362668715022496, 'mean_word_length': 1.438854696280439, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2088505', 'n_tokens_mistral': 2058, 'n_tokens_neox': 1890, 'n_words': 402}