Skip to content

Commit f6e4f81

Browse files
committed
Change the result of detect methods to return DetectionResult objects
1 parent 6c48d14 commit f6e4f81

13 files changed

Lines changed: 60 additions & 44 deletions

File tree

vscode/src/ruby.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { RubyInterface } from "./common";
66
import { WorkspaceChannel } from "./workspaceChannel";
77
import { Shadowenv, UntrustedWorkspaceError } from "./ruby/shadowenv";
88
import { Chruby } from "./ruby/chruby";
9-
import { VersionManager } from "./ruby/versionManager";
9+
import { VersionManager, DetectionResult } from "./ruby/versionManager";
1010
import { Mise } from "./ruby/mise";
1111
import { RubyInstaller } from "./ruby/rubyInstaller";
1212
import { Rbenv } from "./ruby/rbenv";
@@ -42,7 +42,7 @@ interface ManagerClass {
4242
manuallySelectRuby: () => Promise<void>,
4343
...args: any[]
4444
): VersionManager;
45-
detect: (workspaceFolder: vscode.WorkspaceFolder, outputChannel: WorkspaceChannel) => Promise<vscode.Uri | undefined>;
45+
detect: (workspaceFolder: vscode.WorkspaceFolder, outputChannel: WorkspaceChannel) => Promise<DetectionResult>;
4646
}
4747

