-
Notifications
You must be signed in to change notification settings - Fork 122
/
asdf.ts
86 lines (74 loc) · 2.41 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
/* eslint-disable no-process-env */
import os from "os";
import path from "path";
import * as vscode from "vscode";
import { asyncExec } from "../common";
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 activationScript =
"STDERR.print({env: ENV.to_h,yjit:!!defined?(RubyVM::YJIT),version:RUBY_VERSION}.to_json)";
const result = await asyncExec(
`. ${asdfUri.fsPath} && asdf exec ruby -W0 -rjson -e '${activationScript}'`,
{
cwd: this.bundleUri.fsPath,
shell: vscode.env.shell,
env: process.env,
},
);
const parsedResult = this.parseWithErrorHandling(result.stderr);
return {
env: { ...process.env, ...parsedResult.env },
yjit: parsedResult.yjit,
version: parsedResult.version,
};
}
// 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";
// 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(", ")}`,
);
}
}