Skip to content

Commit b63979d

Browse files
Add RunCode command for CodeLens providers (#1928)
* Add RunCode command for CodeLens providers * pulled out createLaunchConfig and added test * move cwd logic
1 parent 81d9e36 commit b63979d

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed

src/features/RunCode.ts

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
}

src/main.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { NewFileOrProjectFeature } from "./features/NewFileOrProject";
2828
import { OpenInISEFeature } from "./features/OpenInISE";
2929
import { PesterTestsFeature } from "./features/PesterTests";
3030
import { RemoteFilesFeature } from "./features/RemoteFiles";
31+
import { RunCodeFeature } from "./features/RunCode";
3132
import { SelectPSSARulesFeature } from "./features/SelectPSSARules";
3233
import { ShowHelpFeature } from "./features/ShowHelp";
3334
import { Logger, LogLevel } from "./logging";
@@ -149,6 +150,7 @@ export function activate(context: vscode.ExtensionContext): void {
149150
new ShowHelpFeature(logger),
150151
new FindModuleFeature(),
151152
new PesterTestsFeature(sessionManager),
153+
new RunCodeFeature(sessionManager),
152154
new ExtensionCommandsFeature(logger),
153155
new SelectPSSARulesFeature(logger),
154156
new CodeActionsFeature(logger),

test/features/RunCode.test.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*---------------------------------------------------------
2+
* Copyright (C) Microsoft Corporation. All rights reserved.
3+
*--------------------------------------------------------*/
4+
5+
import * as assert from "assert";
6+
import rewire = require("rewire");
7+
import vscode = require("vscode");
8+
9+
// Setup function that is not exported.
10+
const customViews = rewire("../../src/features/RunCode");
11+
const createLaunchConfig = customViews.__get__("createLaunchConfig");
12+
13+
enum LaunchType {
14+
Debug,
15+
Run,
16+
}
17+
18+
suite("RunCode tests", () => {
19+
test("Can create the launch config", () => {
20+
const commandToRun: string = "Invoke-Build";
21+
const args: string[] = ["Clean"];
22+
23+
const expected: object = {
24+
request: "launch",
25+
type: "PowerShell",
26+
name: "PowerShell Run Code",
27+
script: commandToRun,
28+
args,
29+
internalConsoleOptions: "neverOpen",
30+
noDebug: false,
31+
createTemporaryIntegratedConsole: false,
32+
cwd: vscode.workspace.rootPath,
33+
};
34+
35+
const actual: object = createLaunchConfig(LaunchType.Debug, commandToRun, args);
36+
37+
assert.deepEqual(actual, expected);
38+
});
39+
});

0 commit comments

Comments
 (0)