-
Notifications
You must be signed in to change notification settings - Fork 500
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add RunCode command for CodeLens providers (#1928)
* Add RunCode command for CodeLens providers * pulled out createLaunchConfig and added test * move cwd logic
- Loading branch information
1 parent
81d9e36
commit b63979d
Showing
3 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |