-
Notifications
You must be signed in to change notification settings - Fork 182
/
Copy pathasdf.ts
93 lines (81 loc) · 2.6 KB
/
asdf.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/* eslint-disable no-process-env */
import os from "os";
import path from "path";
import * as vscode from "vscode";
import { VersionManager, ActivationResult } from "./versionManager";
// A tool to manage multiple runtime versions with a single CLI tool
//
// Learn more: https://github.com/asdf-vm/asdf
export class Asdf extends VersionManager {
async activate(): Promise<ActivationResult> {
const asdfUri = await this.findAsdfInstallation();
const parsedResult = await this.runEnvActivationScript(
`. ${asdfUri.fsPath} && asdf exec ruby`,
);
return {
env: { ...process.env, ...parsedResult.env },
yjit: parsedResult.yjit,
version: parsedResult.version,
gemPath: parsedResult.gemPath,
};
}
// Only public for testing. Finds the ASDF installation URI based on what's advertised in the ASDF documentation
async findAsdfInstallation(): Promise<vscode.Uri> {
const scriptName =
path.basename(vscode.env.shell) === "fish" ? "asdf.fish" : "asdf.sh";
const config = vscode.workspace.getConfiguration("rubyLsp");
const asdfPath = config.get<string | undefined>(
"rubyVersionManager.asdfExecutablePath",
);
if (asdfPath) {
const configuredPath = vscode.Uri.file(asdfPath);
try {
await vscode.workspace.fs.stat(configuredPath);
return configuredPath;
} catch (error: any) {
throw new Error(
`ASDF executable configured as ${configuredPath}, but that file doesn't exist`,
);
}
}
// Possible ASDF installation paths as described in https://asdf-vm.com/guide/getting-started.html#_3-install-asdf.
// In order, the methods of installation are:
// 1. Git
// 2. Pacman
// 3. Homebrew M series
// 4. Homebrew Intel series
const possiblePaths = [
vscode.Uri.joinPath(vscode.Uri.file(os.homedir()), ".asdf", scriptName),
vscode.Uri.joinPath(vscode.Uri.file("/"), "opt", "asdf-vm", scriptName),
vscode.Uri.joinPath(
vscode.Uri.file("/"),
"opt",
"homebrew",
"opt",
"asdf",
"libexec",
scriptName,
),
vscode.Uri.joinPath(
vscode.Uri.file("/"),
"usr",
"local",
"opt",
"asdf",
"libexec",
scriptName,
),
];
for (const possiblePath of possiblePaths) {
try {
await vscode.workspace.fs.stat(possiblePath);
return possiblePath;
} catch (error: any) {
// Continue looking
}
}
throw new Error(
`Could not find ASDF installation. Searched in ${possiblePaths.join(", ")}`,
);
}
}