/
extensionsWatcher.ts
145 lines (125 loc) · 6.12 KB
/
extensionsWatcher.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Emitter, Event } from 'vs/base/common/event';
import { Disposable } from 'vs/base/common/lifecycle';
import { URI } from 'vs/base/common/uri';
import { INativeEnvironmentService } from 'vs/platform/environment/common/environment';
import { DidUninstallExtensionEvent, IExtensionManagementService, ILocalExtension, InstallExtensionEvent, InstallExtensionResult } from 'vs/platform/extensionManagement/common/extensionManagement';
import { areSameExtensions } from 'vs/platform/extensionManagement/common/extensionManagementUtil';
import { IExtensionIdentifier } from 'vs/platform/extensions/common/extensions';
import { FileChangeType, IFileChange, IFileService } from 'vs/platform/files/common/files';
import { ILogService } from 'vs/platform/log/common/log';
import { IUriIdentityService } from 'vs/platform/uriIdentity/common/uriIdentity';
export class ExtensionsWatcher extends Disposable {
private readonly _onDidChangeExtensionsByAnotherSource = this._register(new Emitter<{ added: ILocalExtension[]; removed: IExtensionIdentifier[] }>());
readonly onDidChangeExtensionsByAnotherSource = this._onDidChangeExtensionsByAnotherSource.event;
private startTimestamp = 0;
private installingExtensions: IExtensionIdentifier[] = [];
private installedExtensions: IExtensionIdentifier[] | undefined;
constructor(
private readonly extensionsManagementService: IExtensionManagementService,
@IFileService fileService: IFileService,
@INativeEnvironmentService environmentService: INativeEnvironmentService,
@ILogService private readonly logService: ILogService,
@IUriIdentityService private readonly uriIdentityService: IUriIdentityService,
) {
super();
this.extensionsManagementService.getInstalled().then(extensions => {
this.installedExtensions = extensions.map(e => e.identifier);
this.startTimestamp = Date.now();
});
this._register(extensionsManagementService.onInstallExtension(e => this.onInstallExtension(e)));
this._register(extensionsManagementService.onDidInstallExtensions(e => this.onDidInstallExtensions(e)));
this._register(extensionsManagementService.onDidUninstallExtension(e => this.onDidUninstallExtension(e)));
const extensionsResource = URI.file(environmentService.extensionsPath);
this._register(fileService.watch(extensionsResource));
this._register(Event.filter(fileService.onDidFilesChange, e => e.rawChanges.some(change => this.doesChangeAffects(change, extensionsResource)))(() => this.onDidChange()));
}
private doesChangeAffects(change: IFileChange, extensionsResource: URI): boolean {
// Only interested in added/deleted changes
if (change.type !== FileChangeType.ADDED && change.type !== FileChangeType.DELETED) {
return false;
}
// Is not immediate child of extensions resource
if (!this.uriIdentityService.extUri.isEqual(this.uriIdentityService.extUri.dirname(change.resource), extensionsResource)) {
return false;
}
// .obsolete file changed
if (this.uriIdentityService.extUri.isEqual(change.resource, this.uriIdentityService.extUri.joinPath(extensionsResource, '.obsolete'))) {
return true;
}
// Ingore changes to files starting with `.`
if (this.uriIdentityService.extUri.basename(change.resource).startsWith('.')) {
return false;
}
return true;
}
private onInstallExtension(e: InstallExtensionEvent): void {
this.addInstallingExtension(e.identifier);
}
private onDidInstallExtensions(results: readonly InstallExtensionResult[]): void {
for (const e of results) {
this.removeInstallingExtension(e.identifier);
if (e.local) {
this.addInstalledExtension(e.identifier);
}
}
}
private onDidUninstallExtension(e: DidUninstallExtensionEvent): void {
if (!e.error) {
this.removeInstalledExtension(e.identifier);
}
}
private addInstallingExtension(extension: IExtensionIdentifier) {
this.removeInstallingExtension(extension);
this.installingExtensions.push(extension);
}
private removeInstallingExtension(identifier: IExtensionIdentifier) {
this.installingExtensions = this.installingExtensions.filter(e => !areSameExtensions(e, identifier));
}
private addInstalledExtension(extension: IExtensionIdentifier): void {
if (this.installedExtensions) {
this.removeInstalledExtension(extension);
this.installedExtensions.push(extension);
}
}
private removeInstalledExtension(identifier: IExtensionIdentifier): void {
if (this.installedExtensions) {
this.installedExtensions = this.installedExtensions.filter(e => !areSameExtensions(e, identifier));
}
}
private async onDidChange(): Promise<void> {
if (this.installedExtensions) {
const extensions = await this.extensionsManagementService.getInstalled();
const added = extensions.filter(e => {
if ([...this.installingExtensions, ...this.installedExtensions!].some(identifier => areSameExtensions(identifier, e.identifier))) {
return false;
}
if (e.installedTimestamp && e.installedTimestamp > this.startTimestamp) {
this.logService.info('Detected extension installed from another source', e.identifier.id);
return true;
} else {
this.logService.info('Ignored extension installed by another source because of invalid timestamp', e.identifier.id);
return false;
}
});
const removed = this.installedExtensions.filter(identifier => {
// Extension being installed
if (this.installingExtensions.some(installingExtension => areSameExtensions(installingExtension, identifier))) {
return false;
}
if (extensions.every(e => !areSameExtensions(e.identifier, identifier))) {
this.logService.info('Detected extension removed from another source', identifier.id);
return true;
}
return false;
});
this.installedExtensions = extensions.map(e => e.identifier);
if (added.length || removed.length) {
this._onDidChangeExtensionsByAnotherSource.fire({ added, removed });
}
}
}
}