-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathworkspace-service.ts
735 lines (656 loc) · 27.5 KB
/
workspace-service.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
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
/********************************************************************************
* Copyright (C) 2017 TypeFox and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/
import { injectable, inject, postConstruct } from '@theia/core/shared/inversify';
import URI from '@theia/core/lib/common/uri';
import { WorkspaceServer, THEIA_EXT, VSCODE_EXT, getTemporaryWorkspaceFileUri } from '../common';
import { WindowService } from '@theia/core/lib/browser/window/window-service';
import {
FrontendApplicationContribution, PreferenceServiceImpl, PreferenceScope, PreferenceSchemaProvider, LabelProvider
} from '@theia/core/lib/browser';
import { Deferred } from '@theia/core/lib/common/promise-util';
import { EnvVariablesServer } from '@theia/core/lib/common/env-variables';
import { ILogger, Disposable, DisposableCollection, Emitter, Event, MaybePromise, MessageService } from '@theia/core';
import { WorkspacePreferences } from './workspace-preferences';
import * as jsoncparser from 'jsonc-parser';
import * as Ajv from 'ajv';
import { FrontendApplicationConfigProvider } from '@theia/core/lib/browser/frontend-application-config-provider';
import { FileStat, BaseStat } from '@theia/filesystem/lib/common/files';
import { FileService } from '@theia/filesystem/lib/browser/file-service';
import { FileSystemPreferences } from '@theia/filesystem/lib/browser';
import { workspaceSchema, WorkspaceSchemaUpdater } from './workspace-schema-updater';
import { IJSONSchema } from '@theia/core/lib/common/json-schema';
/**
* The workspace service.
*/
@injectable()
export class WorkspaceService implements FrontendApplicationContribution {
private _workspace: FileStat | undefined;
private _roots: FileStat[] = [];
private deferredRoots = new Deferred<FileStat[]>();
@inject(FileService)
protected readonly fileService: FileService;
@inject(WorkspaceServer)
protected readonly server: WorkspaceServer;
@inject(WindowService)
protected readonly windowService: WindowService;
@inject(ILogger)
protected logger: ILogger;
@inject(WorkspacePreferences)
protected preferences: WorkspacePreferences;
@inject(PreferenceServiceImpl)
protected readonly preferenceImpl: PreferenceServiceImpl;
@inject(PreferenceSchemaProvider)
protected readonly schemaProvider: PreferenceSchemaProvider;
@inject(EnvVariablesServer)
protected readonly envVariableServer: EnvVariablesServer;
@inject(MessageService)
protected readonly messageService: MessageService;
@inject(LabelProvider)
protected readonly labelProvider: LabelProvider;
@inject(FileSystemPreferences)
protected readonly fsPreferences: FileSystemPreferences;
@inject(WorkspaceSchemaUpdater)
protected readonly schemaUpdater: WorkspaceSchemaUpdater;
protected applicationName: string;
protected _ready = new Deferred<void>();
get ready(): Promise<void> {
return this._ready.promise;
}
@postConstruct()
protected async init(): Promise<void> {
this.applicationName = FrontendApplicationConfigProvider.get().applicationName;
const wsUriString = await this.getDefaultWorkspaceUri();
const wsStat = await this.toFileStat(wsUriString);
await this.setWorkspace(wsStat);
this.fileService.onDidFilesChange(event => {
if (this._workspace && this._workspace.isFile && event.contains(this._workspace.resource)) {
this.updateWorkspace();
}
});
this.preferences.onPreferenceChanged(event => {
if (event.preferenceName === 'workspace.supportMultiRootWorkspace') {
this.updateWorkspace();
}
});
this.fsPreferences.onPreferenceChanged(event => {
if (event.preferenceName === 'files.watcherExclude') {
this.refreshRootWatchers();
}
});
this._ready.resolve();
}
/**
* Resolves to the default workspace URI as string.
*
* The default implementation tries to extract the default workspace location
* from the `window.location.hash`, then falls-back to the most recently
* used workspace root from the server.
*
* It is not ensured that the resolved workspace URI is valid, it can point
* to a non-existing location.
*/
protected getDefaultWorkspaceUri(): MaybePromise<string | undefined> {
return this.doGetDefaultWorkspaceUri();
}
protected async doGetDefaultWorkspaceUri(): Promise<string | undefined> {
// Prefer the workspace path specified as the URL fragment, if present.
if (window.location.hash.length > 1) {
// Remove the leading # and decode the URI.
const wpPath = decodeURI(window.location.hash.substring(1));
const workspaceUri = new URI().withPath(wpPath).withScheme('file');
let workspaceStat: FileStat | undefined;
try {
workspaceStat = await this.fileService.resolve(workspaceUri);
} catch { }
if (workspaceStat && !workspaceStat.isDirectory && !this.isWorkspaceFile(workspaceStat)) {
this.messageService.error(`Not a valid workspace file: ${workspaceUri}`);
return undefined;
}
return workspaceUri.toString();
} else {
// Else, ask the server for its suggested workspace (usually the one
// specified on the CLI, or the most recent).
return this.server.getMostRecentlyUsedWorkspace();
}
}
/**
* Get the path of the workspace to use initially.
* @deprecated use `WorkspaceService#getDefaultWorkspaceUri` instead.
*/
protected getDefaultWorkspacePath(): MaybePromise<string | undefined> {
return this.getDefaultWorkspaceUri();
}
/**
* Set the URL fragment to the given workspace path.
*/
protected setURLFragment(workspacePath: string): void {
window.location.hash = workspacePath;
}
get roots(): Promise<FileStat[]> {
return this.deferredRoots.promise;
}
tryGetRoots(): FileStat[] {
return this._roots;
}
get workspace(): FileStat | undefined {
return this._workspace;
}
protected readonly onWorkspaceChangeEmitter = new Emitter<FileStat[]>();
get onWorkspaceChanged(): Event<FileStat[]> {
return this.onWorkspaceChangeEmitter.event;
}
protected readonly onWorkspaceLocationChangedEmitter = new Emitter<FileStat | undefined>();
get onWorkspaceLocationChanged(): Event<FileStat | undefined> {
return this.onWorkspaceLocationChangedEmitter.event;
}
protected readonly toDisposeOnWorkspace = new DisposableCollection();
protected async setWorkspace(workspaceStat: FileStat | undefined): Promise<void> {
if (this._workspace && workspaceStat &&
this._workspace.resource === workspaceStat.resource &&
this._workspace.mtime === workspaceStat.mtime &&
this._workspace.etag === workspaceStat.etag &&
this._workspace.size === workspaceStat.size) {
return;
}
this.toDisposeOnWorkspace.dispose();
this._workspace = workspaceStat;
if (this._workspace) {
const uri = this._workspace.resource;
if (this._workspace.isFile) {
this.toDisposeOnWorkspace.push(this.fileService.watch(uri));
this.onWorkspaceLocationChangedEmitter.fire(this._workspace);
}
this.setURLFragment(uri.path.toString());
} else {
this.setURLFragment('');
}
this.updateTitle();
await this.updateWorkspace();
}
protected async updateWorkspace(): Promise<void> {
await this.updateRoots();
this.watchRoots();
}
protected async updateRoots(): Promise<void> {
const newRoots = await this.computeRoots();
let rootsChanged = false;
if (newRoots.length !== this._roots.length || newRoots.length === 0) {
rootsChanged = true;
} else {
for (const newRoot of newRoots) {
if (!this._roots.some(r => r.resource.toString() === newRoot.resource.toString())) {
rootsChanged = true;
break;
}
}
}
if (rootsChanged) {
this._roots = newRoots;
this.deferredRoots.resolve(this._roots); // in order to resolve first
this.deferredRoots = new Deferred<FileStat[]>();
this.deferredRoots.resolve(this._roots);
this.onWorkspaceChangeEmitter.fire(this._roots);
}
}
protected async computeRoots(): Promise<FileStat[]> {
const roots: FileStat[] = [];
if (this._workspace) {
if (this._workspace.isDirectory) {
return [this._workspace];
}
const workspaceData = await this.getWorkspaceDataFromFile();
if (workspaceData) {
for (const { path } of workspaceData.folders) {
const valid = await this.toValidRoot(path);
if (valid) {
roots.push(valid);
} else {
roots.push(FileStat.dir(path));
}
}
}
}
return roots;
}
protected async getWorkspaceDataFromFile(): Promise<WorkspaceData | undefined> {
if (this._workspace && await this.fileService.exists(this._workspace.resource)) {
if (this._workspace.isDirectory) {
return {
folders: [{ path: this._workspace.resource.toString() }]
};
} else if (this.isWorkspaceFile(this._workspace)) {
const stat = await this.fileService.read(this._workspace.resource);
const strippedContent = jsoncparser.stripComments(stat.value);
const data = jsoncparser.parse(strippedContent);
if (data && WorkspaceData.is(data)) {
return WorkspaceData.transformToAbsolute(data, stat);
}
this.logger.error(`Unable to retrieve workspace data from the file: '${this.labelProvider.getLongName(this._workspace)}'. Please check if the file is corrupted.`);
} else {
this.logger.warn(`Not a valid workspace file: ${this.labelProvider.getLongName(this._workspace)}`);
}
}
}
protected formatTitle(title?: string): string {
const name = this.applicationName;
return title ? `${title} — ${name}` : name;
}
protected updateTitle(): void {
let title: string | undefined;
if (this._workspace) {
const displayName = this._workspace.name;
if (!this._workspace.isDirectory &&
(displayName.endsWith(`.${THEIA_EXT}`) || displayName.endsWith(`.${VSCODE_EXT}`))) {
title = displayName.slice(0, displayName.lastIndexOf('.'));
} else {
title = displayName;
}
}
document.title = this.formatTitle(title);
}
/**
* on unload, we set our workspace root as the last recently used on the backend.
*/
onStop(): void {
this.server.setMostRecentlyUsedWorkspace(this._workspace ? this._workspace.resource.toString() : '');
}
async recentWorkspaces(): Promise<string[]> {
return this.server.getRecentWorkspaces();
}
/**
* Returns `true` if theia has an opened workspace or folder
* @returns {boolean}
*/
get opened(): boolean {
return !!this._workspace;
}
/**
* Returns `true` if a multiple-root workspace is currently open.
* @returns {boolean}
*/
get isMultiRootWorkspaceOpened(): boolean {
return !!this.workspace && !this.workspace.isDirectory;
}
/**
* Returns `true` if there is an opened workspace, and multi root workspace support is enabled.
* @returns {boolean}
*/
get isMultiRootWorkspaceEnabled(): boolean {
return this.opened && this.preferences['workspace.supportMultiRootWorkspace'];
}
/**
* Opens directory, or recreates a workspace from the file that `uri` points to.
*/
open(uri: URI, options?: WorkspaceInput): void {
this.doOpen(uri, options);
}
protected async doOpen(uri: URI, options?: WorkspaceInput): Promise<void> {
const rootUri = uri.toString();
const stat = await this.toFileStat(rootUri);
if (stat) {
if (!stat.isDirectory && !this.isWorkspaceFile(stat)) {
const message = `Not a valid workspace file: ${uri}`;
this.messageService.error(message);
throw new Error(message);
}
// The same window has to be preserved too (instead of opening a new one), if the workspace root is not yet available and we are setting it for the first time.
// Option passed as parameter has the highest priority (for api developers), then the preference, then the default.
await this.roots;
const { preserveWindow } = {
preserveWindow: this.preferences['workspace.preserveWindow'] || !this.opened,
...options
};
await this.server.setMostRecentlyUsedWorkspace(rootUri);
if (preserveWindow) {
this._workspace = stat;
}
this.openWindow(stat, { preserveWindow });
return;
}
throw new Error('Invalid workspace root URI. Expected an existing directory location.');
}
/**
* Adds a root folder to the workspace
* @param uri URI of the root folder being added
*/
async addRoot(uri: URI): Promise<void> {
await this.spliceRoots(this._roots.length, 0, uri);
}
/**
* Removes root folder(s) from workspace.
*/
async removeRoots(uris: URI[]): Promise<void> {
if (!this.opened) {
throw new Error('Folder cannot be removed as there is no active folder in the current workspace.');
}
if (this._workspace) {
const workspaceData = await this.getWorkspaceDataFromFile();
this._workspace = await this.writeWorkspaceFile(this._workspace,
WorkspaceData.buildWorkspaceData(
this._roots.filter(root => uris.findIndex(u => u.toString() === root.resource.toString()) < 0),
workspaceData
)
);
}
}
async spliceRoots(start: number, deleteCount?: number, ...rootsToAdd: URI[]): Promise<URI[]> {
if (!this._workspace) {
throw new Error('There is not active workspace');
}
const dedup = new Set<string>();
const roots = this._roots.map(root => (dedup.add(root.resource.toString()), root.resource.toString()));
const toAdd: string[] = [];
for (const root of rootsToAdd) {
const uri = root.toString();
if (!dedup.has(uri)) {
dedup.add(uri);
toAdd.push(uri);
}
}
const toRemove = roots.splice(start, deleteCount || 0, ...toAdd);
if (!toRemove.length && !toAdd.length) {
return [];
}
if (this._workspace.isDirectory) {
const untitledWorkspace = await this.getUntitledWorkspace();
await this.save(untitledWorkspace);
}
const currentData = await this.getWorkspaceDataFromFile();
const newData = WorkspaceData.buildWorkspaceData(roots, currentData);
await this.writeWorkspaceFile(this._workspace, newData);
return toRemove.map(root => new URI(root));
}
async getUntitledWorkspace(): Promise<URI> {
return getTemporaryWorkspaceFileUri(this.envVariableServer);
}
private async writeWorkspaceFile(workspaceFile: FileStat | undefined, workspaceData: WorkspaceData): Promise<FileStat | undefined> {
if (workspaceFile) {
const data = JSON.stringify(WorkspaceData.transformToRelative(workspaceData, workspaceFile));
const edits = jsoncparser.format(data, undefined, { tabSize: 3, insertSpaces: true, eol: '' });
const result = jsoncparser.applyEdits(data, edits);
await this.fileService.write(workspaceFile.resource, result);
return this.fileService.resolve(workspaceFile.resource);
}
}
/**
* Clears current workspace root.
*/
async close(): Promise<void> {
this._workspace = undefined;
this._roots.length = 0;
await this.server.setMostRecentlyUsedWorkspace('');
this.reloadWindow();
}
/**
* returns a FileStat if the argument URI points to an existing directory. Otherwise, `undefined`.
*/
protected async toValidRoot(uri: URI | string | undefined): Promise<FileStat | undefined> {
const fileStat = await this.toFileStat(uri);
if (fileStat && fileStat.isDirectory) {
return fileStat;
}
return undefined;
}
/**
* returns a FileStat if the argument URI points to a file or directory. Otherwise, `undefined`.
*/
protected async toFileStat(uri: URI | string | undefined): Promise<FileStat | undefined> {
if (!uri) {
return undefined;
}
let uriStr = uri.toString();
try {
if (uriStr.endsWith('/')) {
uriStr = uriStr.slice(0, -1);
}
const normalizedUri = new URI(uriStr).normalizePath();
return await this.fileService.resolve(normalizedUri);
} catch (error) {
return undefined;
}
}
protected openWindow(uri: FileStat, options?: WorkspaceInput): void {
const workspacePath = uri.resource.path.toString();
if (this.shouldPreserveWindow(options)) {
this.reloadWindow();
} else {
try {
this.openNewWindow(workspacePath);
} catch (error) {
// Fall back to reloading the current window in case the browser has blocked the new window
this._workspace = uri;
this.logger.error(error.toString()).then(() => this.reloadWindow());
}
}
}
protected reloadWindow(): void {
// Set the new workspace path as the URL fragment.
if (this._workspace !== undefined) {
this.setURLFragment(this._workspace.resource.path.toString());
} else {
this.setURLFragment('');
}
window.location.reload(true);
}
protected openNewWindow(workspacePath: string): void {
const url = new URL(window.location.href);
url.hash = workspacePath;
this.windowService.openNewWindow(url.toString());
}
protected shouldPreserveWindow(options?: WorkspaceInput): boolean {
return options !== undefined && !!options.preserveWindow;
}
/**
* Return true if one of the paths in paths array is present in the workspace
* NOTE: You should always explicitly use `/` as the separator between the path segments.
*/
async containsSome(paths: string[]): Promise<boolean> {
await this.roots;
if (this.opened) {
for (const root of this._roots) {
const uri = root.resource;
for (const path of paths) {
const fileUri = uri.resolve(path);
const exists = await this.fileService.exists(fileUri);
if (exists) {
return exists;
}
}
}
}
return false;
}
get saved(): boolean {
return !!this._workspace && !this._workspace.isDirectory;
}
/**
* Save workspace data into a file
* @param uri URI or FileStat of the workspace file
*/
async save(uri: URI | FileStat): Promise<void> {
const resource = uri instanceof URI ? uri : uri.resource;
if (!await this.fileService.exists(resource)) {
await this.fileService.create(resource);
}
const workspaceData: WorkspaceData = { folders: [], settings: {} };
if (!this.saved) {
for (const p of Object.keys(this.schemaProvider.getCombinedSchema().properties)) {
if (this.schemaProvider.isValidInScope(p, PreferenceScope.Folder)) {
continue;
}
const preferences = this.preferenceImpl.inspect(p);
if (preferences && preferences.workspaceValue) {
workspaceData.settings![p] = preferences.workspaceValue;
}
}
}
let stat = await this.toFileStat(resource);
Object.assign(workspaceData, await this.getWorkspaceDataFromFile());
stat = await this.writeWorkspaceFile(stat, WorkspaceData.buildWorkspaceData(this._roots, workspaceData));
await this.server.setMostRecentlyUsedWorkspace(resource.toString());
await this.setWorkspace(stat);
this.onWorkspaceLocationChangedEmitter.fire(stat);
}
protected readonly rootWatchers = new Map<string, Disposable>();
protected async watchRoots(): Promise<void> {
const rootUris = new Set(this._roots.map(r => r.resource.toString()));
for (const [uri, watcher] of this.rootWatchers.entries()) {
if (!rootUris.has(uri)) {
watcher.dispose();
}
}
for (const root of this._roots) {
this.watchRoot(root);
}
}
protected async refreshRootWatchers(): Promise<void> {
for (const watcher of this.rootWatchers.values()) {
watcher.dispose();
}
await this.watchRoots();
}
protected async watchRoot(root: FileStat): Promise<void> {
const uriStr = root.resource.toString();
if (this.rootWatchers.has(uriStr)) {
return;
}
const excludes = this.getExcludes(uriStr);
const watcher = this.fileService.watch(new URI(uriStr), {
recursive: true,
excludes
});
this.rootWatchers.set(uriStr, new DisposableCollection(
watcher,
Disposable.create(() => this.rootWatchers.delete(uriStr))
));
}
protected getExcludes(uri: string): string[] {
const patterns = this.fsPreferences.get('files.watcherExclude', undefined, uri);
return Object.keys(patterns).filter(pattern => patterns[pattern]);
}
/**
* Returns the workspace root uri that the given file belongs to.
* In case that the file is found in more than one workspace roots, returns the root that is closest to the file.
* If the file is not from the current workspace, returns `undefined`.
* @param uri URI of the file
*/
getWorkspaceRootUri(uri: URI | undefined): URI | undefined {
if (!uri) {
const root = this.tryGetRoots()[0];
if (root) {
return root.resource;
}
return undefined;
}
const rootUris: URI[] = [];
for (const root of this.tryGetRoots()) {
const rootUri = root.resource;
if (rootUri && rootUri.isEqualOrParent(uri)) {
rootUris.push(rootUri);
}
}
return rootUris.sort((r1, r2) => r2.toString().length - r1.toString().length)[0];
}
areWorkspaceRoots(uris: URI[]): boolean {
if (!uris.length) {
return false;
}
const rootUris = new Set(this.tryGetRoots().map(root => root.resource.toString()));
return uris.every(uri => rootUris.has(uri.toString()));
}
/**
* Check if the file should be considered as a workspace file.
*
* Example: We should not try to read the contents of an .exe file.
*/
protected isWorkspaceFile(fileStat: FileStat): boolean {
return fileStat.resource.path.ext === `.${THEIA_EXT}` || fileStat.resource.path.ext === `.${VSCODE_EXT}`;
}
/**
*
* @param key the property key under which to store the schema (e.g. tasks, launch)
* @param schema the schema for the property. If none is supplied, the update is treated as a deletion.
*/
async updateSchema(key: string, schema?: IJSONSchema): Promise<boolean> {
return this.schemaUpdater.updateSchema({ key, schema });
}
}
export interface WorkspaceInput {
/**
* Tests whether the same window should be used or a new one has to be opened after setting the workspace root. By default it is `false`.
*/
preserveWindow?: boolean;
}
export interface WorkspaceData {
folders: Array<{ path: string, name?: string }>;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[key: string]: { [id: string]: any };
}
export namespace WorkspaceData {
const validateSchema = new Ajv().compile(workspaceSchema);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function is(data: any): data is WorkspaceData {
return !!validateSchema(data);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function buildWorkspaceData(folders: string[] | FileStat[], additionalFields?: Partial<WorkspaceData>): WorkspaceData {
let roots: string[] = [];
if (folders.length > 0) {
if (typeof folders[0] !== 'string') {
roots = (<FileStat[]>folders).map(folder => folder.resource.toString());
} else {
roots = <string[]>folders;
}
}
const data: WorkspaceData = {
folders: roots.map(folder => ({ path: folder }))
};
if (additionalFields) {
delete additionalFields.folders;
Object.assign(data, additionalFields);
}
return data;
}
export function transformToRelative(data: WorkspaceData, workspaceFile?: FileStat): WorkspaceData {
const folderUris: string[] = [];
const workspaceFileUri = new URI(workspaceFile ? workspaceFile.resource.toString() : '').withScheme('file');
for (const { path } of data.folders) {
const folderUri = new URI(path).withScheme('file');
const rel = workspaceFileUri.parent.relative(folderUri);
if (rel) {
folderUris.push(rel.toString());
} else {
folderUris.push(folderUri.toString());
}
}
return buildWorkspaceData(folderUris, data);
}
export function transformToAbsolute(data: WorkspaceData, workspaceFile?: BaseStat): WorkspaceData {
if (workspaceFile) {
const folders: string[] = [];
for (const folder of data.folders) {
const path = folder.path;
if (path.startsWith('file:///')) {
folders.push(path);
} else {
folders.push(workspaceFile.resource.withScheme('file').parent.resolve(path).toString());
}
}
return Object.assign(data, buildWorkspaceData(folders, data));
}
return data;
}
}