This repository was archived by the owner on Sep 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Expand file tree
/
Copy pathExtensionManager.js
More file actions
610 lines (558 loc) · 25.1 KB
/
ExtensionManager.js
File metadata and controls
610 lines (558 loc) · 25.1 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
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
/*
* Copyright (c) 2013 Adobe Systems Incorporated. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
/*jslint vars: true, plusplus: true, devel: true, nomen: true, regexp: true, indent: 4, maxerr: 50 */
/*global define, window, $, brackets, semver */
/*unittests: ExtensionManager*/
/**
* The ExtensionManager fetches/caches the extension registry and provides
* information about the status of installed extensions. ExtensionManager raises the
* following events:
* statusChange - indicates that an extension has been installed/uninstalled or
* its status has otherwise changed. Second parameter is the id of the
* extension.
* registryUpdate - indicates that an existing extension was synchronized
* with new data from the registry.
*/
define(function (require, exports, module) {
"use strict";
var _ = require("thirdparty/lodash"),
FileUtils = require("file/FileUtils"),
Package = require("extensibility/Package"),
Async = require("utils/Async"),
ExtensionLoader = require("utils/ExtensionLoader"),
ExtensionUtils = require("utils/ExtensionUtils"),
FileSystem = require("filesystem/FileSystem"),
Strings = require("strings"),
StringUtils = require("utils/StringUtils"),
ThemeManager = require("themes/ThemeManager");
// semver.browser is an AMD-compatible module
var semver = require("extensibility/node/node_modules/semver/semver.browser");
/**
* Extension status constants.
*/
var ENABLED = "enabled",
START_FAILED = "startFailed";
/**
* Extension location constants.
*/
var LOCATION_DEFAULT = "default",
LOCATION_DEV = "dev",
LOCATION_USER = "user",
LOCATION_UNKNOWN = "unknown";
/**
* @private
* @type {Object.<string, {metadata: Object, path: string, status: string}>}
* The set of all known extensions, both from the registry and locally installed.
* The keys are either "name" from package.json (for extensions that have package metadata)
* or the last segment of local file paths (for installed legacy extensions
* with no package metadata). The fields of each record are:
* registryInfo: object containing the info for this id from the main registry (containing metadata, owner,
* and versions). This will be null for legacy extensions.
* installInfo: object containing the info for a locally-installed extension:
* metadata: the package metadata loaded from the local package.json, or null if it's a legacy extension.
* This will be different from registryInfo.metadata if there's a newer version in the registry.
* path: the local path to the extension folder on disk
* locationType: general type of installation; one of the LOCATION_* constants above
* status: the current status, one of the status constants above
*/
var extensions = {};
/**
* Requested changes to the installed extensions.
*/
var _idsToRemove = [],
_idsToUpdate = [];
/**
* @private
* Synchronizes the information between the public registry and the installed
* extensions. Specifically, this makes the `owner` available in each and sets
* an `updateAvailable` flag.
*
* @param {string} id of the extension to synchronize
*/
function synchronizeEntry(id) {
var entry = extensions[id];
// Do nothing if we only have one set of data
if (!entry || !entry.installInfo || !entry.registryInfo) {
return;
}
entry.installInfo.owner = entry.registryInfo.owner;
// Assume false
entry.installInfo.updateAvailable = false;
entry.registryInfo.updateAvailable = false;
entry.installInfo.updateCompatible = false;
entry.registryInfo.updateCompatible = false;
var currentVersion = entry.installInfo.metadata ? entry.installInfo.metadata.version : null;
if (currentVersion && semver.lt(currentVersion, entry.registryInfo.metadata.version)) {
// Note: available update may still be incompatible; we check for this when rendering the Update button in ExtensionManagerView._renderItem()
entry.registryInfo.updateAvailable = true;
entry.installInfo.updateAvailable = true;
// Calculate updateCompatible to check if there's an update for current version of Brackets
var lastCompatibleVersionInfo = _.findLast(entry.registryInfo.versions, function (versionInfo) {
return semver.satisfies(brackets.metadata.apiVersion, versionInfo.brackets);
});
if (lastCompatibleVersionInfo && lastCompatibleVersionInfo.version && semver.lt(currentVersion, lastCompatibleVersionInfo.version)) {
entry.installInfo.updateCompatible = true;
entry.registryInfo.updateCompatible = true;
entry.installInfo.lastCompatibleVersion = lastCompatibleVersionInfo.version;
entry.registryInfo.lastCompatibleVersion = lastCompatibleVersionInfo.version;
}
}
$(exports).triggerHandler("registryUpdate", [id]);
}
/**
* @private
* Verifies if an extension is a theme based on the presence of the field "theme"
* in the package.json. If it is a theme, then the theme file is just loaded by the
* ThemeManager
*/
function loadTheme(id) {
var extension = extensions[id];
if ( extension.installInfo && extension.installInfo.metadata && extension.installInfo.metadata.theme ) {
ThemeManager.loadFile(extension.installInfo.path + "/" + extension.installInfo.metadata.theme);
}
}
/**
* @private
* Sets our data. For unit testing only.
*/
function _setExtensions(newExtensions) {
exports.extensions = extensions = newExtensions;
Object.keys(extensions).forEach(function (id) {
synchronizeEntry(id);
});
}
/**
* @private
* Clears out our existing data. For unit testing only.
*/
function _reset() {
exports.extensions = extensions = {};
_idsToRemove = [];
_idsToUpdate = [];
}
/**
* Downloads the registry of Brackets extensions and stores the information in our
* extension info.
*
* @return {$.Promise} a promise that's resolved with the registry JSON data
* or rejected if the server can't be reached.
*/
function downloadRegistry() {
var result = new $.Deferred();
$.ajax({
url: brackets.config.extension_registry,
dataType: "json",
cache: false
})
.done(function (data) {
Object.keys(data).forEach(function (id) {
if (!extensions[id]) {
extensions[id] = {};
}
extensions[id].registryInfo = data[id];
synchronizeEntry(id);
});
$(exports).triggerHandler("registryDownload");
result.resolve();
})
.fail(function () {
result.reject();
});
return result.promise();
}
/**
* @private
* When an extension is loaded, fetches the package.json and stores the extension in our map.
* @param {$.Event} e The event object
* @param {string} path The local path of the loaded extension's folder.
*/
function _handleExtensionLoad(e, path) {
function setData(id, metadata) {
var locationType,
userExtensionPath = ExtensionLoader.getUserExtensionPath();
if (path.indexOf(userExtensionPath) === 0) {
locationType = LOCATION_USER;
} else {
var segments = path.split("/"), parent;
if (segments.length > 2) {
parent = segments[segments.length - 2];
}
if (parent === "dev") {
locationType = LOCATION_DEV;
} else if (parent === "default") {
locationType = LOCATION_DEFAULT;
} else {
locationType = LOCATION_UNKNOWN;
}
}
if (!extensions[id]) {
extensions[id] = {};
}
extensions[id].installInfo = {
metadata: metadata,
path: path,
locationType: locationType,
status: (e.type === "loadFailed" ? START_FAILED : ENABLED)
};
synchronizeEntry(id);
loadTheme(id);
$(exports).triggerHandler("statusChange", [id]);
}
ExtensionUtils.loadPackageJson(path)
.done(function (metadata) {
setData(metadata.name, metadata);
})
.fail(function () {
// If there's no package.json, this is a legacy extension. It was successfully loaded,
// but we don't have an official ID or metadata for it, so we just create an id and
// "title" for it (which is the last segment of its pathname)
// and record that it's enabled.
var match = path.match(/\/([^\/]+)$/),
name = (match && match[1]) || path,
metadata = { name: name, title: name };
setData(name, metadata);
});
}
/**
* Determines if the given versions[] entry is compatible with the given Brackets API version, and if not
* specifies why.
* @param {Object} extVersion
* @param {string} apiVersion
* @return {{isCompatible: boolean, requiresNewer: ?boolean, compatibleVersion: ?string}}
*/
function getCompatibilityInfoForVersion(extVersion, apiVersion) {
var requiredVersion = (extVersion.brackets || (extVersion.engines && extVersion.engines.brackets)),
result = {};
result.isCompatible = !requiredVersion || semver.satisfies(apiVersion, requiredVersion);
if (result.isCompatible) {
result.compatibleVersion = extVersion.version;
} else {
// Find out reason for incompatibility
if (requiredVersion.charAt(0) === '<') {
result.requiresNewer = false;
} else if (requiredVersion.charAt(0) === '>') {
result.requiresNewer = true;
} else if (requiredVersion.charAt(0) === "~") {
var compareVersion = requiredVersion.slice(1);
// Need to add .0s to this style of range in order to compare (since valid version
// numbers must have major/minor/patch).
if (compareVersion.match(/^[0-9]+$/)) {
compareVersion += ".0.0";
} else if (compareVersion.match(/^[0-9]+\.[0-9]+$/)) {
compareVersion += ".0";
}
result.requiresNewer = semver.lt(apiVersion, compareVersion);
}
}
return result;
}
/**
* Finds the newest version of the entry that is compatible with the given Brackets API version, if any.
* @param {Object} entry The registry entry to check.
* @param {string} apiVersion The Brackets API version to check against.
* @return {{isCompatible: boolean, requiresNewer: ?boolean, compatibleVersion: ?string, isLatestVersion: boolean}}
* Result contains an "isCompatible" member saying whether it's compatible. If compatible, "compatibleVersion"
* specifies the newest version that is compatible and "isLatestVersion" indicates if this is the absolute
* latest version of the extension or not. If !isCompatible or !isLatestVersion, "requiresNewer" says whether
* the latest version is incompatible due to requiring a newer (vs. older) version of Brackets.
*/
function getCompatibilityInfo(entry, apiVersion) {
if (!entry.versions) {
var fallback = getCompatibilityInfoForVersion(entry.metadata, apiVersion);
if (fallback.isCompatible) {
fallback.isLatestVersion = true;
}
return fallback;
}
var i = entry.versions.length - 1,
latestInfo = getCompatibilityInfoForVersion(entry.versions[i], apiVersion);
if (latestInfo.isCompatible) {
latestInfo.isLatestVersion = true;
return latestInfo;
} else {
// Look at earlier versions (skipping very latest version since we already checked it)
for (i--; i >= 0; i--) {
var compatInfo = getCompatibilityInfoForVersion(entry.versions[i], apiVersion);
if (compatInfo.isCompatible) {
compatInfo.isLatestVersion = false;
compatInfo.requiresNewer = latestInfo.requiresNewer;
return compatInfo;
}
}
// No version is compatible, so just return info for the latest version
return latestInfo;
}
}
/**
* Given an extension id and version number, returns the URL for downloading that extension from
* the repository. Does not guarantee that the extension exists at that URL.
* @param {string} id The extension's name from the metadata.
* @param {string} version The version to download.
* @return {string} The URL to download the extension from.
*/
function getExtensionURL(id, version) {
return StringUtils.format(brackets.config.extension_url, id, version);
}
/**
* Removes the installed extension with the given id.
* @param {string} id The id of the extension to remove.
* @return {$.Promise} A promise that's resolved when the extension is removed or
* rejected with an error if there's a problem with the removal.
*/
function remove(id) {
var result = new $.Deferred();
if (extensions[id] && extensions[id].installInfo) {
Package.remove(extensions[id].installInfo.path)
.done(function () {
extensions[id].installInfo = null;
result.resolve();
$(exports).triggerHandler("statusChange", [id]);
})
.fail(function (err) {
result.reject(err);
});
} else {
result.reject(StringUtils.format(Strings.EXTENSION_NOT_INSTALLED, id));
}
return result.promise();
}
/**
* Updates an installed extension with the given package file.
* @param {string} id of the extension
* @param {string} packagePath path to the package file
* @return {$.Promise} A promise that's resolved when the extension is updated or
* rejected with an error if there's a problem with the update.
*/
function update(id, packagePath) {
return Package.installUpdate(packagePath, id);
}
/**
* Deletes any temporary files left behind by extensions that
* were marked for update.
*/
function cleanupUpdates() {
Object.keys(_idsToUpdate).forEach(function (id) {
var filename = _idsToUpdate[id].localPath;
if (filename) {
FileSystem.getFileForPath(filename).unlink();
}
});
_idsToUpdate = {};
}
/**
* Unmarks all extensions marked for removal.
*/
function unmarkAllForRemoval() {
_idsToRemove = {};
}
/**
* Marks an extension for later removal, or unmarks an extension previously marked.
* @param {string} id The id of the extension to mark for removal.
* @param {boolean} mark Whether to mark or unmark it.
*/
function markForRemoval(id, mark) {
if (mark) {
_idsToRemove[id] = true;
} else {
delete _idsToRemove[id];
}
$(exports).triggerHandler("statusChange", [id]);
}
/**
* Returns true if an extension is marked for removal.
* @param {string} id The id of the extension to check.
* @return {boolean} true if it's been marked for removal, false otherwise.
*/
function isMarkedForRemoval(id) {
return !!(_idsToRemove[id]);
}
/**
* Returns true if there are any extensions marked for removal.
* @return {boolean} true if there are extensions to remove
*/
function hasExtensionsToRemove() {
return Object.keys(_idsToRemove).length > 0;
}
/**
* If a downloaded package appears to be an update, mark the extension for update.
* If an extension was previously marked for removal, marking for update will
* turn off the removal mark.
* @param {Object} installationResult info about the install provided by the Package.download function
*/
function updateFromDownload(installationResult) {
var installationStatus = installationResult.installationStatus;
if (installationStatus === Package.InstallationStatuses.ALREADY_INSTALLED ||
installationStatus === Package.InstallationStatuses.NEEDS_UPDATE ||
installationStatus === Package.InstallationStatuses.SAME_VERSION ||
installationStatus === Package.InstallationStatuses.OLDER_VERSION) {
var id = installationResult.name;
delete _idsToRemove[id];
_idsToUpdate[id] = installationResult;
$(exports).triggerHandler("statusChange", [id]);
}
}
/**
* Removes the mark for an extension to be updated on restart. Also deletes the
* downloaded package file.
* @param {string} id The id of the extension for which the update is being removed
*/
function removeUpdate(id) {
var installationResult = _idsToUpdate[id];
if (!installationResult) {
return;
}
if (installationResult.localPath) {
FileSystem.getFileForPath(installationResult.localPath).unlink();
}
delete _idsToUpdate[id];
$(exports).triggerHandler("statusChange", [id]);
}
/**
* Returns true if an extension is marked for update.
* @param {string} id The id of the extension to check.
* @return {boolean} true if it's been marked for update, false otherwise.
*/
function isMarkedForUpdate(id) {
return !!(_idsToUpdate[id]);
}
/**
* Returns true if there are any extensions marked for update.
* @return {boolean} true if there are extensions to update
*/
function hasExtensionsToUpdate() {
return Object.keys(_idsToUpdate).length > 0;
}
/**
* Removes extensions previously marked for removal.
* @return {$.Promise} A promise that's resolved when all extensions are removed, or rejected
* if one or more extensions can't be removed. When rejected, the argument will be an
* array of error objects, each of which contains an "item" property with the id of the
* failed extension and an "error" property with the actual error.
*/
function removeMarkedExtensions() {
return Async.doInParallel_aggregateErrors(
Object.keys(_idsToRemove),
function (id) {
return remove(id);
}
);
}
/**
* Updates extensions previously marked for update.
* @return {$.Promise} A promise that's resolved when all extensions are updated, or rejected
* if one or more extensions can't be updated. When rejected, the argument will be an
* array of error objects, each of which contains an "item" property with the id of the
* failed extension and an "error" property with the actual error.
*/
function updateExtensions() {
return Async.doInParallel_aggregateErrors(
Object.keys(_idsToUpdate),
function (id) {
var installationResult = _idsToUpdate[id];
return update(installationResult.name, installationResult.localPath);
}
);
}
/**
* Gets an array of extensions that are currently installed and can be updated to a new version
* @return {Array.<{id: string, installVersion: string, registryVersion: string}>}
* where id = extensionId
* installVersion = currently installed version of extension
* registryVersion = latest version compatible with current Brackets
*/
function getAvailableUpdates() {
var result = [];
Object.keys(extensions).forEach(function (extensionId) {
var extensionInfo = extensions[extensionId];
// skip extensions that are not installed or are not in the registry
if (!extensionInfo.installInfo || !extensionInfo.registryInfo) {
return;
}
if (extensionInfo.registryInfo.updateCompatible) {
result.push({
id: extensionId,
installVersion: extensionInfo.installInfo.metadata.version,
registryVersion: extensionInfo.registryInfo.lastCompatibleVersion
});
}
});
return result;
}
/**
* Takes the array returned from getAvailableUpdates() as an input and removes those entries
* that are no longer current - when currently installed version of an extension
* is equal or newer than registryVersion returned by getAvailableUpdates().
* This function is designed to work without the necessity to download extension registry
* @param {Array.<{id: string, installVersion: string, registryVersion: string}>} updates
* previous output of getAvailableUpdates()
* @return {Array.<{id: string, installVersion: string, registryVersion: string}>}
* filtered input as function description
*/
function cleanAvailableUpdates(updates) {
return updates.reduce(function (arr, updateInfo) {
var extDefinition = extensions[updateInfo.id];
if (!extDefinition || !extDefinition.installInfo) {
// extension has been uninstalled in the meantime
return arr;
}
var installedVersion = extDefinition.installInfo.metadata.version;
if (semver.lt(installedVersion, updateInfo.registryVersion)) {
arr.push(updateInfo);
}
return arr;
}, []);
}
// Listen to extension load and loadFailed events
$(ExtensionLoader)
.on("load", _handleExtensionLoad)
.on("loadFailed", _handleExtensionLoad);
// Public exports
exports.downloadRegistry = downloadRegistry;
exports.getCompatibilityInfo = getCompatibilityInfo;
exports.getExtensionURL = getExtensionURL;
exports.remove = remove;
exports.update = update;
exports.extensions = extensions;
exports.cleanupUpdates = cleanupUpdates;
exports.markForRemoval = markForRemoval;
exports.isMarkedForRemoval = isMarkedForRemoval;
exports.unmarkAllForRemoval = unmarkAllForRemoval;
exports.hasExtensionsToRemove = hasExtensionsToRemove;
exports.updateFromDownload = updateFromDownload;
exports.removeUpdate = removeUpdate;
exports.isMarkedForUpdate = isMarkedForUpdate;
exports.hasExtensionsToUpdate = hasExtensionsToUpdate;
exports.removeMarkedExtensions = removeMarkedExtensions;
exports.updateExtensions = updateExtensions;
exports.getAvailableUpdates = getAvailableUpdates;
exports.cleanAvailableUpdates = cleanAvailableUpdates;
exports.ENABLED = ENABLED;
exports.START_FAILED = START_FAILED;
exports.LOCATION_DEFAULT = LOCATION_DEFAULT;
exports.LOCATION_DEV = LOCATION_DEV;
exports.LOCATION_USER = LOCATION_USER;
exports.LOCATION_UNKNOWN = LOCATION_UNKNOWN;
// For unit testing only
exports._reset = _reset;
exports._setExtensions = _setExtensions;
});