private async Task <AddExistingEnvironmentView> AutoDetectAsync(AddExistingEnvironmentView view)
        {
            if (!Directory.Exists(view.PrefixPath))
            {
                // If view.PrefixPath is not valid by this point, we can't find anything
                // else, so abort without changes.
                return(view);
            }

            if (string.IsNullOrEmpty(view.Description))
            {
                view.Description = PathUtils.GetFileOrDirectoryName(view.PrefixPath);
            }

            if (!File.Exists(view.InterpreterPath))
            {
                view.InterpreterPath = PathUtils.FindFile(
                    view.PrefixPath,
                    CPythonInterpreterFactoryConstants.ConsoleExecutable,
                    firstCheck: new[] { "scripts" }
                    );
            }

            if (!File.Exists(view.WindowsInterpreterPath))
            {
                view.WindowsInterpreterPath = PathUtils.FindFile(
                    view.PrefixPath,
                    CPythonInterpreterFactoryConstants.WindowsExecutable,
                    firstCheck: new[] { "scripts" }
                    );
            }

            if (File.Exists(view.InterpreterPath))
            {
                using (var output = ProcessOutput.RunHiddenAndCapture(
                           view.InterpreterPath, "-c", "import sys; print('%s.%s' % (sys.version_info[0], sys.version_info[1])); print(sys.platform)"
                           )) {
                    var exitCode = await output;
                    if (exitCode == 0)
                    {
                        view.VersionName = output.StandardOutputLines.FirstOrDefault() ?? view.VersionName;
                    }
                }

                var arch = CPythonInterpreterFactoryProvider.ArchitectureFromExe(view.InterpreterPath);
                if (arch != InterpreterArchitecture.Unknown)
                {
                    view.ArchitectureName = arch.ToString();
                }

                if (string.IsNullOrEmpty(view.PathEnvironmentVariable))
                {
                    view.PathEnvironmentVariable = "PYTHONPATH";
                }
            }

            return(view);
        }
        private static async Task ShowDialogAsync(
            PageKind activePage,
            IServiceProvider site,
            PythonProjectNode project,
            string existingCondaEnvName = null,
            string environmentYmlPath   = null,
            string requirementsTxtPath  = null,
            CancellationToken ct        = default(CancellationToken)
            )
        {
            if (site == null)
            {
                throw new ArgumentNullException(nameof(site));
            }

            ProjectView[] projectViews;
            ProjectView   selectedProjectView;

            try {
                var sln      = (IVsSolution)site.GetService(typeof(SVsSolution));
                var projects = sln?.EnumerateLoadedPythonProjects().ToArray() ?? new PythonProjectNode[0];

                projectViews = projects
                               .Select((projectNode) => new ProjectView(projectNode))
                               .ToArray();

                selectedProjectView = projectViews.SingleOrDefault(pv => pv.Node == project);
            } catch (InvalidOperationException ex) {
                Debug.Fail(ex.ToUnhandledExceptionMessage(typeof(AddEnvironmentDialog)));
                projectViews        = new ProjectView[0];
                selectedProjectView = null;
            }

            if (selectedProjectView != null)
            {
                if (existingCondaEnvName != null)
                {
                    selectedProjectView.MissingCondaEnvName = existingCondaEnvName;
                }

                if (environmentYmlPath != null)
                {
                    selectedProjectView.EnvironmentYmlPath = environmentYmlPath;
                }

                if (requirementsTxtPath != null)
                {
                    selectedProjectView.RequirementsTxtPath = requirementsTxtPath;
                }
            }

            var addVirtualView = new AddVirtualEnvironmentView(
                site,
                projectViews,
                selectedProjectView
                );

            var addCondaView = new AddCondaEnvironmentView(
                site,
                projectViews,
                selectedProjectView
                );

            var addExistingView = new AddExistingEnvironmentView(
                site,
                projectViews,
                selectedProjectView
                );

            var addInstalledView = new AddInstalledEnvironmentView(
                site,
                projectViews,
                selectedProjectView
                );

            EnvironmentViewBase activeView;

            switch (activePage)
            {
            case PageKind.VirtualEnvironment:
                activeView = addVirtualView;
                break;

            case PageKind.CondaEnvironment:
                activeView = addCondaView;
                break;

            case PageKind.ExistingEnvironment:
                activeView = addExistingView;
                break;

            case PageKind.InstalledEnvironment:
                activeView = addInstalledView;
                break;

            default:
                Debug.Assert(false, string.Format("Unknown page kind '{0}'", activePage));
                activeView = null;
                break;
            }

            using (var dlg = new AddEnvironmentDialog(
                       new EnvironmentViewBase[] {
                addVirtualView,
                addCondaView,
                addExistingView,
                addInstalledView,
            },
                       activeView
                       )) {
                try {
                    WindowHelper.ShowModal(dlg);
                } catch (Exception) {
                    dlg.Close();
                    throw;
                }

                if (dlg.DialogResult ?? false)
                {
                    var view = dlg.View.PagesView.CurrentItem as EnvironmentViewBase;
                    Debug.Assert(view != null);
                    if (view != null)
                    {
                        try {
                            await view.ApplyAsync();
                        } catch (Exception ex) when(!ex.IsCriticalException())
                        {
                            Debug.Fail(ex.ToUnhandledExceptionMessage(typeof(AddEnvironmentDialog)), Strings.ProductTitle);
                        }
                    }
                }
            }
        }