private void OpenInteractiveWindow_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var view   = (EnvironmentView)e.Parameter;
            var config = view.Factory.Configuration;

            var replId = PythonReplEvaluatorProvider.GetEvaluatorId(config);

            var compModel = _site.GetComponentModel();
            var service   = compModel.GetService <InteractiveWindowProvider>();
            IVsInteractiveWindow window;

            // TODO: Figure out another way to get the project
            //var provider = _service.KnownProviders.OfType<LoadedProjectInterpreterFactoryProvider>().FirstOrDefault();
            //var vsProject = provider == null ?
            //    null :
            //    provider.GetProject(factory);
            //PythonProjectNode project = vsProject == null ? null : vsProject.GetPythonProject();
            try {
                window = service.OpenOrCreate(replId);
            } catch (Exception ex) when(!ex.IsCriticalException())
            {
                TaskDialog.ForException(_site, ex, Strings.ErrorOpeningInteractiveWindow, Strings.IssueTrackerUrl).ShowModal();
                return;
            }

            window?.Show(true);
        }
        private void SetIPythonEnabled(EnvironmentView view, bool enable)
        {
            var path = GetScriptPath(view);

            if (!EnsureScriptDirectory(path))
            {
                return;
            }

            try {
                path = PathUtils.GetAbsoluteFilePath(path, "mode.txt");
                if (enable)
                {
                    File.WriteAllText(path, Strings.ReplScriptPathIPythonModeTxtContents);
                }
                else
                {
                    if (File.Exists(path))
                    {
                        File.Delete(path);
                    }
                }
            } catch (Exception ex) when(!ex.IsCriticalException())
            {
                TaskDialog.ForException(_site, ex, issueTrackerUrl: Strings.IssueTrackerUrl).ShowModal();
            }
        }
        private void UnhandledException_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var ex = (ExceptionDispatchInfo)e.Parameter;

            Debug.Assert(ex != null, "Unhandled exception with no exception object");

            var td = TaskDialog.ForException(_site, ex.SourceException, string.Empty, PythonConstants.IssueTrackerUrl);

            td.Title = Strings.ProductTitle;
            td.ShowModal();
        }
        public void ProjectFinishedGenerating(Project project)
        {
            if (project.DTE.SuppressUI)
            {
                return;
            }

            ProjectItem requirementsTxt = null;

            try {
                requirementsTxt = project.ProjectItems.Item("requirements.txt");
            } catch (ArgumentException) {
            }

            if (requirementsTxt == null)
            {
                return;
            }

            var txt = requirementsTxt.FileNames[0];

            if (!File.Exists(txt))
            {
                return;
            }

            var provider = WizardHelpers.GetProvider(project.DTE);

            if (provider == null)
            {
                return;
            }

            try {
                object inObj = (object)txt, outObj = null;
                project.DTE.Commands.Raise(
                    GuidList.guidPythonToolsCmdSet.ToString("B"),
                    (int)PkgCmdIDList.cmdidInstallProjectRequirements,
                    ref inObj,
                    ref outObj
                    );
            } catch (Exception ex) {
                if (ex.IsCriticalException())
                {
                    throw;
                }
                TaskDialog.ForException(
                    provider,
                    ex,
                    Strings.InstallRequirementsFailed,
                    Strings.IssueTrackerUrl
                    ).ShowModal();
            }
        }
        private void UnhandledException_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var ex = (ExceptionDispatchInfo)e.Parameter;

            Debug.Assert(ex != null, "Unhandled exception with no exception object");
            if (ex.SourceException is PipException)
            {
                // Don't report Pip exceptions. The output messages have
                // already been handled.
                return;
            }

            var td = TaskDialog.ForException(_site, ex.SourceException, String.Empty, PythonConstants.IssueTrackerUrl);

            td.Title = SR.ProductName;
            td.ShowModal();
        }
        private void EnableIPythonInteractive_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var path = GetScriptPath(e.Parameter as EnvironmentView);

            if (!EnsureScriptDirectory(path))
            {
                return;
            }

            path = PathUtils.GetAbsoluteFilePath(path, "mode.txt");
            try {
                File.WriteAllText(path, Strings.ReplScriptPathIPythonModeTxtContents);
            } catch (Exception ex) when(!ex.IsCriticalException())
            {
                TaskDialog.ForException(_site, ex, issueTrackerUrl: PythonConstants.IssueTrackerUrl).ShowModal();
                return;
            }

            e.Handled = true;
        }
        private bool EnsureScriptDirectory(string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                return(false);
            }

            if (!Directory.Exists(path))
            {
                try {
                    Directory.CreateDirectory(path);
                    File.WriteAllText(PathUtils.GetAbsoluteFilePath(path, "readme.txt"), Strings.ReplScriptPathReadmeContents);
                } catch (Exception ex) when(!ex.IsCriticalException())
                {
                    TaskDialog.ForException(_site, ex, issueTrackerUrl: PythonConstants.IssueTrackerUrl).ShowModal();
                    return(false);
                }
            }
            return(true);
        }
        private void DisableIPythonInteractive_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var path = GetScriptPath(e.Parameter as EnvironmentView);

            if (!EnsureScriptDirectory(path))
            {
                return;
            }

            path = PathUtils.GetAbsoluteFilePath(path, "mode.txt");
            if (File.Exists(path))
            {
                try {
                    File.Delete(path);
                } catch (Exception ex) when(!ex.IsCriticalException())
                {
                    TaskDialog.ForException(_site, ex, issueTrackerUrl: PythonConstants.IssueTrackerUrl).ShowModal();
                    return;
                }
            }
        }
        private void OpenInCommandPrompt_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            try {
                var view = (EnvironmentView)e.Parameter;

                var paths   = GetPathEntries(view);
                var pathCmd = string.IsNullOrEmpty(paths) ? "" : string.Format("set PATH={0};%PATH% & ", paths);
                var psi     = new ProcessStartInfo(Path.Combine(Environment.SystemDirectory, "cmd.exe"))
                {
                    Arguments = string.Join(" ", new[] {
                        "/S",
                        "/K",
                        pathCmd + string.Format("title {0} environment", view.Description)
                    }.Select(ProcessOutput.QuoteSingleArgument)),
                    WorkingDirectory = view.PrefixPath
                };

                Process.Start(psi)?.Dispose();
            } catch (Exception ex) when(!ex.IsCriticalException())
            {
                TaskDialog.ForException(_site, ex, Strings.ErrorOpeningCommandPrompt, Strings.IssueTrackerUrl).ShowModal();
                return;
            }
        }
        public void ProjectFinishedGenerating(Project project)
        {
            if (project.DTE.SuppressUI)
            {
                return;
            }

            string description = null;

            try {
                var p = project.Properties.Item("InterpreterDescription");
                if (p != null)
                {
                    description = p.Value as string;
                }
            } catch (ArgumentException) {
            }

            if (string.IsNullOrEmpty(description))
            {
                bool isValid = false;
                try {
                    var p = project.Properties.Item("InterpreterId");
                    isValid = p != null && !string.IsNullOrEmpty(p.Value as string);
                } catch (ArgumentException) {
                }
                if (!isValid)
                {
                    // We don't have a usable interpreter, so there's no point
                    // going ahead.
                    // Fall out - the user will find what they need when they
                    // try and run or edit, but until then there's no reason to
                    // block them or duplicate all of our help messages into yet
                    // another assembly.
                    return;
                }
            }

            ProjectItem requirementsTxt = null;

            try {
                requirementsTxt = project.ProjectItems.Item("requirements.txt");
            } catch (ArgumentException) {
            }

            if (requirementsTxt == null)
            {
                return;
            }

            var txt = requirementsTxt.FileNames[0];

            if (!File.Exists(txt))
            {
                return;
            }

            var provider = WizardHelpers.GetProvider(project.DTE);

            if (provider == null)
            {
                return;
            }

            var td = new TaskDialog(provider)
            {
                Title             = string.Format("{0} - {1}", project.Name, Resources.PythonToolsForVisualStudio),
                MainInstruction   = Resources.InstallRequirementsHeading,
                Content           = Resources.InstallRequirementsMessage,
                EnableHyperlinks  = true,
                AllowCancellation = true,
            };

            var venv = new TaskDialogButton(
                Resources.InstallRequirementsIntoVirtualEnv,
                Resources.InstallRequirementsIntoVirtualEnvTip
                );

            description = description ?? Resources.DefaultInterpreterDescription;
            var install = new TaskDialogButton(
                string.Format(Resources.InstallRequirementsIntoGlobalEnv, description),
                Resources.InstallRequirementsIntoGlobalEnvTip
                );
            var goAway = new TaskDialogButton(Resources.InstallRequirementsNowhere);

            td.Buttons.Add(venv);
            td.Buttons.Add(install);
            td.Buttons.Add(goAway);

            try {
                td.ExpandedInformation  = File.ReadAllText(txt);
                td.CollapsedControlText = Resources.InstallRequirementsShowPackages;
                td.ExpandedControlText  = Resources.InstallRequirementsHidePackages;
            } catch (IOException) {
            } catch (NotSupportedException) {
            } catch (UnauthorizedAccessException) {
            }

            var btn   = td.ShowModal();
            int cmdId = 0;

            if (btn == venv)
            {
                cmdId = (int)PkgCmdIDList.cmdidAddVirtualEnv;
            }
            else if (btn == install)
            {
                cmdId = (int)PkgCmdIDList.cmdidInstallRequirementsTxt;
            }
            if (cmdId != 0)
            {
                object inObj = (object)true, outObj = null;
                try {
                    project.DTE.Commands.Raise(
                        GuidList.guidPythonToolsCmdSet.ToString("B"),
                        cmdId,
                        ref inObj,
                        ref outObj
                        );
                } catch (Exception ex) {
                    if (ex.IsCriticalException())
                    {
                        throw;
                    }
                    TaskDialog.ForException(
                        provider,
                        ex,
                        Resources.InstallRequirementsFailed,
                        PythonConstants.IssueTrackerUrl
                        ).ShowModal();
                }
            }
        }