protected async Task HandleLaunchRequest(
            LaunchRequestArguments launchParams,
            EditorSession editorSession,
            RequestContext <object, object> requestContext)
        {
            // Execute the given PowerShell script and send the response.
            // Note that we aren't waiting for execution to complete here
            // because the debugger could stop while the script executes.
            editorSession.PowerShellContext
            .ExecuteScriptAtPath(launchParams.Program)
            .ContinueWith(
                async(t) =>
            {
                Logger.Write(LogLevel.Verbose, "Execution completed, terminating...");

                await requestContext.SendEvent(
                    TerminatedEvent.Type,
                    null);

                // TODO: Find a way to exit more gracefully!
                Environment.Exit(0);
            });

            await requestContext.SendResult(null);
        }
        protected async Task HandleLaunchRequest(
            LaunchRequestArguments launchParams,
            RequestContext <object> requestContext)
        {
            // Set the working directory for the PowerShell runspace to the cwd passed in via launch.json.
            // In case that is null, use the the folder of the script to be executed.  If the resulting
            // working dir path is a file path then extract the directory and use that.
            string workingDir = launchParams.Cwd ?? launchParams.Program;

            try
            {
                if ((File.GetAttributes(workingDir) & FileAttributes.Directory) != FileAttributes.Directory)
                {
                    workingDir = Path.GetDirectoryName(workingDir);
                }
            }
            catch (Exception ex)
            {
                Logger.Write(LogLevel.Error, "cwd path is bad: " + ex.Message);
                workingDir = Environment.CurrentDirectory;
            }

            var setWorkingDirCommand = new PSCommand();

            setWorkingDirCommand.AddCommand(@"Microsoft.PowerShell.Management\Set-Location")
            .AddParameter("LiteralPath", workingDir);

            await editorSession.PowerShellContext.ExecuteCommand(setWorkingDirCommand);

            Logger.Write(LogLevel.Verbose, "Working dir set to: " + workingDir);

            // Prepare arguments to the script - if specified
            string arguments = null;

            if ((launchParams.Args != null) && (launchParams.Args.Length > 0))
            {
                arguments = string.Join(" ", launchParams.Args);
                Logger.Write(LogLevel.Verbose, "Script arguments are: " + arguments);
            }

            // Execute the given PowerShell script and send the response.
            // Note that we aren't waiting for execution to complete here
            // because the debugger could stop while the script executes.
            Task executeTask =
                editorSession.PowerShellContext
                .ExecuteScriptAtPath(launchParams.Program, arguments)
                .ContinueWith(
                    async(t) => {
                Logger.Write(LogLevel.Verbose, "Execution completed, terminating...");

                await requestContext.SendEvent(
                    TerminatedEvent.Type,
                    null);

                // Stop the server
                this.Stop();
            });

            await requestContext.SendResult(null);
        }
