From aa0db26622dc3e27053370df4b88d95cbf7186fb Mon Sep 17 00:00:00 2001 From: Tyler Leonhardt Date: Sat, 27 Apr 2019 21:54:41 -0700 Subject: [PATCH 1/3] Add RunCode command for CodeLens providers --- src/features/RunCode.ts | 86 +++++++++++++++++++++++++++++++++++++++++ src/main.ts | 2 + 2 files changed, 88 insertions(+) create mode 100644 src/features/RunCode.ts diff --git a/src/features/RunCode.ts b/src/features/RunCode.ts new file mode 100644 index 0000000000..91a479cdcf --- /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 = 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); + } +} 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), From cf9457751dc1dace8529beb2f0143b6cf4035dd3 Mon Sep 17 00:00:00 2001 From: Tyler Leonhardt Date: Sun, 28 Apr 2019 19:31:05 -0700 Subject: [PATCH 2/3] pulled out createLaunchConfig and added test --- src/features/RunCode.ts | 53 ++++++++++++++++++----------------- test/features/RunCode.test.ts | 39 ++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 26 deletions(-) create mode 100644 test/features/RunCode.test.ts diff --git a/src/features/RunCode.ts b/src/features/RunCode.ts index 91a479cdcf..521c998202 100644 --- a/src/features/RunCode.ts +++ b/src/features/RunCode.ts @@ -41,35 +41,10 @@ export class RunCodeFeature implements IFeature { args: string[]) { const launchType = runInDebugger ? LaunchType.Debug : LaunchType.Run; - const launchConfig = this.createLaunchConfig(launchType, scriptToRun, args); + const launchConfig = 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 @@ -84,3 +59,29 @@ export class RunCodeFeature implements IFeature { vscode.debug.startDebugging(vscode.workspace.workspaceFolders[0], launchConfig); } } + +function createLaunchConfig(launchType: LaunchType, commandToRun: string, args: string[]) { + const settings = Settings.load(); + + let currentDocument: vscode.TextDocument; + if (vscode.window.activeTextEditor && vscode.window.activeTextEditor.document) { + currentDocument = vscode.window.activeTextEditor.document; + } + + const launchConfig = { + request: "launch", + type: "PowerShell", + name: "PowerShell Run Code", + script: commandToRun, + args, + internalConsoleOptions: "neverOpen", + noDebug: (launchType === LaunchType.Run), + createTemporaryIntegratedConsole: settings.debugging.createTemporaryIntegratedConsole, + cwd: + !currentDocument || currentDocument.isUntitled + ? vscode.workspace.rootPath + : path.dirname(currentDocument.fileName), + }; + + return launchConfig; +} 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); + }); +}); From 6b2c23b03458ca46770ad40cf16cd7b4c6b5611b Mon Sep 17 00:00:00 2001 From: Tyler Leonhardt Date: Sun, 28 Apr 2019 19:38:14 -0700 Subject: [PATCH 3/3] move cwd logic --- src/features/RunCode.ts | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/features/RunCode.ts b/src/features/RunCode.ts index 521c998202..5aabd946f7 100644 --- a/src/features/RunCode.ts +++ b/src/features/RunCode.ts @@ -63,24 +63,23 @@ export class RunCodeFeature implements IFeature { function createLaunchConfig(launchType: LaunchType, commandToRun: string, args: string[]) { const settings = Settings.load(); - let currentDocument: vscode.TextDocument; - if (vscode.window.activeTextEditor && vscode.window.activeTextEditor.document) { - currentDocument = vscode.window.activeTextEditor.document; + 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", - script: commandToRun, - args, internalConsoleOptions: "neverOpen", noDebug: (launchType === LaunchType.Run), createTemporaryIntegratedConsole: settings.debugging.createTemporaryIntegratedConsole, - cwd: - !currentDocument || currentDocument.isUntitled - ? vscode.workspace.rootPath - : path.dirname(currentDocument.fileName), + script: commandToRun, + args, + cwd, }; return launchConfig;