aboutsummaryrefslogtreecommitdiff
path: root/editors/vscode/src/toolchain.ts
blob: d62f6ba300880c893dcb2a885293219574dc8e76 (plain)
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
import * as vscode from "vscode";
import * as os from "os";
import * as path from "path";
import * as fs from "fs";

import { execute, log, memoize } from './util';
import { Config } from "./config";

export function isOdinInstalled(config: Config): boolean {
	return vscode.workspace.workspaceFolders?.some(folder => {
		if (config.odinCommand) {
			let command = path.isAbsolute(config.odinCommand) ? config.odinCommand :
				path.join(folder.uri.fsPath, config.odinCommand);
			return isFile(command) && fs.existsSync(command);
		}
	}) || getPathForExecutable("odin") !== "";
}

export const getPathForExecutable = memoize(
	// We apply caching to decrease file-system interactions
	(executableName: "odin"): string => {
		{
			const envVar = process.env[executableName.toUpperCase()];
			if (envVar) {
				return envVar;
			}
		}

		const path = lookupInPath(executableName);
		if (path != undefined) {
			return path;
		}

		return "";
	}
);

function lookupInPath(exec: string): string | undefined {
	const paths = process.env.PATH ?? "";

	const candidates = paths.split(path.delimiter).flatMap(dirInPath => {
		const candidate = path.join(dirInPath, exec);
		return os.type() === "Windows_NT"
			? [candidate, `${candidate}.exe`]
			: [candidate];
	});

	for (let i = 0; i < candidates.length; i += 1) {
		try {
			const pathToOdin = fs.realpathSync(candidates[i]);
			if (!!pathToOdin) {
				return pathToOdin;
			}
		} catch (realpathError) {
			console.debug("couldn't find odin at", candidates[i], "on account of", realpathError)
		}
	}

	return undefined;
}

function isFile(suspectPath: string): boolean {
	// It is not mentionned in docs, but `statSync()` throws an error when
	// the path doesn't exist
	try {
		return fs.statSync(suspectPath).isFile();
	} catch {
		return false;
	}
}