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

namespace VsixUtil
{
    public sealed class ExtensionManagerFactory
    {
        private static Assembly LoadImplementationAssembly(VsVersion version)
        {
            var format = "Microsoft.VisualStudio.ExtensionManager.Implementation, Version={0}.0.0.0, Culture=neutral, PublicKeyToken=<KEY>";
            var strongName = string.Format(format, VsVersionUtil.GetVersionNumber(version));
            return Assembly.Load(strongName);
        }

        private static Assembly LoadSettingsAssembly(VsVersion version)
        {
            var format = "Microsoft.VisualStudio.Settings{0}, Version={1}.0.0.0, Culture=neutral, PublicKeyToken=<KEY>";
            string suffix = "";
            switch (version)
            {
                case VsVersion.Vs2010:
                    suffix = "";
                    break;
                case VsVersion.Vs2012:
                    suffix = ".11.0";
                    break;
                case VsVersion.Vs2013:
                    suffix = ".12.0";
                    break;
                case VsVersion.Vs2015:
                    suffix = ".14.0";
                    break;
                case VsVersion.Vs2017:
                    suffix = ".15.0";
                    break;
                default:
                    throw new Exception("Bad Version");
            }

            var strongName = string.Format(format, suffix, VsVersionUtil.GetVersionNumber(version));
            return Assembly.Load(strongName);
        }

        private static Type GetExtensionManagerServiceType(VsVersion version)
        {
            var assembly = LoadImplementationAssembly(version);
            return assembly.GetType("Microsoft.VisualStudio.ExtensionManager.ExtensionManagerService");
        }

        public static IVsExtensionManager CreateExtensionManager(InstalledVersion installedVersion, string rootSuffix)
        {
            var settingsAssembly = LoadSettingsAssembly(installedVersion.VsVersion);

            var externalSettingsManagerType = settingsAssembly.GetType("Microsoft.VisualStudio.Settings.ExternalSettingsManager");
            var settingsManager = externalSettingsManagerType
                .GetMethods()
                .Where(x => x.Name == "CreateForApplication")
                .Where(x =>
                {
                    var parameters = x.GetParameters();
                    return
                        parameters.Length == 2 &&
                        parameters[0].ParameterType == typeof(string) &&
                        parameters[1].ParameterType == typeof(string);
                })
                .FirstOrDefault()
                .Invoke(null, new[] { installedVersion.ApplicationPath, rootSuffix });

            var extensionManagerServiceType = GetExtensionManagerServiceType(installedVersion.VsVersion);
            var extensionManager = (IVsExtensionManager)extensionManagerServiceType
                .GetConstructors()
                .Where(x => x.GetParameters().Length == 1 && x.GetParameters()[0].ParameterType.Name.Contains("SettingsManager"))
                .FirstOrDefault()
                .Invoke(new[] { settingsManager });
            return extensionManager;
        }
    }
}
<commit_msg>Add support for VS2017 15.7.3<commit_after>﻿using System;
using System.Linq;
using System.Reflection;
using Microsoft.VisualStudio.ExtensionManager;

namespace VsixUtil
{
    public sealed class ExtensionManagerFactory
    {
        private static Assembly LoadImplementationAssembly(VsVersion version)
        {
            var format = "Microsoft.VisualStudio.ExtensionManager.Implementation, Version={0}.0.0.0, Culture=neutral, PublicKeyToken=<KEY>";
            var strongName = string.Format(format, VsVersionUtil.GetVersionNumber(version));
            return Assembly.Load(strongName);
        }

        private static Assembly LoadSettingsAssembly(VsVersion version)
        {
            var format = "Microsoft.VisualStudio.Settings{0}, Version={1}.0.0.0, Culture=neutral, PublicKeyToken=<KEY>";
            string suffix = "";
            switch (version)
            {
                case VsVersion.Vs2010:
                    suffix = "";
                    break;
                case VsVersion.Vs2012:
                    suffix = ".11.0";
                    break;
                case VsVersion.Vs2013:
                    suffix = ".12.0";
                    break;
                case VsVersion.Vs2015:
                    suffix = ".14.0";
                    break;
                case VsVersion.Vs2017:
                    suffix = ".15.0";
                    break;
                default:
                    throw new Exception("Bad Version");
            }

            var strongName = string.Format(format, suffix, VsVersionUtil.GetVersionNumber(version));
            return Assembly.Load(strongName);
        }

        private static Type GetExtensionManagerServiceType(VsVersion version)
        {
            var assembly = LoadImplementationAssembly(version);
            return assembly.GetType("Microsoft.VisualStudio.ExtensionManager.ExtensionManagerService");
        }

        public static IVsExtensionManager CreateExtensionManager(InstalledVersion installedVersion, string rootSuffix)
        {
            var settingsAssembly = LoadSettingsAssembly(installedVersion.VsVersion);

            var externalSettingsManagerType = settingsAssembly.GetType("Microsoft.VisualStudio.Settings.ExternalSettingsManager");
            var settingsManager = externalSettingsManagerType
                .GetMethods()
                .Where(x => x.Name == "CreateForApplication")
                .Where(x =>
                {
                    var parameters = x.GetParameters();
                    return
                        parameters.Length == 2 &&
                        parameters[0].ParameterType == typeof(string) &&
                        parameters[1].ParameterType == typeof(string);
                })
                .FirstOrDefault()
                .Invoke(null, new[] { installedVersion.ApplicationPath, rootSuffix });

            var extensionManagerServiceType = GetExtensionManagerServiceType(installedVersion.VsVersion);
            var extensionManager = (IVsExtensionManager)extensionManagerServiceType
                .GetConstructors()
                .Where(x => x.GetParameters().Length == 1 && x.GetParameters()[0].ParameterType.IsAssignableFrom(settingsManager.GetType()))
                .FirstOrDefault()
                .Invoke(new[] { settingsManager });
            return extensionManager;
        }
    }
}

{'fraction_non_alphanumeric': 0.08798696489408976, 'fraction_numerical': 0.014812620352540365, 'mean_word_length': 1.819206680584551, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, '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': '5875026', 'n_tokens_mistral': 1628, 'n_tokens_neox': 1465, 'n_words': 292}