Skip to content

Add RunCode command for CodeLens providers #1928

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions src/features/RunCode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/

import * as path from "path";
import vscode = require("vscode");
import { IFeature, LanguageClient } from "../feature";
import { SessionManager } from "../session";
import Settings = require("../settings");
import utils = require("../utils");

enum LaunchType {
Debug,
Run,
}

export class RunCodeFeature implements IFeature {

private command: vscode.Disposable;
private languageClient: LanguageClient;

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();
}

public setLanguageClient(languageClient: LanguageClient) {
this.languageClient = languageClient;
}

private async launchTask(
runInDebugger: boolean,
scriptToRun: string,
args: string[]) {

const launchType = runInDebugger ? LaunchType.Debug : LaunchType.Run;
const launchConfig = createLaunchConfig(launchType, scriptToRun, args);
this.launch(launchConfig);
}

private launch(launchConfig) {
// Create or show the interactive console
// TODO #367: Check if "newSession" mode is configured
vscode.commands.executeCommand("PowerShell.ShowSessionConsole", true);

// Write out temporary debug session file
utils.writeSessionFile(
utils.getDebugSessionFilePath(),
this.sessionManager.getSessionDetails());

// TODO: Update to handle multiple root workspaces.
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;
}
2 changes: 2 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { NewFileOrProjectFeature } from "./features/NewFileOrProject";
import { OpenInISEFeature } from "./features/OpenInISE";
import { PesterTestsFeature } from "./features/PesterTests";
import { RemoteFilesFeature } from "./features/RemoteFiles";
import { RunCodeFeature } from "./features/RunCode";
import { SelectPSSARulesFeature } from "./features/SelectPSSARules";
import { ShowHelpFeature } from "./features/ShowHelp";
import { Logger, LogLevel } from "./logging";
Expand Down Expand Up @@ -149,6 +150,7 @@ export function activate(context: vscode.ExtensionContext): void {
new ShowHelpFeature(logger),
new FindModuleFeature(),
new PesterTestsFeature(sessionManager),
new RunCodeFeature(sessionManager),
new ExtensionCommandsFeature(logger),
new SelectPSSARulesFeature(logger),
new CodeActionsFeature(logger),
Expand Down
39 changes: 39 additions & 0 deletions test/features/RunCode.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/

import * as assert from "assert";
import rewire = require("rewire");
import vscode = require("vscode");

// Setup function that is not exported.
const customViews = rewire("../../src/features/RunCode");
const createLaunchConfig = customViews.__get__("createLaunchConfig");

enum LaunchType {
Debug,
Run,
}

suite("RunCode tests", () => {
test("Can create the launch config", () => {
const commandToRun: string = "Invoke-Build";
const args: string[] = ["Clean"];

const expected: object = {
request: "launch",
type: "PowerShell",
name: "PowerShell Run Code",
script: commandToRun,
args,
internalConsoleOptions: "neverOpen",
noDebug: false,
createTemporaryIntegratedConsole: false,
cwd: vscode.workspace.rootPath,
};

const actual: object = createLaunchConfig(LaunchType.Debug, commandToRun, args);

assert.deepEqual(actual, expected);
});
});