internal void InstallExtension(string vsixPath)
        {
            if (!Exists())
                throw new InvalidOperationException("Cannot install VSIX in non-existing instance.");

            using (var settings = ExternalSettingsManager.CreateForApplication(GetExePath(), Suffix))
            {
                var ems = new ExtensionManagerService(settings);
                IInstallableExtension vsix = ExtensionManagerService.CreateInstallableExtension(vsixPath);

                if (ems.IsInstalled(vsix))
                {
                    IInstalledExtension installedVsix = ems.GetInstalledExtension(vsix.Header.Identifier);
                    ems.Uninstall(installedVsix);
                    if (ems.IsInstalled(vsix))
                        throw new InvalidOperationException("Could not uninstall already installed GoogleTestAdapter.");
                }

                ems.Install(vsix, perMachine: false);
                if (!ems.IsInstalled(vsix))
                    throw new InvalidOperationException("Could not install GoogleTestAdapter.");

                ems.Close();
            }
        }
Ejemplo n.º 2
0
        public void InstallExtension(string vsixPath)
        {
            if (!Exists())
            {
                throw new InvalidOperationException("Cannot install VSIX in non-existing instance.");
            }

            using (var settings = ExternalSettingsManager.CreateForApplication(GetExePath(), Suffix))
            {
                var ems = new ExtensionManagerService(settings);
                IInstallableExtension vsix = ExtensionManagerService.CreateInstallableExtension(vsixPath);

                if (ems.IsInstalled(vsix))
                {
                    IInstalledExtension installedVsix = ems.GetInstalledExtension(vsix.Header.Identifier);
                    ems.Uninstall(installedVsix);
                    if (ems.IsInstalled(vsix))
                    {
                        throw new InvalidOperationException("Could not uninstall already installed GoogleTestAdapter.");
                    }
                }

                ems.Install(vsix, perMachine: false);
                if (!ems.IsInstalled(vsix))
                {
                    throw new InvalidOperationException("Could not install GoogleTestAdapter.");
                }

                ems.Close();
            }
        }
Ejemplo n.º 3
0
 static void Install(string vsExe, IInstallableExtension vsix, string rootSuffix)
 {
     using (var esm = ExternalSettingsManager.CreateForApplication(vsExe, rootSuffix))
     {
         var ems       = new ExtensionManagerService(esm);
         var installed = ems.GetInstalledExtensions().FirstOrDefault(x => x.Header.Identifier == vsix.Header.Identifier);
         if (installed != null)
         {
             ems.Uninstall(installed);
         }
         ems.Install(vsix, perMachine: false);
     }
 }
Ejemplo n.º 4
0
        public static void Install(string vsExe, IInstallableExtension vsix, string rootSuffix)
        {
            using (var esm = ExternalSettingsManager.CreateForApplication(vsExe, rootSuffix))
            {
                var ems = new ExtensionManagerService(esm);
                IInstalledExtension installedVsix = null;
                if (ems.TryGetInstalledExtension(vsix.Header.Identifier, out installedVsix))
                {
                    Console.WriteLine($"Extension {vsix.Header.Name} version {vsix.Header.Version} already installed, unistalling first.");
                    ems.Uninstall(installedVsix);
                }

                ems.Install(vsix, perMachine: false);
            }
        }
Ejemplo n.º 5
0
        static void UninstallAll(ExtensionManagerService service)
        {
            // We only want extensions which are not installed per machine and we want them sorted by their dependencies.
            var installedExtensions = service.GetInstalledExtensions()
                                      .Where((installedExtension) => !installedExtension.InstalledPerMachine)
                                      .OrderBy((installedExtension) => installedExtension,
                                               Comparer <IInstalledExtension> .Create((left, right) =>
            {
                if (left.References.Count() == 0)
                {
                    // When left.References.Count() is zero, then we have two scenarios:
                    //    * right.References.Count() is zero, and the order of the two components doesn't matter, so we return 0
                    //    * right.References.Count() is not zero, which means it should be uninstalled after left, so we return -1
                    return(right.References.Count() == 0 ? 0 : -1);
                }
                else if (right.References.Count() == 0)
                {
                    // When left.References.Count() is not zero, but right.References.Count() is, then we have one scenario:
                    //    * right should be uninstalled before left, so we return 1
                    return(1);
                }

                if (left.References.Any((extensionReference) => extensionReference.Identifier == right.Header.Identifier))
                {
                    // When left.References contains right, then we have one scenario:
                    //    * left is dependent on right, which means it must be uninstalled afterwards, so we return 1
                    return(1);
                }
                else if (right.References.Any((extensionReference) => extensionReference.Identifier == left.Header.Identifier))
                {
                    // When right.References contains left, then we have one scenario:
                    //    * right is dependent on left, which means it must be uninstalled afterwards, so we return -1
                    return(-1);
                }

                // Finally, if both projects contain references, but neither depends on the other, we have one scenario:
                //    * left and right are independent of each other, and the order of the two components doesn't matter, so we return 0
                return(0);
            }));

            foreach (var installedExtension in installedExtensions)
            {
                Console.WriteLine("  Uninstalling {0}... ", installedExtension.Header.Name);
                service.Uninstall(installedExtension);
            }
        }
