-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
181 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* eslint-disable no-process-env */ | ||
|
||
import path from "path"; | ||
import os from "os"; | ||
|
||
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,home:Gem.user_dir,default:Gem.default_dir}", | ||
".to_json)", | ||
].join(""); | ||
|
||
const result = await asyncExec( | ||
`. ${asdfUri.fsPath} && asdf exec ruby -W0 -rjson -e '${activationScript}'`, | ||
{ | ||
cwd: this.bundleUri.fsPath, | ||
env: { | ||
ASDF_DIR: path.dirname(asdfUri.fsPath), | ||
}, | ||
}, | ||
); | ||
|
||
const parsedResult = JSON.parse(result.stderr); | ||
|
||
// The addition of GEM_HOME, GEM_PATH and putting the bin directories into the PATH happens through ASDF's shell | ||
// hooks. Since we want to avoid spawning shells due to integration issues, we need to insert these variables | ||
// ourselves, so that gem executables can be properly found | ||
parsedResult.env.GEM_HOME = parsedResult.home; | ||
parsedResult.env.GEM_PATH = `${parsedResult.home}${path.delimiter}${parsedResult.default}`; | ||
parsedResult.env.PATH = [ | ||
path.join(parsedResult.home, "bin"), | ||
path.join(parsedResult.default, "bin"), | ||
parsedResult.env.PATH, | ||
].join(path.delimiter); | ||
|
||
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> { | ||
// 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", "asdf.sh"), | ||
vscode.Uri.joinPath(vscode.Uri.file("/"), "opt", "asdf-vm", "asdf.sh"), | ||
vscode.Uri.joinPath( | ||
vscode.Uri.file("/"), | ||
"opt", | ||
"homebrew", | ||
"opt", | ||
"asdf", | ||
"libexec", | ||
"asdf.sh", | ||
), | ||
vscode.Uri.joinPath( | ||
vscode.Uri.file("/"), | ||
"usr", | ||
"local", | ||
"opt", | ||
"asdf", | ||
"libexec", | ||
"asdf.sh", | ||
), | ||
]; | ||
|
||
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(", ")}`, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import assert from "assert"; | ||
import path from "path"; | ||
import os from "os"; | ||
|
||
import * as vscode from "vscode"; | ||
import sinon from "sinon"; | ||
|
||
import { Asdf } from "../../../ruby/asdf"; | ||
import { WorkspaceChannel } from "../../../workspaceChannel"; | ||
import * as common from "../../../common"; | ||
|
||
suite("Asdf", () => { | ||
if (os.platform() === "win32") { | ||
// eslint-disable-next-line no-console | ||
console.log("Skipping Asdf tests on Windows"); | ||
return; | ||
} | ||
|
||
test("Finds Ruby based on .tool-versions", async () => { | ||
// eslint-disable-next-line no-process-env | ||
const workspacePath = process.env.PWD!; | ||
const workspaceFolder = { | ||
uri: vscode.Uri.from({ scheme: "file", path: workspacePath }), | ||
name: path.basename(workspacePath), | ||
index: 0, | ||
}; | ||
const outputChannel = new WorkspaceChannel("fake", common.LOG_CHANNEL); | ||
const asdf = new Asdf(workspaceFolder, outputChannel); | ||
|
||
const activationScript = [ | ||
"STDERR.print(", | ||
"{env: ENV.to_h,yjit:!!defined?(RubyVM::YJIT),version:RUBY_VERSION,home:Gem.user_dir,default:Gem.default_dir}", | ||
".to_json)", | ||
].join(""); | ||
|
||
const execStub = sinon.stub(common, "asyncExec").resolves({ | ||
stdout: "", | ||
stderr: JSON.stringify({ | ||
env: { ANY: "true" }, | ||
yjit: true, | ||
version: "3.0.0", | ||
home: "/home/user/.gem/ruby/3.0.0", | ||
default: "/usr/lib/ruby/gems/3.0.0", | ||
}), | ||
}); | ||
|
||
const findInstallationStub = sinon | ||
.stub(asdf, "findAsdfInstallation") | ||
.resolves(vscode.Uri.file(`${os.homedir()}/.asdf/asdf.sh`)); | ||
|
||
const { env, version, yjit } = await asdf.activate(); | ||
|
||
assert.ok( | ||
execStub.calledOnceWithExactly( | ||
`. ${os.homedir()}/.asdf/asdf.sh && asdf exec ruby -W0 -rjson -e '${activationScript}'`, | ||
{ | ||
cwd: workspacePath, | ||
env: { | ||
ASDF_DIR: `${os.homedir()}/.asdf`, | ||
}, | ||
}, | ||
), | ||
); | ||
|
||
assert.strictEqual(version, "3.0.0"); | ||
assert.strictEqual(yjit, true); | ||
assert.strictEqual(env.GEM_HOME, "/home/user/.gem/ruby/3.0.0"); | ||
assert.strictEqual( | ||
env.GEM_PATH, | ||
"/home/user/.gem/ruby/3.0.0:/usr/lib/ruby/gems/3.0.0", | ||
); | ||
assert.ok(env.PATH!.includes("/home/user/.gem/ruby/3.0.0/bin")); | ||
assert.ok(env.PATH!.includes("/usr/lib/ruby/gems/3.0.0/bin")); | ||
assert.strictEqual(env.ANY, "true"); | ||
|
||
execStub.restore(); | ||
findInstallationStub.restore(); | ||
}); | ||
}); |