-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathutils.ts
218 lines (190 loc) · 6.11 KB
/
utils.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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import * as fs from "fs";
import * as path from "path";
import * as cp from "child_process";
import * as vscode from "vscode";
import * as which from "which";
import { createHash, BinaryLike } from "crypto";
import {
ExtensionConfiguration,
Target,
SettingsKey,
ModifiableExtension,
type ToolCheckResult,
type ToolCheckErrorResult,
} from "./types";
import { getMesonBuildOptions } from "./introspection";
import { extensionPath, workspaceState } from "./extension";
export interface ExecResult {
stdout: string;
stderr: string;
error?: cp.ExecFileException;
}
export async function exec(
command: string,
args: string[],
extraEnv: { [key: string]: string } | undefined = undefined,
options: cp.ExecFileOptions = { shell: true },
) {
if (extraEnv) {
options.env = { ...(options.env ?? process.env), ...extraEnv };
}
return new Promise<ExecResult>((resolve, reject) => {
cp.execFile(command, args, options, (error, stdout, stderr) => {
if (error) {
reject({ error, stdout, stderr });
} else {
resolve({ stdout, stderr });
}
});
});
}
export async function execFeed(
command: string,
args: string[],
options: cp.ExecFileOptions = { shell: true },
stdin: string,
) {
return new Promise<ExecResult>((resolve) => {
const p = cp.execFile(command, args, options, (error, stdout, stderr) => {
resolve({ stdout, stderr, error: error ? error : undefined });
});
p.stdin?.write(stdin);
p.stdin?.end();
});
}
export async function parseJSONFileIfExists<T = object>(path: string) {
try {
const data = await fs.promises.readFile(path);
return JSON.parse(data.toString()) as T;
} catch (err) {
return false;
}
}
let _channel: vscode.OutputChannel;
export function getOutputChannel(): vscode.OutputChannel {
if (!_channel) {
_channel = vscode.window.createOutputChannel("Meson Build");
}
return _channel;
}
export function extensionRelative(filepath: string) {
return path.join(extensionPath, filepath);
}
export function getBuildDirectory(sourceDir: string) {
const buildDir = extensionConfiguration("buildFolder");
if (path.isAbsolute(buildDir)) return buildDir;
return path.join(sourceDir, buildDir);
}
let _layoutPromise: Promise<string> | null = null;
async function getLayout() {
const buildDir = workspaceState.get<string>("mesonbuild.buildDir")!;
const buildOptions = await getMesonBuildOptions(buildDir);
return buildOptions.filter((o) => o.name === "layout")[0].value;
}
export function clearCache() {
_layoutPromise = null;
}
export async function getTargetName(target: Target) {
_layoutPromise ??= getLayout();
const layout = await _layoutPromise;
if (layout === "mirror") {
const relativePath = path.relative(
workspaceState.get<string>("mesonbuild.sourceDir")!,
path.dirname(target.defined_in),
);
// Meson requires the separator between path and target name to be '/'.
const targetRelativePath = path.join(relativePath, target.name);
const p = targetRelativePath.split(path.sep).join(path.posix.sep);
return `${p}:${target.type.replace(" ", "_")}`;
} else {
return `meson-out/${target.name}`;
}
}
export function hash(input: BinaryLike) {
const hashObj = createHash("sha1");
hashObj.update(input);
return hashObj.digest("hex");
}
export function getConfiguration() {
return vscode.workspace.getConfiguration("mesonbuild");
}
export function extensionConfiguration<K extends keyof ExtensionConfiguration>(key: K) {
return getConfiguration().get<ExtensionConfiguration[K]>(key)!;
}
export function extensionConfigurationSet<K extends keyof ExtensionConfiguration>(
key: K,
value: ExtensionConfiguration[K],
target = vscode.ConfigurationTarget.Global,
) {
return getConfiguration().update(key, value, target);
}
export function shouldModifySetting(key: ModifiableExtension) {
const modifySettings = extensionConfiguration(SettingsKey.modifySettings);
if (typeof modifySettings == "boolean") return modifySettings;
if (modifySettings.includes(key)) return true;
return false;
}
export function arrayIncludes<T>(array: T[], value: T) {
return array.indexOf(value) !== -1;
}
export function isThenable<T>(x: vscode.ProviderResult<T>): x is Thenable<T> {
return arrayIncludes(Object.getOwnPropertyNames(x), "then");
}
export async function genEnvFile(buildDir: string) {
const envfile = path.join(buildDir, "meson-vscode.env");
try {
await exec(extensionConfiguration("mesonPath"), [
"devenv",
"-C",
buildDir,
"--dump",
envfile,
"--dump-format",
"vscode",
]);
} catch {
// Ignore errors, Meson could be too old to support --dump-format.
return;
}
}
// meson setup --reconfigure is needed if and only if coredata.dat exists.
// Note: With Meson >= 1.1.0 we can always pass --reconfigure even if it was
// not already configured.
export function checkMesonIsConfigured(buildDir: string) {
return fs.existsSync(path.join(buildDir, "meson-private", "coredata.dat"));
}
export async function mesonRootDirs(): Promise<string[]> {
let rootDirs: string[] = [];
let pending: vscode.Uri[] = [];
vscode.workspace.workspaceFolders!.forEach((i) => pending.push(i.uri));
while (true) {
const d = pending.pop();
if (!d) break;
let hasMesonFile: boolean = false;
let subdirs: vscode.Uri[] = [];
for (const [name, type] of await vscode.workspace.fs.readDirectory(d)) {
if (type & vscode.FileType.File && name == "meson.build") {
rootDirs.push(d.fsPath);
hasMesonFile = true;
break;
} else if (type & vscode.FileType.Directory) {
subdirs.push(vscode.Uri.joinPath(d, name));
}
}
if (!hasMesonFile) {
pending.push(...subdirs);
}
}
return rootDirs;
}
export function whenFileExists(ctx: vscode.ExtensionContext, file: string, listener: () => void) {
const watcher = vscode.workspace.createFileSystemWatcher(file, false, true, true);
watcher.onDidCreate(listener);
ctx.subscriptions.push(watcher);
if (fs.existsSync(file)) {
listener();
}
}
export function mesonProgram(): string {
return which.sync(extensionConfiguration("mesonPath"));
}