|
| 1 | +/*--------------------------------------------------------- |
| 2 | + * Copyright (C) Microsoft Corporation. All rights reserved. |
| 3 | + *--------------------------------------------------------*/ |
| 4 | + |
| 5 | +import * as path from "path"; |
| 6 | +import vscode = require("vscode"); |
| 7 | +import { IFeature, LanguageClient } from "../feature"; |
| 8 | +import { SessionManager } from "../session"; |
| 9 | +import Settings = require("../settings"); |
| 10 | +import utils = require("../utils"); |
| 11 | + |
| 12 | +enum LaunchType { |
| 13 | + Debug, |
| 14 | + Run, |
| 15 | +} |
| 16 | + |
| 17 | +export class RunCodeFeature implements IFeature { |
| 18 | + |
| 19 | + private command: vscode.Disposable; |
| 20 | + private languageClient: LanguageClient; |
| 21 | + |
| 22 | + constructor(private sessionManager: SessionManager) { |
| 23 | + this.command = vscode.commands.registerCommand( |
| 24 | + "PowerShell.RunCode", |
| 25 | + (runInDebugger: boolean, scriptToRun: string, args: string[]) => { |
| 26 | + this.launchTask(runInDebugger, scriptToRun, args); |
| 27 | + }); |
| 28 | + } |
| 29 | + |
| 30 | + public dispose() { |
| 31 | + this.command.dispose(); |
| 32 | + } |
| 33 | + |
| 34 | + public setLanguageClient(languageClient: LanguageClient) { |
| 35 | + this.languageClient = languageClient; |
| 36 | + } |
| 37 | + |
| 38 | + private async launchTask( |
| 39 | + runInDebugger: boolean, |
| 40 | + scriptToRun: string, |
| 41 | + args: string[]) { |
| 42 | + |
| 43 | + const launchType = runInDebugger ? LaunchType.Debug : LaunchType.Run; |
| 44 | + const launchConfig = createLaunchConfig(launchType, scriptToRun, args); |
| 45 | + this.launch(launchConfig); |
| 46 | + } |
| 47 | + |
| 48 | + private launch(launchConfig) { |
| 49 | + // Create or show the interactive console |
| 50 | + // TODO #367: Check if "newSession" mode is configured |
| 51 | + vscode.commands.executeCommand("PowerShell.ShowSessionConsole", true); |
| 52 | + |
| 53 | + // Write out temporary debug session file |
| 54 | + utils.writeSessionFile( |
| 55 | + utils.getDebugSessionFilePath(), |
| 56 | + this.sessionManager.getSessionDetails()); |
| 57 | + |
| 58 | + // TODO: Update to handle multiple root workspaces. |
| 59 | + vscode.debug.startDebugging(vscode.workspace.workspaceFolders[0], launchConfig); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +function createLaunchConfig(launchType: LaunchType, commandToRun: string, args: string[]) { |
| 64 | + const settings = Settings.load(); |
| 65 | + |
| 66 | + let cwd: string = vscode.workspace.rootPath; |
| 67 | + if (vscode.window.activeTextEditor |
| 68 | + && vscode.window.activeTextEditor.document |
| 69 | + && !vscode.window.activeTextEditor.document.isUntitled) { |
| 70 | + cwd = path.dirname(vscode.window.activeTextEditor.document.fileName); |
| 71 | + } |
| 72 | + |
| 73 | + const launchConfig = { |
| 74 | + request: "launch", |
| 75 | + type: "PowerShell", |
| 76 | + name: "PowerShell Run Code", |
| 77 | + internalConsoleOptions: "neverOpen", |
| 78 | + noDebug: (launchType === LaunchType.Run), |
| 79 | + createTemporaryIntegratedConsole: settings.debugging.createTemporaryIntegratedConsole, |
| 80 | + script: commandToRun, |
| 81 | + args, |
| 82 | + cwd, |
| 83 | + }; |
| 84 | + |
| 85 | + return launchConfig; |
| 86 | +} |
0 commit comments