Ejemplo n.º 6
0
        private static IInstalledExtension PerformInstallation(IInstallableExtension vsix, string devenvPath, string hive)
        {
            IInstalledExtension extension;

            using (var settingsManager = ExternalSettingsManager.CreateForApplication(devenvPath, hive))
            {
                var identifier = vsix.Header.Identifier;
                var name       = vsix.Header.Name;

                Log($"Preparing to install '{name}' with identifier '{identifier}' into '{hive}'");

                var extensionManager = new ExtensionManagerService(settingsManager);

                if (extensionManager.TryGetInstalledExtension(identifier, out extension))
                {
                    Log($"Extension '{name}' was already installed. Uninstalling...");
                    try
                    {
                        extensionManager.Uninstall(extension);
                        extensionManager.CommitExternalUninstall(extension);
                    }
                    catch (Exception ex)
                    {
                        LogError($"An error ocurred while trying to uninstall '{name}'. Rolling back...", ex);
                        RevertUninstall(extensionManager, extension);
                        throw;
                    }
                }
                try
                {
                    Log($"Starting installation of '{name}'");
                    extensionManager.Install(vsix, perMachine: false);
                    extension = extensionManager.GetInstalledExtension(identifier);
                    Log($"Installation of '{name}' into '{hive}' completed successfully.");
                }
                catch (Exception ex)
                {
                    LogError($"An error ocurred while trying to install '{name}'. Rolling back...", ex);
                    RevertUninstall(extensionManager, extension);
                    throw;
                }
            }

            return(extension);
        }
 static void Install(string vsExe, IInstallableExtension vsix, string rootSuffix)
 {
     using (var esm = ExternalSettingsManager.CreateForApplication(vsExe, rootSuffix))
     {
         var ems = new ExtensionManagerService(esm);
         var installed = ems.GetInstalledExtensions().FirstOrDefault(x => x.Header.Identifier == vsix.Header.Identifier);
         if (installed != null)
             ems.Uninstall(installed);
         ems.Install(vsix, perMachine: false);
     }
 }
Ejemplo n.º 8
0
        public static int Main(string[] args)
        {
            if (args.Length == 0)
            {
                PrintUsage();
                return(0);
            }

            var argList = new List <string>(args);

            var rootSuffix   = ExtractArg(argList, "rootSuffix") ?? "Exp";
            var uninstall    = FindArg(argList, "u");
            var uninstallAll = FindArg(argList, "uninstallAll");
            var printHelp    = FindArg(argList, "?") || FindArg(argList, "h") || FindArg(argList, "help");
            var vsInstallDir = ExtractArg(argList, "vsInstallDir") ?? Environment.GetEnvironmentVariable("VsInstallDir");

            var expectedArgCount = uninstallAll ? 0 : 1;

            if (argList.Count != expectedArgCount)
            {
                PrintUsage();
                return(1);
            }

            string vsixPath = uninstallAll ? string.Empty : argList[0];
            string devenvPath;

            try
            {
                devenvPath = GetDevenvPath(vsInstallDir);

                var assemblyResolutionPaths = new string[] {
                    Path.Combine(vsInstallDir, @"Common7\IDE"),
                    Path.Combine(vsInstallDir, @"Common7\IDE\PrivateAssemblies"),
                    Path.Combine(vsInstallDir, @"Common7\IDE\PublicAssemblies")
                };

                AppDomain.CurrentDomain.AssemblyResolve += (object sender, ResolveEventArgs eventArgs) => {
                    var assemblyFileName = $"{eventArgs.Name.Split(',')[0]}.dll";

                    foreach (var assemblyResolutionPath in assemblyResolutionPaths)
                    {
                        var assemblyFilePath = Path.Combine(assemblyResolutionPath, assemblyFileName);

                        if (File.Exists(assemblyFilePath))
                        {
                            return(Assembly.LoadFrom(assemblyFilePath));
                        }
                    }

                    return(null);
                };

                RunProgram();

                // Move all of this into a local method so that it only causes the assembly loads after the resolver has been hooked up
                void RunProgram()
                {
                    using (var settingsManager = ExternalSettingsManager.CreateForApplication(devenvPath, rootSuffix))
                    {
                        ExtensionManagerService extensionManagerService = null;

                        try
                        {
                            extensionManagerService = new ExtensionManagerService(settingsManager);

                            if (uninstallAll)
                            {
                                Console.WriteLine("Uninstalling all... ");
                                UninstallAll(extensionManagerService);
                            }
                            else
                            {
                                var extensionManager    = (IVsExtensionManager)(extensionManagerService);
                                var vsixToInstall       = extensionManager.CreateInstallableExtension(vsixPath);
                                var vsixToInstallHeader = vsixToInstall.Header;

                                var foundBefore             = extensionManagerService.TryGetInstalledExtension(vsixToInstallHeader.Identifier, out var installedVsixBefore);
                                var installedGloballyBefore = foundBefore && installedVsixBefore.InstallPath.StartsWith(vsInstallDir, StringComparison.OrdinalIgnoreCase);

                                if (uninstall)
                                {
                                    if (foundBefore && !installedGloballyBefore)
                                    {
                                        Console.WriteLine("Uninstalling {0}... ", vsixPath);
                                        extensionManagerService.Uninstall(installedVsixBefore);
                                    }
                                    else
                                    {
                                        Console.WriteLine("Nothing to uninstall... ");
                                    }
                                }
                                else
                                {
                                    if (foundBefore && installedGloballyBefore && (vsixToInstallHeader.Version < installedVsixBefore.Header.Version))
                                    {
                                        throw new Exception($"The version you are attempting to install ({vsixToInstallHeader.Version}) has a version that is less than the one installed globally ({installedVsixBefore.Header.Version}).");
                                    }
                                    else if (foundBefore && !installedGloballyBefore)
                                    {
                                        Console.WriteLine("Updating {0}... ", vsixPath);
                                        extensionManagerService.Uninstall(installedVsixBefore);
                                    }
                                    else
                                    {
                                        Console.WriteLine("Installing {0}... ", vsixPath);
                                    }

                                    extensionManagerService.Install(vsixToInstall, perMachine: false);
                                    var settingsStore = settingsManager.GetWritableSettingsStore(SettingsScope.UserSettings);

                                    EnableLoadingAllExtensions(settingsStore);
                                    RemoveExtensionFromPendingDeletions(settingsStore, vsixToInstallHeader);
                                    UpdateLastExtensionsChange(settingsStore);

                                    // Recreate the extensionManagerService to force the extension cache to recreate
                                    extensionManagerService?.Close();
                                    extensionManagerService = new ExtensionManagerService(settingsManager);

                                    var foundAfter             = extensionManagerService.TryGetInstalledExtension(vsixToInstallHeader.Identifier, out var installedVsixAfter);
                                    var installedGloballyAfter = foundAfter && installedVsixAfter.InstallPath.StartsWith(vsInstallDir, StringComparison.OrdinalIgnoreCase);

                                    if (uninstall && foundAfter)
                                    {
                                        if (installedGloballyBefore && installedGloballyAfter)
                                        {
                                            throw new Exception($"The extension failed to uninstall. It is still installed globally.");
                                        }
                                        else if (!installedGloballyBefore && installedGloballyAfter)
                                        {
                                            Console.WriteLine("The local extension was succesfully uninstalled. However, the global extension is still installed.");
                                        }
                                        else
                                        {
                                            Console.WriteLine("The extension was succesfully uninstalled.");
                                        }
                                    }
                                    else if (!uninstall)
                                    {
                                        if (!foundAfter)
                                        {
                                            throw new Exception($"The extension failed to install. It could not be located.");
                                        }
                                        else if (installedVsixAfter.Header.Version != vsixToInstallHeader.Version)
                                        {
                                            throw new Exception("The extension failed to install. The located version does not match the expected version.");
                                        }
                                        else
                                        {
                                            Console.WriteLine("The extension was succesfully installed.");
                                        }
                                    }
                                }
                            }
                        }
                        finally
                        {
                            extensionManagerService?.Close();
                            extensionManagerService = null;
                        }

                        Console.WriteLine("Done!");
                    }
                }
            }
            catch (Exception e)
            {
                string message = e.GetType().Name + ": " + e.Message + Environment.NewLine + e.ToString();
                Console.Error.WriteLine(message);
                return(2);
            }

            return(0);
        }