4848
const VERSION_MANAGERS: Record<ManagerIdentifier, ManagerClass> = {
@@ -326,7 +326,8 @@ export class Ruby implements RubyInterface {
326326
continue;
327327
}
328328

329-
if (await ManagerClass.detect(this.workspaceFolder, this.outputChannel)) {
329+
const result = await ManagerClass.detect(this.workspaceFolder, this.outputChannel);
330+
if (result.type !== "none") {
330331
this.versionManager = identifier;
331332
return;
332333
}

vscode/src/ruby/asdf.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import path from "path";
33

44
import * as vscode from "vscode";
55

6-
import { VersionManager, ActivationResult } from "./versionManager";
6+
import { VersionManager, ActivationResult, DetectionResult } from "./versionManager";
77
import { WorkspaceChannel } from "../workspaceChannel";
88
import { pathToUri } from "../common";
99

@@ -36,28 +36,28 @@ export class Asdf extends VersionManager {
3636
static async detect(
3737
workspaceFolder: vscode.WorkspaceFolder,
3838
outputChannel: WorkspaceChannel,
39-
): Promise<vscode.Uri | undefined> {
39+
): Promise<DetectionResult> {
4040
// Check for v0.16+ executables first
4141
const executablePaths = Asdf.getPossibleExecutablePaths();
4242
const asdfExecPaths = executablePaths.map((dir) => vscode.Uri.joinPath(dir, "asdf"));
4343
const execResult = await VersionManager.findFirst(asdfExecPaths);
4444
if (execResult) {
45-
return execResult;
45+
return { type: "path", uri: execResult };
4646
}
4747

4848
// Check for < v0.16 scripts
4949
const scriptResult = await VersionManager.findFirst(Asdf.getPossibleScriptPaths());
5050
if (scriptResult) {
51-
return scriptResult;
51+
return { type: "path", uri: scriptResult };
5252
}
5353

5454
// check on PATH
5555
const toolExists = await VersionManager.toolExists("asdf", workspaceFolder, outputChannel);
5656
if (toolExists) {
57-
return vscode.Uri.file("asdf");
57+
return { type: "semantic", marker: "asdf" };
5858
}
5959

60-
return undefined;
60+
return { type: "none" };
6161
}
6262

6363
async activate(): Promise<ActivationResult> {
@@ -68,9 +68,13 @@ export class Asdf extends VersionManager {
6868
if (configuredPath) {
6969
asdfUri = vscode.Uri.file(configuredPath);
7070
} else {
71-
asdfUri = await Asdf.detect(this.workspaceFolder, this.outputChannel);
71+
const result = await Asdf.detect(this.workspaceFolder, this.outputChannel);
7272

73-
if (!asdfUri) {
73+
if (result.type === "path") {
74+
asdfUri = result.uri;
75+
} else if (result.type === "semantic") {
76+
// Use ASDF from PATH
77+
} else {
7478
throw new Error(
7579
`Could not find ASDF installation. Searched in ${[
7680
...Asdf.getPossibleExecutablePaths(),
@@ -82,7 +86,7 @@ export class Asdf extends VersionManager {
8286

8387
let baseCommand: string;
8488

85-
if (asdfUri.fsPath !== "/asdf") {
89+
if (asdfUri) {
8690
const asdfPath = asdfUri.fsPath;
8791
// If there's no extension name, then we are using the ASDF executable directly. If there is an extension, then it's
8892
// a shell script and we have to source it first

vscode/src/ruby/chruby.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as vscode from "vscode";
66
import { WorkspaceChannel } from "../workspaceChannel";
77
import { pathToUri } from "../common";
88

9-
import { ActivationResult, VersionManager, ACTIVATION_SEPARATOR } from "./versionManager";
9+
import { ActivationResult, VersionManager, ACTIVATION_SEPARATOR, DetectionResult } from "./versionManager";
1010

1111
interface RubyVersion {
1212
engine?: string;
@@ -21,9 +21,9 @@ export class Chruby extends VersionManager {
2121
static async detect(
2222
workspaceFolder: vscode.WorkspaceFolder,
2323
outputChannel: WorkspaceChannel,
24-
): Promise<vscode.Uri | undefined> {
24+
): Promise<DetectionResult> {
2525
const exists = await VersionManager.toolExists("chruby", workspaceFolder, outputChannel);
26-
return exists ? vscode.Uri.file("chruby") : undefined;
26+
return exists ? { type: "semantic", marker: "chruby" } : { type: "none" };
2727
}
2828

2929
// Only public so that we can point to a different directory in tests

vscode/src/ruby/custom.ts

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

3-
import { VersionManager, ActivationResult } from "./versionManager";
3+
import { VersionManager, ActivationResult, DetectionResult } from "./versionManager";
44

55
// Custom
66
//
@@ -12,8 +12,8 @@ export class Custom extends VersionManager {
1212
static async detect(
1313
_workspaceFolder: vscode.WorkspaceFolder,
1414
_outputChannel: vscode.LogOutputChannel,
15-
): Promise<vscode.Uri | undefined> {
16-
return undefined;
15+
): Promise<DetectionResult> {
16+
return { type: "none" };
1717
}
1818

1919
async activate(): Promise<ActivationResult> {

vscode/src/ruby/mise.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import os from "os";
22

33
import * as vscode from "vscode";
44

5-
import { VersionManager, ActivationResult } from "./versionManager";
5+
import { VersionManager, ActivationResult, DetectionResult } from "./versionManager";
66
import { WorkspaceChannel } from "../workspaceChannel";
77
import { pathToUri } from "../common";
88

@@ -13,8 +13,9 @@ export class Mise extends VersionManager {
1313
static async detect(
1414
_workspaceFolder: vscode.WorkspaceFolder,
1515
_outputChannel: WorkspaceChannel,
16-
): Promise<vscode.Uri | undefined> {
17-
return this.findFirst(this.getPossiblePaths());
16+
): Promise<DetectionResult> {
17+
const result = await this.findFirst(this.getPossiblePaths());
18+
return result ? { type: "path", uri: result } : { type: "none" };
1819
}
1920

2021
async activate(): Promise<ActivationResult> {
@@ -75,10 +76,10 @@ export class Mise extends VersionManager {
7576
}
7677
}
7778

78-
const detectedPath = await constructor.detect(this.workspaceFolder, this.outputChannel);
79+
const result = await constructor.detect(this.workspaceFolder, this.outputChannel);
7980

80-
if (detectedPath) {
81-
return detectedPath;
81+
if (result.type === "path") {
82+
return result.uri;
8283
}
8384

8485
const possiblePaths = constructor.getPossiblePaths();

vscode/src/ruby/none.ts

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

33
import { WorkspaceChannel } from "../workspaceChannel";
44

5-
import { VersionManager, ActivationResult } from "./versionManager";
5+
import { VersionManager, ActivationResult, DetectionResult } from "./versionManager";
66

77
// None
88
//
@@ -17,9 +17,9 @@ export class None extends VersionManager {
1717
static async detect(
1818
_workspaceFolder: vscode.WorkspaceFolder,
1919
_outputChannel: vscode.LogOutputChannel,
20-
): Promise<vscode.Uri | undefined> {
20+
): Promise<DetectionResult> {
2121
// None always matches as the final fallback
22-
return vscode.Uri.file("none");
22+
return { type: "semantic", marker: "none" };
2323
}
2424

2525
private readonly rubyPath: string;

vscode/src/ruby/rbenv.ts

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

3-
import { VersionManager, ActivationResult } from "./versionManager";
3+
import { VersionManager, ActivationResult, DetectionResult } from "./versionManager";
44
import { WorkspaceChannel } from "../workspaceChannel";
55

66
// Seamlessly manage your app’s Ruby environment with rbenv.
@@ -10,9 +10,9 @@ export class Rbenv extends VersionManager {
1010
static async detect(
1111
workspaceFolder: vscode.WorkspaceFolder,
1212
outputChannel: WorkspaceChannel,
13-
): Promise<vscode.Uri | undefined> {
13+
): Promise<DetectionResult> {
1414
const exists = await VersionManager.toolExists("rbenv", workspaceFolder, outputChannel);
15-
return exists ? vscode.Uri.file("rbenv") : undefined;
15+
return exists ? { type: "semantic", marker: "rbenv" } : { type: "none" };
1616
}
1717

1818
async activate(): Promise<ActivationResult> {

vscode/src/ruby/rubyInstaller.ts

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

55
import { Chruby } from "./chruby";
66
import { pathToUri } from "../common";
7+
import { DetectionResult } from "./versionManager";
78

89
interface RubyVersion {
910
engine?: string;
@@ -21,8 +22,8 @@ export class RubyInstaller extends Chruby {
2122
static async detect(
2223
_workspaceFolder: vscode.WorkspaceFolder,
2324
_outputChannel: vscode.LogOutputChannel,
24-
): Promise<vscode.Uri | undefined> {
25-
return os.platform() === "win32" ? vscode.Uri.file("RubyInstaller") : undefined;
25+
): Promise<DetectionResult> {
26+
return os.platform() === "win32" ? { type: "semantic", marker: "RubyInstaller" } : { type: "none" };
2627
}
2728

2829
// Environment variables are case sensitive on Windows when we access them through NodeJS. We need to ensure that

vscode/src/ruby/rvm.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import os from "os";
22

33
import * as vscode from "vscode";
44

5-
import { ActivationResult, VersionManager } from "./versionManager";
5+
import { ActivationResult, VersionManager, DetectionResult } from "./versionManager";
66
import { WorkspaceChannel } from "../workspaceChannel";
77
import { pathToUri } from "../common";
88

@@ -14,9 +14,9 @@ export class Rvm extends VersionManager {
1414
static async detect(
1515
workspaceFolder: vscode.WorkspaceFolder,
1616
outputChannel: WorkspaceChannel,
17-
): Promise<vscode.Uri | undefined> {
17+
): Promise<DetectionResult> {
1818
const exists = await VersionManager.toolExists("rvm", workspaceFolder, outputChannel);
19-
return exists ? vscode.Uri.file("rvm") : undefined;
19+
return exists ? { type: "semantic", marker: "rvm" } : { type: "none" };
2020
}
2121

2222
async activate(): Promise<ActivationResult> {

vscode/src/ruby/shadowenv.ts

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

33
import { asyncExec } from "../common";
44

5-
import { VersionManager, ActivationResult } from "./versionManager";
5+
import { VersionManager, ActivationResult, DetectionResult } from "./versionManager";
66

77
// Shadowenv is a tool that allows managing environment variables upon entering a directory. It allows users to manage
88
// which Ruby version should be used for each project, in addition to other customizations such as GEM_HOME.
@@ -23,9 +23,12 @@ export class Shadowenv extends VersionManager {
2323
static async detect(
2424
workspaceFolder: vscode.WorkspaceFolder,
2525
_outputChannel: vscode.LogOutputChannel,
26-
): Promise<vscode.Uri | undefined> {
26+
): Promise<DetectionResult> {
2727
const exists = await Shadowenv.shadowenvDirExists(workspaceFolder.uri);
28-
return exists ? vscode.Uri.joinPath(workspaceFolder.uri, ".shadowenv.d") : undefined;
28+
if (exists) {
29+
return { type: "path", uri: vscode.Uri.joinPath(workspaceFolder.uri, ".shadowenv.d") };
30+
}
31+
return { type: "none" };
2932
}
3033

3134
async activate(): Promise<ActivationResult> {

0 commit comments

Comments
 (0)