-
Notifications
You must be signed in to change notification settings - Fork 252
Expand file tree
/
Copy pathasdf.ts
More file actions
124 lines (103 loc) · 4.3 KB
/
asdf.ts
File metadata and controls
124 lines (103 loc) · 4.3 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import os from "os";
import path from "path";
import * as vscode from "vscode";
import { VersionManager, ActivationResult, DetectionResult } from "./versionManager";
import { WorkspaceChannel } from "../workspaceChannel";
import { pathToUri } from "../common";
import { ExecutableNotFoundError } from "./errors";
// 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 {
private static getPossibleExecutablePaths(): vscode.Uri[] {
// These directories are where we can find the ASDF executable for v0.16 and above
return [pathToUri("/", "opt", "homebrew", "bin"), pathToUri("/", "usr", "local", "bin")];
}
private static getPossibleScriptPaths(): vscode.Uri[] {
const scriptName = path.basename(vscode.env.shell) === "fish" ? "asdf.fish" : "asdf.sh";
// 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
return [
pathToUri(os.homedir(), ".asdf", scriptName),
pathToUri("/", "opt", "asdf-vm", scriptName),
pathToUri("/", "opt", "homebrew", "opt", "asdf", "libexec", scriptName),
pathToUri("/", "usr", "local", "opt", "asdf", "libexec", scriptName),
];
}
static async detect(
workspaceFolder: vscode.WorkspaceFolder,
outputChannel: WorkspaceChannel,
): Promise<DetectionResult> {
// Check for v0.16+ executables first
const executablePaths = Asdf.getPossibleExecutablePaths();
const asdfExecPaths = executablePaths.map((dir) => vscode.Uri.joinPath(dir, "asdf"));
const execResult = await VersionManager.findFirst(asdfExecPaths);
if (execResult) {
return { type: "path", uri: execResult };
}
// Check for < v0.16 scripts
const scriptResult = await VersionManager.findFirst(Asdf.getPossibleScriptPaths());
if (scriptResult) {
return { type: "path", uri: scriptResult };
}
// check on PATH
const toolExists = await VersionManager.toolExists("asdf", workspaceFolder, outputChannel);
if (toolExists) {
return { type: "semantic", marker: "asdf" };
}
return { type: "none" };
}
async activate(): Promise<ActivationResult> {
// Prefer the path configured by the user, then use detect() to find ASDF
const configuredPath = await this.getConfiguredAsdfPath();
let asdfUri: vscode.Uri | undefined;
if (configuredPath) {
asdfUri = vscode.Uri.file(configuredPath);
} else {
const result = await Asdf.detect(this.workspaceFolder, this.outputChannel);
if (result.type === "path") {
asdfUri = result.uri;
} else if (result.type === "semantic") {
// Use ASDF from PATH
} else {
throw new ExecutableNotFoundError("asdf", [
...Asdf.getPossibleExecutablePaths().map((uri) => uri.fsPath),
...Asdf.getPossibleScriptPaths().map((uri) => uri.fsPath),
]);
}
}
let baseCommand: string;
if (asdfUri) {
const asdfPath = asdfUri.fsPath;
// If there's no extension name, then we are using the ASDF executable directly. If there is an extension, then it's
// a shell script and we have to source it first
baseCommand = path.extname(asdfPath) === "" ? asdfPath : `. ${asdfPath} && asdf`;
} else {
baseCommand = "asdf";
}
const parsedResult = await this.runEnvActivationScript(`${baseCommand} exec ruby`);
return {
env: { ...process.env, ...parsedResult.env },
yjit: parsedResult.yjit,
version: parsedResult.version,
gemPath: parsedResult.gemPath,
};
}
private async getConfiguredAsdfPath(): Promise<string | undefined> {
const config = vscode.workspace.getConfiguration("rubyLsp");
const asdfPath = config.get<string | undefined>("rubyVersionManager.asdfExecutablePath");
if (!asdfPath) {
return;
}
const configuredPath = vscode.Uri.file(asdfPath);
if (!(await VersionManager.pathExists(configuredPath))) {
throw new ExecutableNotFoundError("asdf", [configuredPath.fsPath], configuredPath.fsPath);
}
this.outputChannel.info(`Using configured ASDF executable path: ${asdfPath}`);
return configuredPath.fsPath;
}
}