Beispiel #3
0
        protected async Task HandleLaunchRequest(
            LaunchRequestArguments launchParams,
            RequestContext <object> requestContext)
        {
            // Set the working directory for the PowerShell runspace to the cwd passed in via launch.json.
            // In case that is null, use the the folder of the script to be executed.  If the resulting
            // working dir path is a file path then extract the directory and use that.
            string workingDir = launchParams.Cwd ?? launchParams.Program;

            workingDir = PowerShellContext.UnescapePath(workingDir);
            try
            {
                if ((File.GetAttributes(workingDir) & FileAttributes.Directory) != FileAttributes.Directory)
                {
                    workingDir = Path.GetDirectoryName(workingDir);
                }
            }
            catch (Exception ex)
            {
                Logger.Write(LogLevel.Error, "cwd path is invalid: " + ex.Message);
                workingDir = Environment.CurrentDirectory;
            }

            editorSession.PowerShellContext.SetWorkingDirectory(workingDir);
            Logger.Write(LogLevel.Verbose, "Working dir set to: " + workingDir);

            // Prepare arguments to the script - if specified
            string arguments = null;

            if ((launchParams.Args != null) && (launchParams.Args.Length > 0))
            {
                arguments = string.Join(" ", launchParams.Args);
                Logger.Write(LogLevel.Verbose, "Script arguments are: " + arguments);
            }

            // We may not actually launch the script in response to this
            // request unless it comes after the configurationDone request.
            // If the launch request comes first, then stash the launch
            // params so that the subsequent configurationDone request handler
            // can launch the script.
            this.noDebug            = launchParams.NoDebug;
            this.scriptPathToLaunch = launchParams.Program;
            this.arguments          = arguments;

            // The order of debug protocol messages apparently isn't as guaranteed as we might like.
            // Need to be able to handle the case where we get the launch request after the
            // configurationDone request.
            if (this.isConfigurationDoneRequestComplete)
            {
                this.LaunchScript(requestContext);
            }

            this.isLaunchRequestComplete = true;

            await requestContext.SendResult(null);
        }
        protected async Task HandleLaunchRequestAsync(
            LaunchRequestArguments launchParams,
            RequestContext <object> requestContext)
        {
            RegisterEventHandlers();

            // Determine whether or not the working directory should be set in the PowerShellContext.
            if ((_editorSession.PowerShellContext.CurrentRunspace.Location == RunspaceLocation.Local) &&
                !_editorSession.DebugService.IsDebuggerStopped)
            {
                // Get the working directory that was passed via the debug config
                // (either via launch.json or generated via no-config debug).
                string workingDir = launchParams.Cwd;

                // Assuming we have a non-empty/null working dir, unescape the path and verify
                // the path exists and is a directory.
                if (!string.IsNullOrEmpty(workingDir))
                {
                    try
                    {
                        if ((File.GetAttributes(workingDir) & FileAttributes.Directory) != FileAttributes.Directory)
                        {
                            workingDir = Path.GetDirectoryName(workingDir);
                        }
                    }
                    catch (Exception ex)
                    {
                        workingDir = null;
                        Logger.Write(
                            LogLevel.Error,
                            $"The specified 'cwd' path is invalid: '{launchParams.Cwd}'. Error: {ex.Message}");
                    }
                }

                // If we have no working dir by this point and we are running in a temp console,
                // pick some reasonable default.
                if (string.IsNullOrEmpty(workingDir) && launchParams.CreateTemporaryIntegratedConsole)
                {
                    workingDir = Environment.CurrentDirectory;
                }

                // At this point, we will either have a working dir that should be set to cwd in
                // the PowerShellContext or the user has requested (via an empty/null cwd) that
                // the working dir should not be changed.
                if (!string.IsNullOrEmpty(workingDir))
                {
                    await _editorSession.PowerShellContext.SetWorkingDirectoryAsync(workingDir, isPathAlreadyEscaped : false);
                }

                Logger.Write(LogLevel.Verbose, $"Working dir " + (string.IsNullOrEmpty(workingDir) ? "not set." : $"set to '{workingDir}'"));
            }

            // Prepare arguments to the script - if specified
            string arguments = null;

            if ((launchParams.Args != null) && (launchParams.Args.Length > 0))
            {
                arguments = string.Join(" ", launchParams.Args);
                Logger.Write(LogLevel.Verbose, "Script arguments are: " + arguments);
            }

            // Store the launch parameters so that they can be used later
            _noDebug        = launchParams.NoDebug;
            _scriptToLaunch = launchParams.Script;
            _arguments      = arguments;
            IsUsingTempIntegratedConsole = launchParams.CreateTemporaryIntegratedConsole;

            // If the current session is remote, map the script path to the remote
            // machine if necessary
            if (_scriptToLaunch != null &&
                _editorSession.PowerShellContext.CurrentRunspace.Location == RunspaceLocation.Remote)
            {
                _scriptToLaunch =
                    _editorSession.RemoteFileManager.GetMappedPath(
                        _scriptToLaunch,
                        _editorSession.PowerShellContext.CurrentRunspace);
            }

            await requestContext.SendResultAsync(null);

            // If no script is being launched, mark this as an interactive
            // debugging session
            _isInteractiveDebugSession = string.IsNullOrEmpty(_scriptToLaunch);

            // Send the InitializedEvent so that the debugger will continue
            // sending configuration requests
            await _messageSender.SendEventAsync(
                InitializedEvent.Type,
                null);
        }
        protected async Task HandleLaunchRequest(
            LaunchRequestArguments launchParams,
            RequestContext <object> requestContext)
        {
            this.RegisterEventHandlers();

            // Set the working directory for the PowerShell runspace to the cwd passed in via launch.json.
            // In case that is null, use the the folder of the script to be executed.  If the resulting
            // working dir path is a file path then extract the directory and use that.
            string workingDir =
                launchParams.Cwd ??
                launchParams.Script ??
#pragma warning disable 618
                launchParams.Program;

#pragma warning restore 618

            // When debugging an "untitled" (unsaved) file - the working dir can't be derived
            // from the Script path.  OTOH, if the launch params specifies a Cwd, use it.
            if (ScriptFile.IsUntitledPath(workingDir) && string.IsNullOrEmpty(launchParams.Cwd))
            {
                workingDir = null;
            }

            if (!string.IsNullOrEmpty(workingDir))
            {
                workingDir = PowerShellContext.UnescapePath(workingDir);
                try
                {
                    if ((File.GetAttributes(workingDir) & FileAttributes.Directory) != FileAttributes.Directory)
                    {
                        workingDir = Path.GetDirectoryName(workingDir);
                    }
                }
                catch (Exception ex)
                {
                    Logger.Write(LogLevel.Error, "cwd path is invalid: " + ex.Message);

                    workingDir = null;
                }
            }

            if (string.IsNullOrEmpty(workingDir))
            {
#if CoreCLR
                workingDir = AppContext.BaseDirectory;
#else
                workingDir = Environment.CurrentDirectory;
#endif
            }

            if (this.editorSession.PowerShellContext.CurrentRunspace.Location == RunspaceLocation.Local &&
                !this.editorSession.DebugService.IsDebuggerStopped)
            {
                await editorSession.PowerShellContext.SetWorkingDirectory(workingDir, isPathAlreadyEscaped : false);

                Logger.Write(LogLevel.Verbose, "Working dir set to: " + workingDir);
            }

            // Prepare arguments to the script - if specified
            string arguments = null;
            if ((launchParams.Args != null) && (launchParams.Args.Length > 0))
            {
                arguments = string.Join(" ", launchParams.Args);
                Logger.Write(LogLevel.Verbose, "Script arguments are: " + arguments);
            }

            // Store the launch parameters so that they can be used later
            this.noDebug = launchParams.NoDebug;
#pragma warning disable 618
            this.scriptToLaunch = launchParams.Script ?? launchParams.Program;
#pragma warning restore 618
            this.arguments = arguments;
            this.IsUsingTempIntegratedConsole = launchParams.CreateTemporaryIntegratedConsole;

            // If the current session is remote, map the script path to the remote
            // machine if necessary
            if (this.scriptToLaunch != null &&
                this.editorSession.PowerShellContext.CurrentRunspace.Location == RunspaceLocation.Remote)
            {
                this.scriptToLaunch =
                    this.editorSession.RemoteFileManager.GetMappedPath(
                        this.scriptToLaunch,
                        this.editorSession.PowerShellContext.CurrentRunspace);
            }

            await requestContext.SendResult(null);

            // If no script is being launched, mark this as an interactive
            // debugging session
            this.isInteractiveDebugSession = string.IsNullOrEmpty(this.scriptToLaunch);

            // Send the InitializedEvent so that the debugger will continue
            // sending configuration requests
            await this.messageSender.SendEvent(
                InitializedEvent.Type,
                null);
        }
