diff --git a/CHANGELOG.md b/CHANGELOG.md index dffb8e3..29a5b38 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,12 @@ # Change Log +## [0.0.3] +- Added some more documentation +- Refactored some code + ## [0.0.2] -- Removed option to configure the listening interface for security reasons. Now the extension can only listen on the localhost interface. +- Removed option to configure the listening interface for security reasons. Now the extension can only listen on `localhost` interface. - Added `custom.showQuickPick` command to run quick pick dialogs in vscode via automation. - Command `custom.goToFileLineCharacter` can now take a relative path to the workspace. - Added `custom.showInformationMessage`, `custom.showWarningMessage` and `custom.showErrorMessage` to show messages to the users in vscode. diff --git a/README.md b/README.md index 3a6e1d9..419d3d9 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@

-This extension allows you to remotely control instances of Visual Studio Code by exposing a REST endpoint that you can use to invoke vscode commands. In the background it launches a HTTP server that listen to requests of commands to execute. +This extension allows you to remotely control instances of Visual Studio Code by exposing a REST endpoint that you can use to invoke vscode commands. In the background it launches a HTTP server that listen on the `localhost` interface for requests of commands to execute. The demo below shows how we can open a terminal, run some commands in it, then close all terminals, open a file with the cursor in a specified location and finally start a debug session on the same file that is orchestrating it all! @@ -30,13 +30,12 @@ The main motivation behind it was that while `Remote Control` uses `websockets`, The extension has the following settings which you can use to configure it: - `restRemoteControl.enable`: enable/disable this extension -- `restRemoteControl.host`: the hostname of the HTTP server. Default: `127.0.0.1`. - `restRemoteControl.port`: set the port number on which the HTTP server will listen - `restRemoteControl.fallbacks`: an array of port numbers to fallback to if the port is already in use. ## Usage -When you install this extension, it will automatically try to start a HTTP server on port `37100`. This port can be changed on in the VSCode settings. When you are going to use multiple VSCode sessions at the same time, it is best to configure it on workspace level or use the `restRemoteControl.fallbacks` setting to specify fallback ports when the previous one is already in use. +When you install this extension, it will automatically try to start a HTTP server on port `37100`. This port can be changed on in the VSCode settings. When you are going to use multiple VSCode sessions at the same time, it is best to configure it on workspace level or use the `restRemoteControl.fallbacks` setting to specify fallback ports when the previous one is already in use. VSCode terminals opened will have environment variable `REMOTE_CONTROL_PORT` set with the port the server is currently listening to. ![status bar listening message](assets/statusbar-item.png) @@ -100,14 +99,14 @@ Some VSCode commands expect VSCode's defined types such as [Range](https://code. As the extension progresses, I plan to add more _special_ commands (i.e. commands that require some use of the [VSCode API](https://code.visualstudio.com/api/references/vscode-api)). For now, we have defined the following commands: - `custom.goToFileLineCharacter`: allows you to nagivate to a specific position in a file by passing the file path, line and column number as arguments - - `custom.startDebugSession`: allows you to invoke `vscode.debug.startDebugging()` API by passing the workspace folder and a name or definition of a debug configuration - - `custom.runInTerminal`: allows you to invoke commands the currently active integrated terminal - - `custom.showQuickPick`: show quick pick dialog to collect selection from the user +- `custom.showInformationMessage`, `custom.showWarningMessage` and `custom.showErrorMessage`: show message dialogs to the user and let them click on a button +- `custom.listInstalledExtensions`: get the list of installed extension IDs -- `custom.showInformationMessage`, `custom.showWarningMessage` and `custom.showErrorMessage`: Show message dialogs to the user +## To implement in the near future: +- Add the ability to set a breakpoint at the specified file/line combination ### How do I get the command ID? diff --git a/package.json b/package.json index 96740bb..7db62af 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "This extension allows you to remotely control Visual Studio Code via a REST endpoint, taking automation to the next level.", "publisher": "dpar39", "license": "MIT", - "version": "0.0.2", + "version": "0.0.3", "engines": { "vscode": "^1.55.0" }, diff --git a/src/extension.ts b/src/extension.ts index d95f5c3..c2e5f51 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -7,12 +7,24 @@ import { AddressInfo } from "net"; import { ControlRequest } from "./models/controlRequest"; import { processRemoteControlRequest } from "./services/requestProcessor"; -export let server: http.Server; +let server: http.Server; let statusbar: vscode.StatusBarItem; export const EXTENSION_ID: string = "dpar39.vscode-rest-control"; +export function getListeningPort(): number | undefined { + if (!server || !server.listening) { + return; + } + const address = server.address() as any; + return address?.port; +} + const SETTINGS_NAME: string = "restRemoteControl"; +function setRemoteControlEnvironmentVariable(context: vscode.ExtensionContext, port: number = 0) { + context.environmentVariableCollection.replace("REMOTE_CONTROL_PORT", port ? `${port}` : ''); +} + const startHttpServer = async ( context: vscode.ExtensionContext, host: string, @@ -71,7 +83,7 @@ const startHttpServer = async ( Logger.info(listeningMessage); // set the remote control port as an environment variable - context.environmentVariableCollection.replace("REMOTE_CONTROL_PORT", `${verifiedPort}`); + setRemoteControlEnvironmentVariable(context, verifiedPort); statusbar.text = `$(plug) RC Port: ${verifiedPort}`; statusbar.tooltip = listeningMessage; }); @@ -94,6 +106,7 @@ function setupRestControl(context: vscode.ExtensionContext) { statusbar.tooltip = `REST remote control has been disabled via setting "${SETTINGS_NAME}.enabled": false`; statusbar.text = "$(debug-disconnect) RC Disabled"; server?.close(); + setRemoteControlEnvironmentVariable(context); Logger.info("VSCode REST Control has been disabled via settings!"); } } @@ -117,6 +130,7 @@ export function activate(context: vscode.ExtensionContext) { } // deactivate extension method -export function deactivate() { +export function deactivate(context: vscode.ExtensionContext) { server?.close(); + setRemoteControlEnvironmentVariable(context); } diff --git a/src/test/suite/extension.test.ts b/src/test/suite/extension.test.ts index 18d723c..cd3725d 100644 --- a/src/test/suite/extension.test.ts +++ b/src/test/suite/extension.test.ts @@ -1,5 +1,5 @@ import * as assert from "assert"; -import { EXTENSION_ID, server } from "../../extension"; +import { EXTENSION_ID, getListeningPort } from "../../extension"; // You can import and use all API from the 'vscode' module // as well as import your extension to test it @@ -11,10 +11,9 @@ suite("Extension Test Suite", () => { suiteTeardown(() => { }); test("check can list extensions", async () => { - const address = server.address() as any; const extensionIds: string[] = (await makeRequest( "custom.listInstalledExtensions", - [], address.port + [], getListeningPort() )) as string[]; assert(extensionIds.includes(EXTENSION_ID)); }).timeout(5000);