示例#1
0
        private void RunInstall(string extensionPath)
        {
            try
            {
                Console.Write("{0} Install ... ", _version);
                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);
            }
        }
示例#2
0
        public bool Install(string extensionPath, bool?allUsers = default)
        {
            var installableExtension = extensionManager.CreateInstallableExtension(extensionPath);

            if (extensionManager.TryGetInstalledExtension(installableExtension.Header.Identifier, out var installedExtension))
            {
                if (IsUpToDate(installableExtension, installedExtension))
                {
                    Console.WriteLine($"Extension {NameVer(installedExtension)} is up to date.");
                    if (installedExtension.State != EnabledState.Enabled)
                    {
                        extensionManager.Enable(installedExtension);
                    }
                    return(false);
                }

                Uninstall(installableExtension.Header.Identifier, skipGlobal: GetIsExperimentalProperty(installableExtension.Header) != null);
            }

            if (extensionManager.TryGetInstalledExtension(installableExtension.Header.Identifier, out var globalExtension) && globalExtension.InstalledPerMachine)
            {
                if (installableExtension.Header.Version <= globalExtension.Header.Version)
                {
                    throw new Exception($"Extension '{NameVer(installableExtension)}' version must be higher than the globally installed extension '{NameVer(globalExtension)}'.");
                }

                SetIsExperimental(installableExtension.Header, true);
            }

            if (allUsers.HasValue)
            {
                SetAllUsers(installableExtension.Header, allUsers.Value);
            }

            Console.WriteLine($"Installing {NameVer(installableExtension)}");
            extensionManager.Install(installableExtension, installableExtension.Header.AllUsers);
            ExtensionManagerUtil.EnableLoadingExtensionsFromLocalAppData(externalSettingsManager);
            ExtensionManagerUtil.RemovePendingExtensionDeletion(externalSettingsManager, installableExtension.Header);
            ExtensionManagerService.UpdateLastExtensionsChange();
            var newlyInstalledExtension = extensionManager.GetInstalledExtension(installableExtension.Header.Identifier);

            extensionManager.Enable(newlyInstalledExtension);
            File.SetLastWriteTime(GetManifestPath(newlyInstalledExtension), DateTime.Now);
            if (!IsUpToDate(installableExtension, newlyInstalledExtension))
            {
                throw new Exception($"Failed installing extension '{NameVer(installableExtension)}'.");
            }
            return(true);
        }
示例#3
0
        public bool InstallExtension(string extensionPath)
        {
            var installableExtension = extensionManager.CreateInstallableExtension(extensionPath);
            var packageWriteDateTime = File.GetLastWriteTime(installableExtension.PackagePath);
            var installedExtension   = default(IInstalledExtension);

            if (extensionManager.IsInstalled(installableExtension))
            {
                installedExtension = extensionManager.GetInstalledExtension(installableExtension.Header.Identifier);
                var installedOn = File.GetLastWriteTime(GetManifestPath(installedExtension)); // installedExtension.InstalledOn throws

                if (installableExtension.Header.Version == installedExtension.Header.Version && packageWriteDateTime < installedOn)
                {
                    Console.WriteLine($"Extension {NameVer(installedExtension)} is up to date.");
                    extensionManager.Enable(installedExtension);
                    return(false);
                }

                Console.WriteLine($"Uninstalling {NameVer(installedExtension)}");
                extensionManager.Uninstall(installedExtension);
            }

            if (installableExtension.Header.AllUsers)
            {
                var header = installableExtension.Header;
                header.GetType().GetProperty(nameof(header.AllUsers)).SetValue(header, false);
            }

            Console.WriteLine($"Installing {NameVer(installableExtension)}");
            extensionManager.Install(installableExtension, false);

            var newlyInstalledExtension = extensionManager.GetInstalledExtension(installableExtension.Header.Identifier);

            extensionManager.Enable(newlyInstalledExtension);
            File.SetLastWriteTime(GetManifestPath(newlyInstalledExtension), DateTime.Now);

            if (installedExtension != null && !PathUtil.ArePathEqual(installedExtension.InstallPath, newlyInstalledExtension.InstallPath))
            {
                Console.WriteLine($"Removing {installedExtension.InstallPath}");

                if (!DirectoryUtil.TryDeleteHard(installedExtension.InstallPath, recursive: true))
                {
                    Console.WriteLine($"The directory {installedExtension.InstallPath} could not be deleted completely. If you do not delete it manually, it will waste disk space.");
                }
            }

            return(true);
        }
