Repository navigation
Expand file tree
/
Copy pathextension-runtime.js
More file actions
261 lines (214 loc) · 6.05 KB
/
Copy pathextension-runtime.js
File metadata and controls
261 lines (214 loc) · 6.05 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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
'use strict';
const fs = require('fs');
const path = require('path');
const { executableForRid, ridFor } = require('./server-platform');
const PATH_SERVER_COMMAND = 'cvolo-language-server';
function currentRid(platform = process.platform, arch = process.arch) {
return ridFor(platform, arch);
}
function bundledServerPath({
extensionRoot,
platform = process.platform,
arch = process.arch,
pathApi = path
}) {
const rid = currentRid(platform, arch);
if (!rid) {
return null;
}
const executable = executableForRid(rid);
if (!executable) {
return null;
}
return pathApi.join(extensionRoot, 'server', rid, executable);
}
function isFile(fsApi, candidate) {
try {
return fsApi.statSync(candidate).isFile();
} catch {
return false;
}
}
function resolveServerCommand({
configuredPath = '',
extensionRoot,
platform = process.platform,
arch = process.arch,
fsApi = fs,
pathApi = path
}) {
const configured = String(configuredPath ?? '').trim();
if (configured.length > 0) {
if (!pathApi.isAbsolute(configured)) {
throw new Error('cvolo.server.path must be an absolute executable path.');
}
if (!fsApi.existsSync(configured)) {
throw new Error(`Configured Cvolo Language Server does not exist: ${configured}`);
}
if (!isFile(fsApi, configured)) {
throw new Error(`Configured Cvolo Language Server is not a file: ${configured}`);
}
return { command: configured, source: 'cvolo.server.path' };
}
const bundled = bundledServerPath({ extensionRoot, platform, arch, pathApi });
if (bundled && fsApi.existsSync(bundled) && isFile(fsApi, bundled)) {
return { command: bundled, source: `bundled ${currentRid(platform, arch)}` };
}
return { command: PATH_SERVER_COMMAND, source: 'PATH' };
}
class LifecycleController {
constructor({ startClient, stopClient, onError = () => {} }) {
if (typeof startClient !== 'function' || typeof stopClient !== 'function') {
throw new TypeError('LifecycleController requires startClient and stopClient functions.');
}
this._startClient = startClient;
this._stopClient = stopClient;
this._onError = onError;
this._client = undefined;
this._tail = Promise.resolve();
this._deactivating = false;
this._deactivationPromise = undefined;
}
get isDeactivating() {
return this._deactivating;
}
get hasClient() {
return this._client !== undefined;
}
get client() {
return this._client;
}
_enqueue(label, operation) {
const result = this._tail
.catch(() => undefined)
.then(operation);
this._tail = result.catch((error) => {
try {
this._onError(label, error);
} catch {
// Error reporting must never poison lifecycle serialization.
}
return undefined;
});
return result;
}
async _startCurrent() {
if (this._deactivating || this._client) {
return false;
}
const nextClient = await this._startClient();
if (!nextClient) {
throw new Error('startClient must resolve to a client instance.');
}
this._client = nextClient;
return true;
}
async _stopCurrent() {
const current = this._client;
if (!current) {
return false;
}
// Keep the reference until stop succeeds. If stop fails, a later allowed
// lifecycle operation can retry the same client instead of accidentally
// starting a replacement beside a process that may still be alive.
await this._stopClient(current);
this._client = undefined;
return true;
}
start(reason = 'start') {
if (this._deactivating) {
return Promise.resolve(false);
}
return this._enqueue(reason, () => this._startCurrent());
}
stop(reason = 'stop') {
return this._enqueue(reason, () => this._stopCurrent());
}
restart(reason = 'restart') {
if (this._deactivating) {
return Promise.resolve(false);
}
return this._enqueue(reason, async () => {
if (this._deactivating) {
return false;
}
await this._stopCurrent();
if (this._deactivating) {
return false;
}
return this._startCurrent();
});
}
deactivate(reason = 'deactivate') {
this._deactivating = true;
if (!this._deactivationPromise) {
this._deactivationPromise = this._enqueue(reason, () => this._stopCurrent());
}
return this._deactivationPromise;
}
settled() {
return this._tail;
}
}
function bundleManifestPath({
extensionRoot,
platform = process.platform,
arch = process.arch,
pathApi = path
}) {
const rid = currentRid(platform, arch);
if (!rid) {
return null;
}
return pathApi.join(extensionRoot, 'server', rid, 'bundle-manifest.json');
}
// Reads the provenance manifest that ships inside the bundled server directory.
// The manifest is the single source of truth for the bundled Language Server's
// identity; the extension never hardcodes those values. Returns null when there
// is no bundle for the current platform or the manifest cannot be read/parsed.
function readBundleManifest({
extensionRoot,
platform = process.platform,
arch = process.arch,
fsApi = fs,
pathApi = path
}) {
const rid = currentRid(platform, arch);
const manifestPath = bundleManifestPath({ extensionRoot, platform, arch, pathApi });
if (!rid || !manifestPath) {
return null;
}
let raw;
try {
raw = fsApi.readFileSync(manifestPath, 'utf8');
} catch {
return null;
}
let manifest;
try {
manifest = JSON.parse(raw);
} catch {
return null;
}
return { rid, manifestPath, manifest };
}
function formatBundleProvenance(manifest, source) {
return [
'Cvolo Language Server',
`Source: ${source}`,
`Version: ${manifest.languageServerVersion}`,
`Commit: ${manifest.languageServerCommit}`,
`Tooling: ${manifest.toolingVersion}`,
`Compiler compatibility: ${manifest.compilerCompatibilityLine}`
];
}
module.exports = {
PATH_SERVER_COMMAND,
LifecycleController,
bundleManifestPath,
bundledServerPath,
currentRid,
formatBundleProvenance,
readBundleManifest,
resolveServerCommand
};