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 1 commit
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 = this.createLaunchConfig(launchType, scriptToRun, args);
this.launch(launchConfig);
}

private createLaunchConfig(launchType: LaunchType, scriptToRun: string, args: string[]) {
const currentDocument = vscode.window.activeTextEditor.document;
const settings = Settings.load();

// Since we pass the script path to PSES in single quotes to avoid issues with PowerShell
// special chars like & $ @ () [], we do have to double up the interior single quotes.

const launchConfig = {
request: "launch",
type: "PowerShell",
name: "PowerShell Launch Pester Tests",
script: scriptToRun,
args,
internalConsoleOptions: "neverOpen",
noDebug: (launchType === LaunchType.Run),
createTemporaryIntegratedConsole: settings.debugging.createTemporaryIntegratedConsole,
cwd:
currentDocument.isUntitled
? vscode.workspace.rootPath
: path.dirname(currentDocument.fileName),
};

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