From 8e420d1470ee56e8c9cfd3dab9c9644373d5368d Mon Sep 17 00:00:00 2001 From: Darien Pardinas Diaz Date: Fri, 9 Aug 2024 11:19:24 -0400 Subject: [PATCH] 0.0.8 - Assign port based on workspace path if not specified in settings so every workspace gets a unique port if available. --- .vscode/settings.json | 10 +++++++++- CHANGELOG.md | 4 ++++ README.md | 8 ++++---- package.json | 6 +++--- src/extension.ts | 28 +++++++++++++++++++++++----- 5 files changed, 43 insertions(+), 13 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 30bf8c2..c20eab5 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -7,5 +7,13 @@ "out": true // set this to false to include "out" folder in search results }, // Turn off tsc task auto detection since we have the necessary tasks as npm scripts - "typescript.tsc.autoDetect": "off" + "typescript.tsc.autoDetect": "off", + "workbench.colorCustomizations": { + "tab.activeBackground": "#013353", + "tab.hoverBackground": "#023557", + "list.hoverBackground": "#092832", + "editorHint.foreground": "#ac2ca8", + "titleBar.activeBackground": "#481b1b", + "titleBar.inactiveBackground": "#270c0c", + }, } \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index aa0d7a5..f7955f5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Change Log +## [0.0.8] + +- Assign the remote control port based on a hash of the path to the workspace or open folders if such port is available. + ## [0.0.7] - Added `custom.showInputBox` to show input box dialog to collect a input string from the user diff --git a/README.md b/README.md index 701f136..b0043d4 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ Number of installs - Buy me a coffee + Buy me a coffee

@@ -30,17 +30,17 @@ 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.port`: set the port number on which the HTTP server will listen +- `restRemoteControl.port`: set the port number on which the HTTP server will listen, otherwise the extension will pick one available port for you based on the current workspace path. - `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. VSCode terminals opened will have environment variable `REMOTE_CONTROL_PORT` set with the port the server is currently listening to. +When you install this extension, it will automatically try to start a HTTP server. The port can be specified with VSCode setting `restRemoteControl.port`. When you are going to use multiple VSCode sessions at the same time, it is best to configure it at workspace level or use the `restRemoteControl.fallbacks` setting to specify fallback ports when the specified one is already in use. VSCode terminals opened will have environment variable `REMOTE_CONTROL_PORT` set with the port the server is currently listening on. ![status bar listening message](assets/statusbar-item.png) -Once installed, you can execute vscode `commands` by making HTTP requests. Here are few examples using `curl`: +Once installed, you can execute vscode `commands` by making HTTP requests. Here are few examples using `curl`, assuming VSCode is listening on port `37100`: ```bash # Create a new terminal diff --git a/package.json b/package.json index 56dc9ab..bad9140 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.7", + "version": "0.0.8", "engines": { "vscode": "^1.55.0" }, @@ -47,8 +47,8 @@ }, "restRemoteControl.port": { "type": "number", - "default": 37100, - "description": "Specifies the port on which the REST API server will start. If you are using multiple VSCode instances, it is best to set this in your workspace settings." + "default": 0, + "description": "Specifies the port on which the REST API server will start. If not specified, it will pick one based on a hash of the current workspace path if possible." }, "restRemoteControl.fallbacks": { "type": "array", diff --git a/src/extension.ts b/src/extension.ts index b5a5936..c0977a4 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -28,9 +28,12 @@ function setRemoteControlEnvironmentVariable(context: vscode.ExtensionContext, p } else { context.environmentVariableCollection.replace(RC_PORT_ENVVAR_NAME, port.toString()); } - const terminalUpdateCommand = port === 0 ? `unset ${RC_PORT_ENVVAR_NAME}` : `export ${RC_PORT_ENVVAR_NAME}=${port}`; - vscode.window.terminals.map(terminal => { - terminal.sendText(terminalUpdateCommand); + const terminalUpdateCommand = + port === 0 ? `unset ${RC_PORT_ENVVAR_NAME}` : `export ${RC_PORT_ENVVAR_NAME}=${port}`; + vscode.window.terminals.map((terminal) => { + if (terminal.exitStatus) { + terminal.sendText(terminalUpdateCommand); + } }); } @@ -38,7 +41,7 @@ const startHttpServer = async ( context: vscode.ExtensionContext, host: string, port: number, - fallbackPorts: number[], + fallbackPorts: number[] ): Promise => { let isInUse = false; if (port) { @@ -97,6 +100,21 @@ const startHttpServer = async ( }); }; +function getDefaultPortForWorkspace(): number { + const identifier = vscode.workspace.workspaceFile + ? vscode.workspace.workspaceFile.toString() + : vscode.workspace.workspaceFolders?.join(""); + if (!identifier) { + return 37100; + } + const hash = identifier.split("").reduce((a: number, b: string) => { + a = (a << 5) - a + b.charCodeAt(0); + return a & a; + }, 0); + const port = 37100 + (Math.abs(hash) % 900); + return port; +} + function setupRestControl(context: vscode.ExtensionContext) { const config = vscode.workspace.getConfiguration(SETTINGS_NAME); const enabled = config.get("enable"); @@ -106,7 +124,7 @@ function setupRestControl(context: vscode.ExtensionContext) { startHttpServer( context, "127.0.0.1", - port || 37100, + port || getDefaultPortForWorkspace(), (fallbackPorts || []).filter((p: number) => p !== port) ); Logger.info("VSCode REST Control is now active!");