-
-
Notifications
You must be signed in to change notification settings - Fork 392
/
Copy pathplugin.service.ts
243 lines (223 loc) · 7.06 KB
/
plugin.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
"use strict";
import * as vscode from "vscode";
export class ExtensionInformation {
public static fromJSON(text: string) {
try {
// TODO: JSON.parse may throw error
// Throw custom error should be more friendly
const obj = JSON.parse(text);
const meta = new ExtensionMetadata(
obj.meta.galleryApiUrl,
obj.meta.id,
obj.meta.downloadUrl,
obj.meta.publisherId,
obj.meta.publisherDisplayName,
obj.meta.date
);
const item = new ExtensionInformation();
item.metadata = meta;
item.name = obj.name;
item.publisher = obj.publisher;
item.version = obj.version;
return item;
} catch (err) {
throw new Error(err);
}
}
public static fromJSONList(text: string) {
const extList: ExtensionInformation[] = [];
try {
// TODO: JSON.parse may throw error
// Throw custom error should be more friendly
const list = JSON.parse(text);
list.forEach(obj => {
const meta = new ExtensionMetadata(
obj.metadata.galleryApiUrl,
obj.metadata.id,
obj.metadata.downloadUrl,
obj.metadata.publisherId,
obj.metadata.publisherDisplayName,
obj.metadata.date
);
const item = new ExtensionInformation();
item.metadata = meta;
item.name = obj.name;
item.publisher = obj.publisher;
item.version = obj.version;
if (item.name !== "code-settings-sync") {
extList.push(item);
}
});
} catch (err) {
throw new Error(err);
}
return extList;
}
public metadata: ExtensionMetadata;
public name: string;
public version: string;
public publisher: string;
}
export class ExtensionMetadata {
constructor(
public galleryApiUrl: string,
public id: string,
public downloadUrl: string,
public publisherId: string,
public publisherDisplayName: string,
public date: string
) {}
}
export class PluginService {
public static GetMissingExtensions(
remoteExt: string,
ignoredExtensions: string[]
) {
const remoteList = ExtensionInformation.fromJSONList(remoteExt);
const localList = this.CreateExtensionList();
return remoteList.filter(
ext =>
!ignoredExtensions.includes(ext.name) &&
!localList.map(e => e.name).includes(ext.name)
);
}
public static GetDeletedExtensions(
remoteExtensions: ExtensionInformation[],
ignoredExtensions: string[]
) {
const localExtensions = this.CreateExtensionList();
// for (var i = 0; i < remoteList.length; i++) {
// var ext = remoteList[i];
// var found: boolean = false;
// for (var j = 0; j < localList.length; j++) {
// var localExt = localList[j];
// if (ext.name == localExt.name) {
// found = true;
// break;
// }
// }
// if (!found) {
// deletedList.push(localExt);
// }
// }
return localExtensions.filter(
ext =>
ext.name !== "code-settings-sync" &&
!remoteExtensions.map(e => e.name).includes(ext.name) &&
!ignoredExtensions.includes(ext.name)
);
}
public static CreateExtensionList() {
return vscode.extensions.all
.filter(ext => !ext.packageJSON.isBuiltin)
.map(ext => {
const meta = ext.packageJSON.__metadata || {
id: ext.packageJSON.uuid,
publisherId: ext.id,
publisherDisplayName: ext.packageJSON.publisher
};
const data = new ExtensionMetadata(
meta.galleryApiUrl,
meta.id,
meta.downloadUrl,
meta.publisherId,
meta.publisherDisplayName,
meta.date
);
const info = new ExtensionInformation();
info.metadata = data;
info.name = ext.packageJSON.name;
info.publisher = ext.packageJSON.publisher;
info.version = ext.packageJSON.version;
return info;
});
}
public static async DeleteExtension(
extension: ExtensionInformation
): Promise<boolean> {
try {
await vscode.commands.executeCommand(
"workbench.extensions.uninstallExtension",
`${extension.publisher}.${extension.name}`
);
return true;
} catch (err) {
throw new Error(err);
}
}
public static async DeleteExtensions(
extensionsJson: string,
ignoredExtensions: string[]
): Promise<ExtensionInformation[]> {
const remoteExtensions = ExtensionInformation.fromJSONList(extensionsJson);
const toDelete = PluginService.GetDeletedExtensions(
remoteExtensions,
ignoredExtensions
);
return Promise.all(
toDelete.map(async selectedExtension => {
try {
await PluginService.DeleteExtension(selectedExtension);
return selectedExtension;
} catch (err) {
throw new Error(
`Sync : Unable to delete extension ${selectedExtension.name} ${selectedExtension.version}: ${err}`
);
}
})
);
}
public static async InstallExtensions(
extensions: string,
ignoredExtensions: string[],
notificationCallBack: (...data: any[]) => void
): Promise<ExtensionInformation[]> {
let addedExtensions: ExtensionInformation[] = [];
const missingExtensions = PluginService.GetMissingExtensions(
extensions,
ignoredExtensions
);
if (missingExtensions.length === 0) {
notificationCallBack("Sync : No Extensions needs to be installed.");
return [];
}
addedExtensions = await PluginService.InstallWithAPI(
missingExtensions,
notificationCallBack
);
return addedExtensions;
}
public static async InstallWithAPI(
missingExtensions: ExtensionInformation[],
notificationCallBack: (...data: any[]) => void
): Promise<ExtensionInformation[]> {
const addedExtensions: ExtensionInformation[] = [];
const missingExtensionsCount = missingExtensions.length;
notificationCallBack("TOTAL EXTENSIONS : " + missingExtensionsCount);
notificationCallBack("");
notificationCallBack("");
for (const ext of missingExtensions) {
const name = ext.publisher + "." + ext.name;
try {
notificationCallBack("");
notificationCallBack(`[x] - EXTENSION: ${ext.name} - INSTALLING`);
await vscode.commands.executeCommand(
"workbench.extensions.installExtension",
name
);
notificationCallBack("");
notificationCallBack(`[x] - EXTENSION: ${ext.name} INSTALLED.`);
notificationCallBack(
` ${missingExtensions.indexOf(ext) +
1} OF ${missingExtensionsCount} INSTALLED`,
true
);
notificationCallBack("");
addedExtensions.push(ext);
} catch (err) {
throw new Error(err);
}
}
return addedExtensions;
}
}