示例#4
0
        private void RunInstall(string extensionPath)
        {
            try
            {
                Console.Write("{0} Install ... ", _version);
                var installableExtension = _extensionManager.CreateInstallableExtension(extensionPath);
                var identifier           = installableExtension.Header.Identifier;
                UninstallSilent(identifier);
                _extensionManager.Install(installableExtension, perMachine: false);

                var installedExtension = _extensionManager.GetInstalledExtension(identifier);
                _extensionManager.Enable(installedExtension);
                Console.WriteLine("Succeeded");
            }
            catch (Exception ex)
            {
                Console.WriteLine("ERROR: {0}", ex.Message);
            }
        }
        private void TryInstallVsIntegration([CanBeNull] IVsExtensionManager vsExtensionManager)
        {
            if (vsExtensionManager == null)
            {
                return;
            }

            IInstalledExtension vsIntegrationExtension;

            if (vsExtensionManager.TryGetInstalledExtension(VsIntegrationExtensionId, out vsIntegrationExtension))
            {
                return;
            }

            FileSystemPath thisAssemblyPath = FileSystemPath.TryParse(typeof(VsIntegrationInstaller).Assembly.Location);

            if (thisAssemblyPath.IsNullOrEmpty())
            {
                return;
            }

            FileSystemPath vsixPath = thisAssemblyPath.Directory.Combine(VsIntegrationExtensionId + ".vsix");

            if (!vsixPath.ExistsFile)
            {
                MessageBox.ShowInfo("Does not exist: \"{0}\"".FormatEx(vsixPath.FullPath));
                return;
            }

            IInstallableExtension installableExtension = vsExtensionManager.CreateInstallableExtension(vsixPath.FullPath);
            RestartReason         restartReason        = vsExtensionManager.Install(installableExtension, false);

            if (restartReason != RestartReason.None)
            {
                _restartRequired = true;
            }
        }
