diff --git a/src/features/RunCode.ts b/src/features/RunCode.ts new file mode 100644 index 0000000000..5aabd946f7 --- /dev/null +++ b/src/features/RunCode.ts @@ -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; +} diff --git a/src/main.ts b/src/main.ts index b36e15c32c..dd6148df42 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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"; @@ -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), diff --git a/test/features/RunCode.test.ts b/test/features/RunCode.test.ts new file mode 100644 index 0000000000..e01d35f122 --- /dev/null +++ b/test/features/RunCode.test.ts @@ -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); + }); +});