/
workspace.ts
1266 lines (1126 loc) · 35.7 KB
/
workspace.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
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* This file is part of the vscode-powertools distribution.
* Copyright (c) Next.e.GO Mobile SE, Aachen, Germany (https://www.e-go-mobile.com/)
*
* vscode-powertools is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, version 3.
*
* vscode-powertools is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import * as _ from 'lodash';
import * as childProcess from 'child_process';
import * as ego_code from './code';
import * as ego_contracts from './contracts';
import * as ego_helpers from './helpers';
import * as ego_states from './states';
import * as ego_stores from './stores';
import * as ego_values from './values';
import * as ego_workspaces_apps from './workspaces/apps';
import * as ego_workspaces_buttons from './workspaces/buttons';
import * as ego_workspaces_commands from './workspaces/commands';
import * as ego_workspaces_config from './workspaces/config';
import * as ego_workspaces_events from './workspaces/events';
import * as ego_workspaces_jobs from './workspaces/jobs';
import * as ego_workspaces_npm from './workspaces/npm';
import * as ego_workspaces_startup from './workspaces/startup';
import * as fsExtra from 'fs-extra';
import * as path from 'path';
import pQueue from 'p-queue';
import * as vscode from 'vscode';
/**
* A context for a workspace instaqnce.
*/
export interface WorkspaceContext {
/**
* The extension context.
*/
readonly extension: vscode.ExtensionContext;
/**
* The underlying file system watcher.
*/
readonly fileWatcher: vscode.FileSystemWatcher;
/**
* The logger for that workspace.
*/
readonly logger: ego_helpers.Logger;
/**
* The output channel.
*/
readonly output: vscode.OutputChannel;
/**
* The underlying queue for handling concurrent actions between all workspaces.
*/
readonly queue: pQueue;
}
/**
* A function that provides workspaces.
*
* @return {Workspace|Workspace[]} The workspace(s).
*/
export type WorkspaceProvider = () => Workspace | Workspace[];
/**
* Workspace settings.
*/
export interface WorkspaceSettings extends ego_contracts.ExtensionConfiguration {
}
let allWorkspacesProvider: WorkspaceProvider;
/**
* Handles a workspace.
*/
export class Workspace extends ego_helpers.WorkspaceBase {
private _configSrc: ego_helpers.WorkspaceConfigSource;
private _isInitialized = false;
private readonly _QUEUE = ego_helpers.createQueue();
private _scriptStates: ego_contracts.FileStateStorage;
private _settings: WorkspaceSettings;
/**
* Initializes a new instance of that class.
*
* @param {number} id The ID of the workspace.
* @param {vscode.WorkspaceFolder} folder The folder instance.
* @param {WorkspaceContext} context The context.
*/
public constructor(
public readonly id: number,
folder: vscode.WorkspaceFolder,
public readonly context: WorkspaceContext,
) {
super(folder);
}
/**
* @inheritdoc
*/
public get configSource() {
return this._configSrc;
}
/**
* Checks if a conditional object does match items condition.
*
* @param {ego_contracts.Conditional} obj The object to check.
*
* @return {boolean} Matches condition or not.
*/
public doesMatchFilterCondition(obj: ego_contracts.Conditional): boolean {
return this.filterConditionals(
obj
).length > 0;
}
/**
* Checks if a platform object does match the (current) platform.
*
* @param {ego_contracts.Conditional} obj The object to check.
*
* @return {boolean} Matches condition or not.
*/
public doesMatchPlatformCondition(obj: ego_contracts.ForPlatforms): boolean {
return this.filterForPlatform(
obj
).length > 0;
}
/**
* Executes code for that workspace.
*
* @param {string} code The code to execute.
* @param {ego_contracts.Value|ego_contracts.Value[]} [values] One or more additional value.
*
* @return {any} The result of the execution.
*/
public executeCode(
code: string,
values?: ego_contracts.Value | ego_contracts.Value[],
): any {
code = ego_helpers.toStringSafe(code);
if ('' === code.trim()) {
return;
}
return ego_code.run({
code: code,
values: ego_values.toValueStorage(
this.getValues(true)
.concat( ego_helpers.asArray(values) )
),
});
}
/**
* Executes the code in 'onEditorChanged' of an object for that workspace.
*
* @param {TObj|TObj[]} objs One or more objects.
* @param {Function} codeExecutor A custom code executor.
*/
public executeOnEditorChangedEvents<
TObj extends ego_contracts.WithEditorChangedEvents,
>(
objs: TObj | TObj[],
codeExecutor?: (code: string, obj: TObj) => any,
) {
if (arguments.length < 2) {
codeExecutor = (code) => {
return this.executeCode(code);
};
}
return ego_helpers.executeOnEditorChangedEvents(
objs, codeExecutor
);
}
/**
* Executes a script.
*
* @param {TSettings} settings The object with the settings.
* @param {Function} argsFactory The function that produces the argument object for the execution.
*
* @return {Promise<any>} The promise with the result of the execution.
*/
public async executeScript<
TArgs extends ego_contracts.WorkspaceScriptArguments,
TSettings extends ego_contracts.WithScript = ego_contracts.WithScript
>(
settings: TSettings,
argsFactory: (args: TArgs, settings: TSettings) => TArgs | PromiseLike<TArgs>
): Promise<any> {
const SCRIPT_PATH = this.replaceValues(
settings.script
);
const FULL_SCRIPT_PATH = this.getExistingFullPath(
SCRIPT_PATH
);
if (false === FULL_SCRIPT_PATH) {
throw new Error(`Script '${ SCRIPT_PATH }' not found!`);
}
const SCRIPT_MODULE = ego_helpers.loadScriptModule<ego_contracts.ScriptModule>(
FULL_SCRIPT_PATH
);
if (SCRIPT_MODULE) {
if (SCRIPT_MODULE.execute) {
const BASE_ARGS: ego_contracts.WorkspaceScriptArguments = {
extension: this.context.extension,
globals: ego_helpers.cloneObject(this.settings.globals),
globalState: ego_states.GLOBAL_STATE,
globalStore: new ego_stores.UserStore(),
logger: this.logger,
options: ego_helpers.cloneObject(settings.options),
output: this.output,
replaceValues: (val) => {
return this.replaceValues(val);
},
require: (id) => {
return ego_helpers.requireModule(id);
},
state: undefined,
store: new ego_stores.UserStore(FULL_SCRIPT_PATH),
};
// BASE_ARGS.state
const STATE_GETTER_SETTER = ego_states.getScriptState(
FULL_SCRIPT_PATH, this.scriptStates,
ego_helpers.getInitialStateValue(settings)
);
Object.defineProperty(BASE_ARGS, 'state', {
enumerable: true,
get: STATE_GETTER_SETTER.get,
set: STATE_GETTER_SETTER.set,
});
const ARGS: TArgs = await Promise.resolve(
argsFactory(
<any>BASE_ARGS, settings
)
);
return await Promise.resolve(
SCRIPT_MODULE.execute(ARGS)
);
}
}
}
/**
* Filters "conditional" items.
*
* @param {TObj|TObj[]} objs The objects to check.
*
* @return {TObj[]} The filtered items.
*/
public filterConditionals<TObj extends ego_contracts.Conditional = ego_contracts.Conditional>(
objs: TObj | TObj[]
): TObj[] {
return ego_helpers.asArray(objs).filter(o => {
try {
const IF = ego_helpers.toStringSafe(o.if);
if ('' !== IF.trim()) {
const ALL_VALUES = this.getValues(true);
const VALUES: any = {};
ALL_VALUES.forEach(v => {
Object.defineProperty(VALUES, v.name, {
enumerable: true,
get: () => {
return v.value;
},
});
});
return ego_helpers.toBooleanSafe(
ego_code.run({
code: IF,
values: VALUES,
}),
true
);
}
return true;
} catch (e) {
this.logger
.trace(e, 'workspace.Workspace.filterConditionals(1)');
return false;
}
});
}
/**
* Filters "platform" items.
*
* @param {TObj|TObj[]} objs The objects to check.
*
* @return {TObj[]} The filtered items.
*/
public filterForPlatform<TObj extends ego_contracts.ForPlatforms>(
objs: TObj | TObj[]
): TObj[] {
return ego_helpers.filterForPlatform(
objs
);
}
/**
* Returns all apps of that workspace.
*
* @return {ego_contracts.WorkspaceApp[]} The list of apps.
*/
public getApps(): ego_contracts.WorkspaceApp[] {
return ego_helpers.asArray(
this.instanceState[
ego_workspaces_apps.KEY_APPS
]
);
}
/**
* Returns all commands of that workspace.
*
* @return {ego_contracts.WorkspaceCommand[]} The list of commands.
*/
public getCommands(): ego_contracts.WorkspaceCommand[] {
return ego_helpers.asArray(
this.instanceState[
ego_workspaces_commands.KEY_COMMANDS
]
);
}
/**
* Returns all config imports of that workspace.
*
* @return {ego_contracts.WorkspaceConfigImport[]} The list of config imports.
*/
public getConfigImports(): ego_contracts.WorkspaceConfigImport[] {
return ego_helpers.asArray(
this.instanceState[
ego_workspaces_config.KEY_CONFIG_IMPORTS
]
);
}
/**
* Returns all events of that workspace.
*
* @return {ego_contracts.WorkspaceEvent[]} The list of events.
*/
public getEvents(): ego_contracts.WorkspaceEvent[] {
return ego_helpers.asArray(
this.instanceState[
ego_workspaces_events.KEY_EVENTS
]
);
}
/**
* Returns all events of that workspace by type.
*
* @param {string} type The type.
*
* @return {ego_contracts.WorkspaceEvent[]} The list of events.
*/
public getEventsBy(type: string): ego_contracts.WorkspaceEvent[] {
type = ego_helpers.normalizeString(type);
return this.getEvents()
.filter(e => {
const EVENT_TYPE = ego_helpers.normalizeString(e.type);
return EVENT_TYPE === type ||
EVENT_TYPE === '';
});
}
/**
* Returns the full path of an existing file.
*
* @param {string} p The input path value.
*
* @return {string|false} The full path or (false) if not found.
*/
public getExistingFullPath(p: string): string | false {
p = ego_helpers.toStringSafe(p);
if (path.isAbsolute(p)) {
return fsExtra.existsSync(p) ? path.resolve(
p
) : false;
}
for (const LU of this.getFolderLookups()) {
const FULL_PATH = path.resolve(
path.join(
LU, p
)
);
if (fsExtra.existsSync(FULL_PATH)) {
return FULL_PATH;
}
}
return false;
}
/**
* Returns the list of folders to lookup for relative paths.
*
* @return {string[]} Folder lookups.
*/
public getFolderLookups(): string[] {
return [
// '.vscode' sub folder
// insode workspace
path.resolve(
path.join(
this.rootPath, '.vscode'
)
),
// extension's suf folder
// inside user's home directory
ego_helpers.getExtensionDirInHome(),
];
}
/**
* Returns the information about that workspace.
*
* @return {ego_contracts.WorkspaceInfo} The workspace information.
*/
public getInfo(): ego_contracts.WorkspaceInfo {
return {
index: this.folder.index,
name: this.folder.name,
rootPath: this.rootPath,
};
}
/**
* Returns all jobs of that workspace.
*
* @return {ego_contracts.WorkspaceJob[]} The list of jobs.
*/
public getJobs(): ego_contracts.WorkspaceJob[] {
return ego_helpers.asArray(
this.instanceState[
ego_workspaces_jobs.KEY_JOBS
]
);
}
/**
* Returns the list of all values.
*
* @param {boolean} [all] Return also the ones from the settings. Default: (false)
*
* @return {ego_contracts.Value[]} The list of values.
*/
public getValues(all?: boolean): ego_contracts.Value[] {
all = ego_helpers.toBooleanSafe(all);
const VALUES: ego_contracts.Value[] = [
new ego_values.FunctionValue(() => {
return this.id;
}, 'workspaceId'),
new ego_values.FunctionValue(() => {
return this.folder.index;
}, 'workspaceIndex'),
new ego_values.FunctionValue(() => {
return this.folder.name;
}, 'workspaceName'),
new ego_values.FunctionValue(() => {
return this.rootPath;
}, 'workspaceRoot'),
new ego_values.FunctionValue(() => {
return this.folder.uri;
}, 'workspaceUri'),
];
if (all) {
ego_helpers.from(
ego_values.toValues(
this.settings,
{
outputProvider: () => {
return this.output;
},
pathResolver: (p) => {
return this.resolveValuePath(p);
},
}
),
).pushTo(
VALUES
);
}
return VALUES;
}
/**
* Imports values to an object.
*
* @param {TObj} obj The object where to import the values in.
* @param {boolean} [clone] Clone input object or not. Default: (true)
*
* @return {TObj} The object that contains the imported values.
*/
public importValues<TObj extends ego_contracts.CanImportValues = ego_contracts.CanImportValues>(
obj: TObj,
clone?: boolean,
): TObj {
return ego_helpers.importValues(
obj,
() => this.getValues(true),
clone,
);
}
/**
* Initializes the workspace.
*/
public async initialize() {
this._configSrc = {
section: 'ego.power-tools',
resource: vscode.Uri.file(path.join(this.rootPath,
'.vscode/settings.json') ),
};
this.instanceState[
ego_workspaces_config.KEY_CONFIG_IMPORTS
] = [];
this.instanceState[
ego_workspaces_apps.KEY_APPS
] = [];
this.instanceState[
ego_workspaces_buttons.KEY_BUTTONS
] = [];
this.instanceState[
ego_workspaces_commands.KEY_COMMANDS
] = [];
this.instanceState[
ego_workspaces_events.KEY_EVENTS
] = [];
this.instanceState[
ego_workspaces_jobs.KEY_JOBS
] = [];
// file change events
{
const RAISE_FILE_CHANGE = (type: ego_contracts.FileChangeType, file: vscode.Uri) => {
this._QUEUE.add(async () => {
await this.onFileChange(type, file);
}).then(() => {
}).catch((err) => {
this.logger.trace(
err, 'workspace.onFileChange(1)'
);
});
};
this.context.fileWatcher.onDidChange((f) => {
RAISE_FILE_CHANGE(ego_contracts.FileChangeType.Changed, f);
});
this.context.fileWatcher.onDidCreate((f) => {
RAISE_FILE_CHANGE(ego_contracts.FileChangeType.Created, f);
});
this.context.fileWatcher.onDidDelete((f) => {
RAISE_FILE_CHANGE(ego_contracts.FileChangeType.Deleted, f);
});
}
try {
this._isInitialized = true;
await this.reloadConfiguration();
} catch (e) {
this.logger.trace(
e, 'workspace.initialize(1)'
);
return false;
}
return true;
}
/**
* A key/value pair for data for that instance.
*/
public readonly instanceState: { [key: string]: any } = {};
/**
* Gets if workspace has been initialized or not.
*/
public get isInitialized() {
return this._isInitialized;
}
/**
* Checks if a path is inside the '.git' folder.
*
* @param {string} p The path to check.
*
* @return {boolean} Is in '.git' folder or not.
*/
public isInGit(p: string): boolean {
p = ego_helpers.toStringSafe(p);
if (!path.isAbsolute(p)) {
p = path.join(
this.rootPath, p
);
}
p = path.resolve(p);
const GIT_FOLDER = path.resolve(
path.join(
this.rootPath, '.git'
)
);
return p.startsWith(GIT_FOLDER + path.sep) ||
GIT_FOLDER === p;
}
/**
* Checks if a path is inside the '.vscode' folder.
*
* @param {string} p The path to check.
*
* @return {boolean} Is in '.vscode' folder or not.
*/
public isInVscode(p: string): boolean {
p = ego_helpers.toStringSafe(p);
if (!path.isAbsolute(p)) {
p = path.join(
this.rootPath, p
);
}
p = path.resolve(p);
const VSCODE_FOLDER = path.resolve(
path.join(
this.rootPath, '.vscode'
)
);
return p.startsWith(VSCODE_FOLDER + path.sep) ||
VSCODE_FOLDER === p;
}
/**
* Checks if a path is part of that workspace.
*
* @param {string} path The path to check.
*
* @return {boolean} Is part of that workspace or not.
*/
public isPathOf(path: string) {
return false !== this.toFullPath(path);
}
/**
* Gets the logger of that workspace.
*/
public get logger() {
return this.context.logger;
}
/**
* @inheritdoc
*/
public async onDidChangeConfiguration(e: vscode.ConfigurationChangeEvent) {
await this._QUEUE.add(async () => {
await this.reloadConfiguration();
});
}
/**
* Is invoked when a text document has been opened.
*
* @param {vscode.TextDocument} doc The underlying text document.
*/
public async onDidOpenTextDocument(doc: vscode.TextDocument) {
await this._QUEUE.add(async () => {
await this.onDocumentOpened(
doc
);
});
}
/**
* Is invoked when a text document has been saved.
*
* @param {vscode.TextDocument} doc The underlying text document.
*/
public async onDidSaveTextDocument(doc: vscode.TextDocument) {
await this._QUEUE.add(async () => {
await this.onFileChange(
ego_contracts.FileChangeType.Saved, doc.uri,
doc
);
});
}
/**
* @inheritdoc
*/
protected onDispose() {
if (!this.isInitialized) {
return;
}
ego_helpers.tryDispose(this.context.fileWatcher);
// config imports
ego_workspaces_config.disposeConfigImports.apply(
this
);
// events
ego_workspaces_events.disposeEvents.apply(
this
);
// jobs
ego_workspaces_jobs.disposeJobs.apply(
this
);
// buttons
ego_workspaces_buttons.disposeButtons.apply(
this
);
// apps
ego_workspaces_apps.disposeApps.apply(
this
);
// commands
ego_workspaces_commands.disposeCommands.apply(
this
);
}
private async onDocumentOpened(doc: vscode.TextDocument) {
if (this.isInFinalizeState) {
return;
}
// do not handle items inside
// the following folders
if (this.isInGit(doc.fileName)) {
return;
}
if (this.isInVscode(doc.fileName)) {
return;
}
const EVENT_TYPE = 'document.opened';
const EVENTS = this.getEventsBy(EVENT_TYPE);
for (const E of EVENTS) {
try {
await Promise.resolve(
E.execute(EVENT_TYPE,
doc)
);
} catch (e) {
this.logger
.trace(e, 'workspace.onDocumentOpened(1)');
}
}
}
private async onFileChange(
type: ego_contracts.FileChangeType, file: vscode.Uri,
doc?: vscode.TextDocument,
...args: any[]
) {
if (this.isInFinalizeState) {
return;
}
// do not handle items inside
// the following folders
if (this.isInGit(file.fsPath)) {
return;
}
if (this.isInVscode(file.fsPath)) {
return;
}
if (arguments.length < 3) {
const EDITORS = ego_helpers.asArray(
vscode.window.visibleTextEditors
);
for (const E of EDITORS) {
const EDITORS_DOC = E.document;
if (EDITORS_DOC) {
if (!ego_helpers.isEmptyString(EDITORS_DOC.fileName)) {
if (path.resolve(EDITORS_DOC.fileName) === path.resolve(file.fsPath)) {
doc = EDITORS_DOC;
}
}
}
}
}
let eventType: string;
switch (type) {
case ego_contracts.FileChangeType.Changed:
eventType = 'file.changed';
break;
case ego_contracts.FileChangeType.Created:
eventType = 'file.created';
break;
case ego_contracts.FileChangeType.Deleted:
eventType = 'file.deleted';
break;
case ego_contracts.FileChangeType.Saved:
eventType = 'file.saved';
break;
case ego_contracts.FileChangeType.WillSave:
eventType = 'file.willsave';
break;
}
const EVENTS = this.getEventsBy(eventType);
for (const E of EVENTS) {
try {
await Promise.resolve(
E.execute.apply(
E,
[eventType, type, file, doc].concat(args)
)
);
} catch (e) {
this.logger
.trace(e, 'workspace.onFileChange(1)');
}
}
}
/**
* Is invoked when a text document is going to be saved.
*
* @param {vscode.TextDocumentWillSaveEvent} e The event arguments.
*/
public async onWillSaveTextDocument(e: vscode.TextDocumentWillSaveEvent) {
await this._QUEUE.add(async () => {
await this.onFileChange(
ego_contracts.FileChangeType.WillSave, e.document.uri,
e.document,
e,
);
});
}
/**
* Gets the output channel.
*/
public get output() {
return this.context.output;
}
/**
* Raises the event that config imports have changed.
*/
public async raiseConfigImportsChanged() {
await this._QUEUE.add(async () => {
await this.reloadConfiguration();
});
}
private async reloadConfiguration() {
if (this.isInFinalizeState) {
return;
}
if (!this.isInitialized) {
return;
}
// load settings
this._settings = await ego_workspaces_config.loadSettings.apply(
this
);
this._scriptStates = {};
// commands
await ego_workspaces_commands.reloadCommands.apply(
this
);
// apps
await ego_workspaces_apps.reloadApps.apply(
this
);
// buttons
await ego_workspaces_buttons.reloadButtons.apply(
this
);
// jobs
await ego_workspaces_jobs.reloadJobs.apply(
this
);
// events
await ego_workspaces_events.reloadEvents.apply(
this
);
// startups !!!THIS HAS TO BE DONE AT LAST!!!
await ego_workspaces_npm.runNPMStartupTasks.apply(
this
);
await ego_workspaces_startup.onStartup.apply(
this
);
}
/**
* Handles a value as string and replaces placeholders.
*
* @param {any} val The input value.
*
* @return {string} The output value.
*/
public replaceValues(val: any): string {
val = ego_helpers.toStringSafe(val);
if (!this.isInFinalizeState) {
if (this.isInitialized) {
val = ego_values.replaceValues(this.settings, val, {
buildInValues: this.getValues(),
pathResolver: (p: string) => {
return this.resolveValuePath(p);
},
});
}
}
return val;
}
private resolveValuePath(p: string) {
p = ego_helpers.toStringSafe(p);
if (path.isAbsolute(p)) {
p = path.resolve(p);
if (fsExtra.existsSync(p)) {
return p;
}
} else {
for (const LU of this.getFolderLookups()) {
const FULL_PATH = path.resolve(
path.join(LU, p)
);
if (fsExtra.existsSync(FULL_PATH)) {
return FULL_PATH;
}
}
}
return false;
}
/**
* Runs a shell command for that workspace and shows it progress in the GUI.
*
* @param {ego_contracts.WithShellCommand} settings Settings with the command to run.
* @param {ego_contracts.RunShellCommandOptions} [opts] Custom options.
*/
public async runShellCommand(settings: ego_contracts.WithShellCommand, opts?: ego_contracts.RunShellCommandOptions) {
if (!opts) {
opts = <any>{};
}
const COMMAND_TO_EXECUTE = this.replaceValues(