Beispiel #6
0
        protected async Task HandleLaunchRequest(
            LaunchRequestArguments launchParams,
            RequestContext <object> requestContext)
        {
            // Set the working directory for the PowerShell runspace to the cwd passed in via launch.json.
            // In case that is null, use the the folder of the script to be executed.  If the resulting
            // working dir path is a file path then extract the directory and use that.
            string workingDir =
                launchParams.Cwd ??
                launchParams.Script ??
                launchParams.Program;

            if (workingDir != null)
            {
                workingDir = PowerShellContext.UnescapePath(workingDir);
                try
                {
                    if ((File.GetAttributes(workingDir) & FileAttributes.Directory) != FileAttributes.Directory)
                    {
                        workingDir = Path.GetDirectoryName(workingDir);
                    }
                }
                catch (Exception ex)
                {
                    Logger.Write(LogLevel.Error, "cwd path is invalid: " + ex.Message);

                    workingDir = null;
                }
            }

            if (workingDir == null)
            {
#if NanoServer
                workingDir = AppContext.BaseDirectory;
#else
                workingDir = Environment.CurrentDirectory;
#endif
            }

            editorSession.PowerShellContext.SetWorkingDirectory(workingDir);
            Logger.Write(LogLevel.Verbose, "Working dir set to: " + workingDir);

            // Prepare arguments to the script - if specified
            string arguments = null;
            if ((launchParams.Args != null) && (launchParams.Args.Length > 0))
            {
                arguments = string.Join(" ", launchParams.Args);
                Logger.Write(LogLevel.Verbose, "Script arguments are: " + arguments);
            }

            // Store the launch parameters so that they can be used later
            this.noDebug            = launchParams.NoDebug;
            this.scriptPathToLaunch = launchParams.Script ?? launchParams.Program;
            this.arguments          = arguments;

            await requestContext.SendResult(null);

            // If no script is being launched, execute an empty script to
            // cause the prompt string to be evaluated and displayed
            if (string.IsNullOrEmpty(this.scriptPathToLaunch))
            {
                await this.editorSession.PowerShellContext.ExecuteScriptString(
                    "", false, true);
            }

            // Send the InitializedEvent so that the debugger will continue
            // sending configuration requests
            await requestContext.SendEvent(
                InitializedEvent.Type,
                null);
        }