-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathUpdate.js
More file actions
130 lines (122 loc) · 6 KB
/
Update.js
File metadata and controls
130 lines (122 loc) · 6 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
/*
* Copyright (c) 2012 Massachusetts Institute of Technology, Adobe Systems
* Incorporated, and other contributors. 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, indent: 4, maxerr: 50 */
/*global define, $, brackets, Mustache */
define(function (require, exports, module) {
"use strict";
var Commands = brackets.getModule("command/Commands");
var CommandManager = brackets.getModule("command/CommandManager");
var Dialogs = brackets.getModule("widgets/Dialogs");
var ExtensionManager = brackets.getModule("extensibility/ExtensionManager");
var Strings = require("./strings");
var Preferences = require("./Preferences");
/**
* @private
* @return {$.Promise} A promise object that is resolved with {hasUpdate: true/false, version: version string}
*/
function _checkForUpdate() {
var result = new $.Deferred();
ExtensionManager.downloadRegistry().done(function () {
var theseus = ExtensionManager.extensions.theseus;
if (!theseus.registryInfo.updateAvailable) {
result.resolve({hasUpdateAvailable: false});
} else {
var bracketsVersion = brackets.metadata.version;
var theseusVersions = theseus.registryInfo.versions;
var currentTheseusVersion = theseus.installInfo.metadata.version;
for (var i = theseusVersions.length - 1; i >= 0; i--) { //Find the latest compatible version
var version = theseusVersions[i];
if (version.version === currentTheseusVersion) { //Already checked all newer versions?
break; //Quit
}
var checkAgainst = { metadata: { engines: { brackets: version.brackets }}}; //Assemble the data from each version into a format that getCompatibilityInfo likes
var compatibility = ExtensionManager.getCompatibilityInfo(checkAgainst, bracketsVersion);
if (compatibility.isCompatible) { //Is the update compatible with this Brackets?
result.resolve({ //Cool, there's an update available
hasUpdateAvailable: true,
version: version.version,
current: currentTheseusVersion
});
break; //At this point, the newest compatible update has been resolved. Quit.
}
}
result.resolve({hasUpdateAvailable: false}); //Nothing was found: no update available
}
});
return result.promise();
}
/**
* @private
* Open the extension manager and (try to) search for Theseus
*/
function _doUpdate() {
CommandManager.execute(Commands.FILE_EXTENSION_MANAGER).done(function () {
var $searchBox = $(".extension-manager-dialog .search");
$searchBox.prop("value", "Theseus");
setTimeout(function () {
$searchBox.trigger("input");
}, 800);
});
}
/**
* Alerts user that Theseus is updatable and prompts to upgrade
*/
function showUpdateDialog(newVersion, currentVersion) {
var newVersionDialogHTML = require("text!./NewVersionAvailable.html");
var newVersionDialogTemplate = Mustache.render(newVersionDialogHTML, {Strings : Strings});
var dialog = Dialogs.showModalDialogUsingTemplate(newVersionDialogTemplate);
var $dialog = dialog.getElement();
$dialog.find(".dialog-message").html(currentVersion + " → " + newVersion);
$dialog.find(".close").on("click", dialog.close.bind(dialog));
$dialog.on("hide", function () {
if ($dialog.data("buttonId") === "upgrade") {
_doUpdate(newVersion);
} else {
Preferences.set("update.update_ignored", true);
Preferences.set("update.last_ignored_version", newVersion, true);
}
});
}
/**
* Determines if a Theseus update is necessary and, if so, whether to show the dialog
*/
function updateIfNecessary() {
var lastCheckedAt = Preferences.get("update.last_checked_at");
var now = new Date();
var MILLIS_IN_DAY = 86400000;
if ((now - lastCheckedAt) < MILLIS_IN_DAY) {
return;
}
_checkForUpdate().done(function (update) {
Preferences.set("update.last_checked_at", now.getTime(), true);
if (update.hasUpdateAvailable) {
if (!Preferences.get("update.update_ignored") || Preferences.get("update.last_ignored_version") !== update.version) {
showUpdateDialog(update.version, update.current);
}
}
});
}
exports.updateIfNecessary = updateIfNecessary;
exports.showUpdateDialog = showUpdateDialog;
});