Skip to content

Commit 15de79c

Browse files
committed
Extract helper function to create URIs from paths and use it across the codebase
1 parent 6e48de7 commit 15de79c

File tree

9 files changed

+29
-24
lines changed

9 files changed

+29
-24
lines changed

vscode/src/common.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,9 @@ export function featureEnabled(feature: keyof typeof FEATURE_FLAGS): boolean {
144144
// If that number is below the percentage, then the feature is enabled for this user
145145
return hashNum < percentage;
146146
}
147+
148+
// Helper to create a URI from a file path and optional path segments
149+
// Usage: pathToUri("/", "opt", "bin") instead of vscode.Uri.joinPath(vscode.Uri.file("/"), "opt", "bin")
150+
export function pathToUri(basePath: string, ...segments: string[]): vscode.Uri {
151+
return vscode.Uri.joinPath(vscode.Uri.file(basePath), ...segments);
152+
}

vscode/src/ruby/asdf.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,15 @@ import * as vscode from "vscode";
55

66
import { VersionManager, ActivationResult } from "./versionManager";
77
import { WorkspaceChannel } from "../workspaceChannel";
8+
import { pathToUri } from "../common";
89

910
// A tool to manage multiple runtime versions with a single CLI tool
1011
//
1112
// Learn more: https://github.com/asdf-vm/asdf
1213
export class Asdf extends VersionManager {
1314
private static getPossibleExecutablePaths(): vscode.Uri[] {
1415
// These directories are where we can find the ASDF executable for v0.16 and above
15-
return [
16-
vscode.Uri.joinPath(vscode.Uri.file("/"), "opt", "homebrew", "bin"),
17-
vscode.Uri.joinPath(vscode.Uri.file("/"), "usr", "local", "bin"),
18-
];
16+
return [pathToUri("/", "opt", "homebrew", "bin"), pathToUri("/", "usr", "local", "bin")];
1917
}
2018

2119
private static getPossibleScriptPaths(): vscode.Uri[] {
@@ -28,10 +26,10 @@ export class Asdf extends VersionManager {
2826
// 3. Homebrew M series
2927
// 4. Homebrew Intel series
3028
return [
31-
vscode.Uri.joinPath(vscode.Uri.file(os.homedir()), ".asdf", scriptName),
32-
vscode.Uri.joinPath(vscode.Uri.file("/"), "opt", "asdf-vm", scriptName),
33-
vscode.Uri.joinPath(vscode.Uri.file("/"), "opt", "homebrew", "opt", "asdf", "libexec", scriptName),
34-
vscode.Uri.joinPath(vscode.Uri.file("/"), "usr", "local", "opt", "asdf", "libexec", scriptName),
29+
pathToUri(os.homedir(), ".asdf", scriptName),
30+
pathToUri("/", "opt", "asdf-vm", scriptName),
31+
pathToUri("/", "opt", "homebrew", "opt", "asdf", "libexec", scriptName),
32+
pathToUri("/", "usr", "local", "opt", "asdf", "libexec", scriptName),
3533
];
3634
}
3735

vscode/src/ruby/chruby.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import path from "path";
44
import * as vscode from "vscode";
55

66
import { WorkspaceChannel } from "../workspaceChannel";
7+
import { pathToUri } from "../common";
78

89
import { ActivationResult, VersionManager, ACTIVATION_SEPARATOR } from "./versionManager";
910

@@ -26,10 +27,7 @@ export class Chruby extends VersionManager {
2627
}
2728

2829
// Only public so that we can point to a different directory in tests
29-
public rubyInstallationUris = [
30-
vscode.Uri.joinPath(vscode.Uri.file(os.homedir()), ".rubies"),
31-
vscode.Uri.joinPath(vscode.Uri.file("/"), "opt", "rubies"),
32-
];
30+
public rubyInstallationUris = [pathToUri(os.homedir(), ".rubies"), pathToUri("/", "opt", "rubies")];
3331

3432
constructor(
3533
workspaceFolder: vscode.WorkspaceFolder,

vscode/src/ruby/mise.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as vscode from "vscode";
44

55
import { VersionManager, ActivationResult } from "./versionManager";
66
import { WorkspaceChannel } from "../workspaceChannel";
7+
import { pathToUri } from "../common";
78

89
// Mise (mise en place) is a manager for dev tools, environment variables and tasks
910
//
@@ -16,9 +17,9 @@ export class Mise extends VersionManager {
1617
// 3. Installation from `apt install mise`
1718
private static getPossiblePaths(): vscode.Uri[] {
1819
return [
19-
vscode.Uri.joinPath(vscode.Uri.file(os.homedir()), ".local", "bin", "mise"),
20-
vscode.Uri.joinPath(vscode.Uri.file("/"), "opt", "homebrew", "bin", "mise"),
21-
vscode.Uri.joinPath(vscode.Uri.file("/"), "usr", "bin", "mise"),
20+
pathToUri(os.homedir(), ".local", "bin", "mise"),
21+
pathToUri("/", "opt", "homebrew", "bin", "mise"),
22+
pathToUri("/", "usr", "bin", "mise"),
2223
];
2324
}
2425

vscode/src/ruby/rubyInstaller.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import os from "os";
33
import * as vscode from "vscode";
44

55
import { Chruby } from "./chruby";
6+
import { pathToUri } from "../common";
67

78
interface RubyVersion {
89
engine?: string;
@@ -35,8 +36,8 @@ export class RubyInstaller extends Chruby {
3536
const [major, minor, _patch] = rubyVersion.version.split(".").map(Number);
3637

3738
const possibleInstallationUris = [
38-
vscode.Uri.joinPath(vscode.Uri.file("C:"), `Ruby${major}${minor}-${os.arch()}`),
39-
vscode.Uri.joinPath(vscode.Uri.file(os.homedir()), `Ruby${major}${minor}-${os.arch()}`),
39+
pathToUri("C:", `Ruby${major}${minor}-${os.arch()}`),
40+
pathToUri(os.homedir(), `Ruby${major}${minor}-${os.arch()}`),
4041
];
4142

4243
for (const installationUri of possibleInstallationUris) {

vscode/src/ruby/rvm.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as vscode from "vscode";
44

55
import { ActivationResult, VersionManager } from "./versionManager";
66
import { WorkspaceChannel } from "../workspaceChannel";
7+
import { pathToUri } from "../common";
78

89
// Ruby enVironment Manager. It manages Ruby application environments and enables switching between them.
910
// Learn more:
@@ -38,9 +39,9 @@ export class Rvm extends VersionManager {
3839

3940
async findRvmInstallation(): Promise<vscode.Uri> {
4041
const possiblePaths = [
41-
vscode.Uri.joinPath(vscode.Uri.file(os.homedir()), ".rvm", "bin", "rvm-auto-ruby"),
42-
vscode.Uri.joinPath(vscode.Uri.file("/"), "usr", "local", "rvm", "bin", "rvm-auto-ruby"),
43-
vscode.Uri.joinPath(vscode.Uri.file("/"), "usr", "share", "rvm", "bin", "rvm-auto-ruby"),
42+
pathToUri(os.homedir(), ".rvm", "bin", "rvm-auto-ruby"),
43+
pathToUri("/", "usr", "local", "rvm", "bin", "rvm-auto-ruby"),
44+
pathToUri("/", "usr", "share", "rvm", "bin", "rvm-auto-ruby"),
4445
];
4546

4647
for (const uri of possiblePaths) {

vscode/src/test/suite/debugger.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { Debugger } from "../../debugger";
1010
import { Ruby, ManagerIdentifier } from "../../ruby";
1111
import { Workspace } from "../../workspace";
1212
import { WorkspaceChannel } from "../../workspaceChannel";
13-
import { LOG_CHANNEL, asyncExec } from "../../common";
13+
import { LOG_CHANNEL, asyncExec, pathToUri } from "../../common";
1414
import { RUBY_VERSION } from "../rubyVersion";
1515

1616
import { FAKE_TELEMETRY } from "./fakeTelemetry";
@@ -156,7 +156,7 @@ suite("Debugger", () => {
156156
{
157157
parallel: "1",
158158
...ruby.env,
159-
BUNDLE_GEMFILE: vscode.Uri.joinPath(vscode.Uri.file(tmpPath), ".ruby-lsp", "Gemfile").fsPath,
159+
BUNDLE_GEMFILE: pathToUri(tmpPath, ".ruby-lsp", "Gemfile").fsPath,
160160
},
161161
configs.env,
162162
);

vscode/src/test/suite/ruby/mise.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ suite("Mise", () => {
5353
});
5454
const findStub = sandbox
5555
.stub(mise, "findMiseUri")
56-
.resolves(vscode.Uri.joinPath(vscode.Uri.file(os.homedir()), ".local", "bin", "mise"));
56+
.resolves(common.pathToUri(os.homedir(), ".local", "bin", "mise"));
5757

5858
const { env, version, yjit } = await mise.activate();
5959

vscode/src/test/suite/ruby/rvm.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ suite("RVM", () => {
4646

4747
const installationPathStub = sandbox
4848
.stub(rvm, "findRvmInstallation")
49-
.resolves(vscode.Uri.joinPath(vscode.Uri.file(os.homedir()), ".rvm", "bin", "rvm-auto-ruby"));
49+
.resolves(common.pathToUri(os.homedir(), ".rvm", "bin", "rvm-auto-ruby"));
5050

5151
const envStub = ["3.0.0", "/path/to/gems", "true", `ANY${VALUE_SEPARATOR}true`].join(FIELD_SEPARATOR);
5252

0 commit comments

Comments
 (0)