示例#6
0
        public void RegisterPackage(string packageIdentifier, string packagePath)
        {
            var fileInfo           = new FileInfo(packagePath);
            var outdated           = false;
            var installedExtension = (IInstalledExtension)null;

            if (!fileInfo.Exists)
            {
                var openFileDialog = new OpenFileDialog();
                var ext            = Path.GetExtension(packagePath).Remove(0, 1);
                var window         = new WindowWrapper(Process.GetCurrentProcess().MainWindowHandle);

                openFileDialog.CheckFileExists  = true;
                openFileDialog.Filter           = string.Format("Package files (*.{0})|*.{0}", ext);
                openFileDialog.DefaultExt       = ext;
                openFileDialog.InitialDirectory = Environment.ExpandEnvironmentVariables("%HYDRASOLUTIONPATH%");
                openFileDialog.Title            = string.Format("Select file for '{0}'", packagePath);

                if (openFileDialog.ShowDialog(window) != DialogResult.OK)
                {
                    var message = new BuildErrorEventArgs(string.Empty, string.Empty, string.Empty, 0, 0, 0, 0, string.Format("Error finding package '{0}' at '{1}'", packageIdentifier, packagePath) + ". Please select file from bin folder of Package Project or get latest from TFS.", "", "", DateTime.Now);
                    this.BuildEngine.LogErrorEvent(message);

                    return;
                }
                else
                {
                    File.Copy(openFileDialog.FileName, packagePath);
                }
            }

            fileInfo = new FileInfo(packagePath);

            if (!fileInfo.Exists)
            {
                var message = new BuildErrorEventArgs(string.Empty, string.Empty, string.Empty, 0, 0, 0, 0, string.Format("Error finding package '{0}' at '{1}'", packageIdentifier, packagePath) + ". Please select file from bin folder of Package Project or get latest from TFS.", "", "", DateTime.Now);
                this.BuildEngine.LogErrorEvent(message);

                return;
            }

            if (fileInfo.Extension == ".dll")
            {
                // is a dll, use RegPkg

                var projectFile = new FileInfo(this.BuildEngine.ProjectFileOfTaskNode);
                var regPkgPath  = Path.Combine(solutionPath, @"Binaries\SolutionLibraries\RegPkg.exe");

                var process   = new Process();
                var startInfo = new ProcessStartInfo
                {
                    UseShellExecute        = false,
                    RedirectStandardError  = true,
                    RedirectStandardOutput = true,
                    CreateNoWindow         = true,
                    FileName  = regPkgPath,
                    Arguments = "/unregister" + " \"" + fileInfo.FullName + "\""
                };

                process.StartInfo = startInfo;
                process.Start();

                var result = process.StandardOutput.ReadToEnd();
                result += "\r\n" + process.StandardError.ReadToEnd();

                process.WaitForExit();

                if (process.ExitCode != 0)
                {
                    var message = new BuildErrorEventArgs(string.Empty, string.Empty, string.Empty, 0, 0, 0, 0, string.Format("Error unregistering package '{0}'. ", packageIdentifier) + result + " Exit code = " + process.ExitCode.ToString() + ". You can try running Visual Studio as Administrator to uninstall this component.", "", "", DateTime.Now);
                    this.BuildEngine.LogErrorEvent(message);
                }
                else
                {
                    var message = new BuildMessageEventArgs(string.Format("'{0}' package unregistered successfully.  ", packageIdentifier) + result, "", "", MessageImportance.High);
                    this.BuildEngine.LogMessageEvent(message);
                }

                process   = new Process();
                startInfo = new ProcessStartInfo
                {
                    UseShellExecute        = false,
                    RedirectStandardError  = true,
                    RedirectStandardOutput = true,
                    CreateNoWindow         = true,
                    FileName  = regPkgPath,
                    Arguments = "/root:Software\\Microsoft\\VisualStudio\\10.0 \"" + packagePath + "\""
                };

                process.StartInfo = startInfo;
                process.Start();

                result  = process.StandardOutput.ReadToEnd();
                result += "\r\n" + process.StandardError.ReadToEnd();

                process.WaitForExit();

                if (process.ExitCode != 0)
                {
                    var message = new BuildErrorEventArgs(string.Empty, string.Empty, string.Empty, 0, 0, 0, 0, string.Format("Error registering package '{0}'. ", packageIdentifier) + result + " Exit code = " + process.ExitCode.ToString() + ". You can try running Visual Studio as Administrator to install this component.", "", "", DateTime.Now);
                    this.BuildEngine.LogErrorEvent(message);

                    return;
                }
                else
                {
                    var message = new BuildMessageEventArgs(string.Format("'{0}' package registered successfully.  ", packageIdentifier) + result, "", "", MessageImportance.High);
                    this.BuildEngine.LogMessageEvent(message);
                }
            }
            else
            {
                // is vsx

                Action <DateTime, DirectoryInfo> checkOutdated = null;

                checkOutdated = new Action <DateTime, DirectoryInfo>((date, directory) =>
                {
                    foreach (var file in directory.GetFiles())
                    {
                        if (file.Extension != ".vsix" && file.Extension != ".zip")
                        {
                            if (DateTime.Compare(file.LastWriteTime, date) > 0)
                            {
                                outdated = true;
                                return;
                            }
                        }
                    }

                    foreach (var dir in directory.GetDirectories())
                    {
                        checkOutdated(date, dir);
                    }
                });

                // lets do another workaround due to Microsoft stupidity

                //var extensionPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), @"Microsoft\VisualStudio\10.0\Extensions\Cloud IDEaaS");
                //var folder = new DirectoryInfo(extensionPath);

                //Action<DirectoryInfo> deleteDeleteMeFiles = null;

                //deleteDeleteMeFiles = new Action<DirectoryInfo>(directory =>
                //{
                //    foreach (var file in directory.GetFiles("*.deleteme"))
                //    {
                //        try
                //        {
                //            file.Delete();
                //        }
                //        catch (Exception ex)
                //        {
                //            try
                //            {
                //                RestartExplorer();
                //                file.Delete();
                //            }
                //            catch
                //            {
                //                try
                //                {
                //                    var hydraCopyPath = Path.Combine(solutionPath, @"Binaries\SolutionLibraries\HydraCopy.exe");

                //                    var process = new Process();
                //                    var startInfo = new ProcessStartInfo
                //                    {
                //                        UseShellExecute = false,
                //                        RedirectStandardError = true,
                //                        RedirectStandardOutput = true,
                //                        CreateNoWindow = true,
                //                        FileName = hydraCopyPath,
                //                        Arguments = "\"" + file.FullName + "\" /delete"
                //                    };

                //                    process.StartInfo = startInfo;
                //                    process.Start();

                //                    process.WaitForExit();

                //                    if (process.ExitCode != 0)
                //                    {
                //                        throw new Exception(string.Format("HydraCopy.exe exited with error code = '{0}'", process.ExitCode));
                //                    }
                //                }
                //                catch (Exception ex2)
                //                {
                //                    var message2 = new BuildErrorEventArgs(string.Empty, string.Empty, string.Empty, 0, 0, 0, 0, string.Format("Error launching HydraCopy.exe to remove leftover files for package '{0}'. Error={1}. You can try rebuilding or restarting Visual Studio to remove these files.", packageIdentifier, ex2.ToString()), "", "", DateTime.Now);
                //                    this.BuildEngine.LogErrorEvent(message2);
                //                }

                //                var message = new BuildErrorEventArgs(string.Empty, string.Empty, string.Empty, 0, 0, 0, 0, string.Format("Error removing leftover files for package '{0}'. Error={1}. You can try rebuilding or restarting Visual Studio to remove these files.", packageIdentifier, ex.ToString()), "", "", DateTime.Now);
                //                this.BuildEngine.LogErrorEvent(message);
                //            }
                //        }
                //    }

                //    foreach (var dir in directory.GetDirectories())
                //    {
                //        deleteDeleteMeFiles(dir);
                //    }
                //});

                //if (folder.Exists)
                //{
                //    deleteDeleteMeFiles(folder);
                //}

                //if (Environment.Is64BitOperatingSystem)
                //{
                //    extensionPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), @"Microsoft Visual Studio 10.0\Common7\IDE\Extensions\Cloud IDEaaS");
                //}
                //else
                //{
                //    extensionPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), @"Microsoft Visual Studio 10.0\Common7\IDE\Extensions\Cloud IDEaaS");
                //}

                //folder = new DirectoryInfo(extensionPath);

                //if (folder.Exists)
                //{
                //    deleteDeleteMeFiles(folder);
                //}

                if (extensionManager.TryGetInstalledExtension(packageIdentifier, out installedExtension))
                {
                    checkOutdated(installedExtension.InstalledOn.DateTime, fileInfo.Directory);

                    if (!outdated)
                    {
                        return;
                    }
                }

                if (outdated)
                {
                    // see if there are templates in folder the VSIX installer will balk

                    fileInfo = new FileInfo(Path.Combine(fileInfo.DirectoryName, "extension.vsixmanifest"));

                    if (fileInfo.Exists)
                    {
                        var stream          = File.OpenRead(fileInfo.FullName);
                        var installLocation = Path.Combine(Environment.ExpandEnvironmentVariables("%LocalAppData%"), @"Microsoft\VisualStudio\10.0\Extensions");
                        var _namespace      = "vs";

                        stream.Seek(0, SeekOrigin.Begin);

                        var xPathDocument = new XPathDocument(stream);

                        stream.Close();

                        var nsmgr = new XmlNamespaceManager(new NameTable());
                        nsmgr.AddNamespace(_namespace, "http://schemas.microsoft.com/developer/vsx-schema/2010");

                        var navigator = xPathDocument.CreateNavigator();

                        var iter = navigator.Select(string.Format("{0}:Vsix/{0}:Identifier/{0}:Author", _namespace), nsmgr);
                        iter.MoveNext();

                        var author = iter.Current.Value;

                        iter = navigator.Select(string.Format("{0}:Vsix/{0}:Identifier/{0}:Name", _namespace), nsmgr);
                        iter.MoveNext();

                        var name = iter.Current.Value;

                        iter = navigator.Select(string.Format("{0}:Vsix/{0}:Identifier/{0}:Version", _namespace), nsmgr);
                        iter.MoveNext();

                        var version = iter.Current.Value;

                        installLocation = Path.Combine(installLocation, string.Format(@"{0}\{1}\{2}", author, name, version));

                        var dirInstall = new DirectoryInfo(installLocation);

                        if (dirInstall.Exists)
                        {
                            try
                            {
                                dirInstall.Delete(true);
                            }
                            catch
                            {
                                try
                                {
                                    RestartExplorer();
                                    dirInstall.Delete(true);
                                }
                                catch (Exception ex)
                                {
                                }
                            }
                        }
                    }

                    try
                    {
                        if (extensionManager.IsInstalled(installedExtension))
                        {
                            extensionManager.Uninstall(installedExtension);
                        }

                        var message = new BuildMessageEventArgs(string.Format("'{0}' package unregistered successfully.  ", packageIdentifier), "", "", MessageImportance.High);
                        this.BuildEngine.LogMessageEvent(message);
                    }
                    catch (Exception ex)
                    {
                        var message = new BuildErrorEventArgs(string.Empty, string.Empty, string.Empty, 0, 0, 0, 0, string.Format("Error unregistering package '{0}'. Error={1}", packageIdentifier, ex.ToString()), "", "", DateTime.Now);
                        this.BuildEngine.LogErrorEvent(message);
                    }
                }

                if (installedExtension == null || outdated)
                {
                    try
                    {
                        var debug = false;

                        if (debug)
                        {
                            var package = ZipPackage.Open(@"C:\Users\u164225\Documents\Visual Studio 2010\My Exported Templates\DummyProject.vsix");
                            var parts   = package.GetParts();

                            package.Close();

                            package = ZipPackage.Open(packagePath);
                            parts   = package.GetParts();

                            package.Close();

                            var installExtension2 = extensionManager.CreateInstallableExtension(@"C:\Users\u164225\Documents\Visual Studio 2010\My Exported Templates\DummyProject.vsix");
                            var reason2           = extensionManager.Install(installExtension2, false);
                            var message2          = new BuildMessageEventArgs(string.Format("'{0}' package registered successfully with restart reason={1}.  ", "DummyProject.fc1c3820-6d48-428d-963d-1f4bfa392f6b", reason2), "", "", MessageImportance.High);

                            Debugger.Break();
                        }

                        var installExtension = extensionManager.CreateInstallableExtension(packagePath);
                        var reason           = extensionManager.Install(installExtension, false);
                        var message          = new BuildMessageEventArgs(string.Format("'{0}' package registered successfully with restart reason={1}.  ", packageIdentifier, reason), "", "", MessageImportance.High);

                        this.BuildEngine.LogMessageEvent(message);
                    }
                    catch (Exception ex2)
                    {
                        try
                        {
                            RestartExplorer();

                            var installExtension = extensionManager.CreateInstallableExtension(packagePath);
                            var reason           = extensionManager.Install(installExtension, false);
                            var message          = new BuildMessageEventArgs(string.Format("'{0}' package registered successfully with restart reason={1}.  ", packageIdentifier, reason), "", "", MessageImportance.High);

                            this.BuildEngine.LogMessageEvent(message);
                        }
                        catch (Exception ex)
                        {
                            var message = new BuildErrorEventArgs(string.Empty, string.Empty, string.Empty, 0, 0, 0, 0, string.Format("Error registering package '{0}'. Error={1}", packageIdentifier, ex.ToString()), "", "", DateTime.Now);
                            this.BuildEngine.LogErrorEvent(message);
                        }
                    }
                }
            }
        }