Skip to content

Commit

Permalink
0.0.8 - Assign port based on workspace path if not specified in setti…
Browse files Browse the repository at this point in the history
…ngs so every workspace gets a unique port if available.
  • Loading branch information
Darien Pardinas Diaz committed Aug 9, 2024
1 parent 3e4fe34 commit 8e420d1
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 13 deletions.
10 changes: 9 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
}
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<img src="https://vscode-marketplace-badge.vercel.app/api/badge/installs/dpar39.vscode-rest-control" alt="Number of installs" style="display: inline-block;margin-left:10px" />

<a href="https://www.buymeacoffee.com/dpar39" title="Buy me a coffee" style="margin-left:10px">
<img src="https://img.shields.io/badge/Buy%20me%20a%20coffee-$5-blue?logo=buy-me-a-coffee&style=flat" alt="Buy me a coffee" style="display: inline-block" />
<img src="https://img.shields.io/badge/Buy%20me%20a%20coffee-$3-blue?logo=buy-me-a-coffee&style=flat" alt="Buy me a coffee" style="display: inline-block" />
</a>
</p>

Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down Expand Up @@ -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",
Expand Down
28 changes: 23 additions & 5 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,20 @@ 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);
}
});
}

const startHttpServer = async (
context: vscode.ExtensionContext,
host: string,
port: number,
fallbackPorts: number[],
fallbackPorts: number[]
): Promise<void> => {
let isInUse = false;
if (port) {
Expand Down Expand Up @@ -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<number | null>("enable");
Expand All @@ -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!");
Expand Down

0 comments on commit 8e420d1

Please sign in to comment.