-
Notifications
You must be signed in to change notification settings - Fork 511
/
Copy pathRunCode.ts
74 lines (60 loc) · 2.32 KB
/
RunCode.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import * as path from "path";
import vscode = require("vscode");
import { SessionManager } from "../session";
import Settings = require("../settings");
import utils = require("../utils");
enum LaunchType {
Debug,
Run,
}
export class RunCodeFeature implements vscode.Disposable {
private command: vscode.Disposable;
constructor(private sessionManager: SessionManager) {
this.command = vscode.commands.registerCommand(
"PowerShell.RunCode",
(runInDebugger: boolean, scriptToRun: string, args: string[]) => {
this.launchTask(runInDebugger, scriptToRun, args);
});
}
public dispose() {
this.command.dispose();
}
private async launchTask(
runInDebugger: boolean,
scriptToRun: string,
args: string[]) {
const launchType = runInDebugger ? LaunchType.Debug : LaunchType.Run;
const launchConfig = createLaunchConfig(launchType, scriptToRun, args);
await this.launch(launchConfig);
}
private async launch(launchConfig: string | vscode.DebugConfiguration) {
// Create or show the interactive console
// TODO: #367: Check if "newSession" mode is configured
await vscode.commands.executeCommand("PowerShell.ShowSessionConsole", true);
// TODO: Update to handle multiple root workspaces.
await vscode.debug.startDebugging(vscode.workspace.workspaceFolders?.[0], launchConfig);
}
}
function createLaunchConfig(launchType: LaunchType, commandToRun: string, args: string[]) {
const settings = Settings.load();
let cwd: string = vscode.workspace.rootPath;
if (vscode.window.activeTextEditor
&& vscode.window.activeTextEditor.document
&& !vscode.window.activeTextEditor.document.isUntitled) {
cwd = path.dirname(vscode.window.activeTextEditor.document.fileName);
}
const launchConfig = {
request: "launch",
type: "PowerShell",
name: "PowerShell Run Code",
internalConsoleOptions: "neverOpen",
noDebug: (launchType === LaunchType.Run),
createTemporaryIntegratedConsole: settings.debugging.createTemporaryIntegratedConsole,
script: commandToRun,
args,
cwd,
};
return launchConfig;
}