Exemple #1
0
        public int LaunchProject(bool debug)
        {
            var startInfo = GetStartInfo(debug);
            var props     = PythonProjectLaunchProperties.Create(_project, _serviceProvider, null);

            if (props.GetIsWindowsApplication() ?? false)
            {
                // Must run hidden if running with pythonw
                startInfo.ExecuteIn = "hidden";
            }

            var env = startInfo.EnvironmentVariables;

            if (env == null)
            {
                env = startInfo.EnvironmentVariables = new Dictionary <string, string>();
            }

            string value;

            if (!env.TryGetValue("SERVER_HOST", out value) || string.IsNullOrEmpty(value))
            {
                env["SERVER_HOST"] = "localhost";
            }
            int dummyInt;

            if (!env.TryGetValue("SERVER_PORT", out value) ||
                string.IsNullOrEmpty(value) ||
                !int.TryParse(value, out dummyInt))
            {
                env["SERVER_PORT"] = TestServerPortString;
            }

            if (debug)
            {
                _pyService.Logger.LogEvent(Logging.PythonLogEvent.Launch, 1);

                using (var dsi = CreateDebugTargetInfo(startInfo, props)) {
                    dsi.Launch(_serviceProvider);
                }
            }
            else
            {
                _pyService.Logger.LogEvent(Logging.PythonLogEvent.Launch, 0);

                var psi = CreateProcessStartInfo(startInfo, props);

                var process = Process.Start(psi);
                if (process != null)
                {
                    StartBrowser(GetFullUrl(), () => process.HasExited);
                }
            }

            return(VSConstants.S_OK);
        }
Exemple #2
0
        public int LaunchFile(string file, bool debug, IProjectLaunchProperties props)
        {
            var startInfo = GetStartInfo(debug, runUnknownCommands: false);

            return(new DefaultPythonLauncher(_serviceProvider, _pyService, _project).LaunchFile(
                       file,
                       debug,
                       props == null ? startInfo : PythonProjectLaunchProperties.Merge(props, startInfo)
                       ));
        }
Exemple #3
0
        private ProcessStartInfo CreateProcessStartInfo(
            CommandStartInfo startInfo,
            IPythonProjectLaunchProperties props
            )
        {
            bool alwaysPause = startInfo.ExecuteInConsoleAndPause;

            // We only want to run the webserver in a console.
            startInfo.ExecuteIn = "console";
            startInfo.AdjustArgumentsForProcessStartInfo(props.GetInterpreterPath(), handleConsoleAndPause: false);

            var psi = new ProcessStartInfo {
                FileName         = startInfo.Filename,
                Arguments        = startInfo.Arguments,
                WorkingDirectory = startInfo.WorkingDirectory,
                UseShellExecute  = false
            };

            var env = new Dictionary <string, string>(startInfo.EnvironmentVariables, StringComparer.OrdinalIgnoreCase);

            PythonProjectLaunchProperties.MergeEnvironmentBelow(env, new Dictionary <string, string> {
                { "SERVER_HOST", "localhost" },
                { "SERVER_PORT", TestServerPortString }
            }, true);

            foreach (var kv in env)
            {
                psi.EnvironmentVariables[kv.Key] = kv.Value;
            }

            // Pause if the user has requested it.
            string pauseCommand = null;

            if (alwaysPause ||
                _pyService.DebuggerOptions.WaitOnAbnormalExit &&
                _pyService.DebuggerOptions.WaitOnNormalExit)
            {
                pauseCommand = "pause";
            }
            else if (_pyService.DebuggerOptions.WaitOnAbnormalExit &&
                     !_pyService.DebuggerOptions.WaitOnNormalExit)
            {
                pauseCommand = "if errorlevel 1 pause";
            }
            else if (_pyService.DebuggerOptions.WaitOnNormalExit &&
                     !_pyService.DebuggerOptions.WaitOnAbnormalExit)
            {
                pauseCommand = "if not errorlevel 1 pause";
            }
            if (!string.IsNullOrEmpty(pauseCommand))
            {
                psi.Arguments = string.Format("/c \"{0} {1}\" & {2}",
                                              ProcessOutput.QuoteSingleArgument(psi.FileName),
                                              psi.Arguments,
                                              pauseCommand
                                              );
                psi.FileName = Path.Combine(Environment.SystemDirectory, "cmd.exe");
            }

            return(psi);
        }