forked from home-sweet-gnome/dash-to-panel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.js
235 lines (194 loc) · 7.19 KB
/
update.js
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
/*
* This file is part of the Dash-To-Panel extension for Gnome 3
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
const GLib = imports.gi.GLib;
const Gio = imports.gi.Gio;
const Soup = imports.gi.Soup;
const FileUtils = imports.misc.fileUtils;
const Me = imports.misc.extensionUtils.getCurrentExtension();
const Gettext = imports.gettext.domain(Me.metadata['gettext-domain']);
const _ = Gettext.gettext;
let apiUrl = '';
function init() {
Me.settings.connect('changed::force-check-update', () => {
if (Me.settings.get_boolean('force-check-update')) {
checkForUpdate(true);
Me.settings.set_boolean('force-check-update', false);
}
});
//!start-update
//check for update now, then every 4 hours
checkForUpdate();
imports.mainloop.timeout_add(14400000, () => checkForUpdate());
Me.settings.connect('changed::version-to-install', () => installRelease(Me.settings.get_string('version-to-install'), true));
//!end-update
}
function checkForUpdate(fromSettings) {
if (!apiUrl) {
return notifyError(_('Unavailable when installed from extensions.gnome.org'));
}
//!start-update
if (fromSettings || Me.settings.get_boolean('check-update')) {
getLatestReleaseInfo((err, latestRelease) => {
if (err) {
return notifyError(err);
}
let latestVersion = latestRelease.tag.substring(1);
if (Me.metadata.version < latestVersion && latestRelease.zipUrl) {
notify(
_('Version %s (%s) is available').format(latestVersion, latestRelease.name),
[{ text: _('Details'), func: () => imports.misc.util.spawn(['xdg-open', latestRelease.url]) },
{ text: _('Update'), func: () => installRelease(latestRelease.zipUrl, fromSettings) }]
);
} else if (fromSettings) {
notify(_('Already up to date'));
}
});
}
//!end-update
}
function notifyError(err) {
Me.imports.utils.notify(_('Error: ') + err, 'dialog-error', null, true);
}
function notify(msg, action) {
Me.imports.utils.notify(msg, 'dialog-information', action, true);
}
//!start-update
apiUrl = 'https://api.github.com/repos/home-sweet-gnome/dash-to-panel/';
const tagsApiUrl = apiUrl + 'tags';
const releasesApiUrl = apiUrl + 'releases';
let httpSession;
function getLatestReleaseInfo(cb) {
getReleaseInfo('/latest', cb);
}
function getTaggedReleaseInfo(releaseTag, cb) {
getReleaseInfo('/tags/' + releaseTag, cb);
}
function getReleaseInfo(suffix, cb) {
getHttpMessageResponseBody(createGet(releasesApiUrl + suffix), (err, body) => {
if (err) {
return cb(err);
}
try {
let release = JSON.parse(body.data);
let releaseInfo = {
name: release.name,
tag: release.tag_name,
url: release.html_url,
zipUrl: (release.assets.length ? release.assets[0].browser_download_url : 0),
};
cb(null, releaseInfo);
} catch (e) {
cb(e.message);
}
});
}
function installRelease(releaseAssetUrl, fromSettings) {
getHttpMessageResponseBody(createGet(releaseAssetUrl), (err, body) => {
if (err) {
return notifyInstallResult(err);
}
let name = 'dtp_install';
let zipFile = createTmp(name + '.zip');
let stream = zipFile.replace(null, false, Gio.FileCreateFlags.NONE, null);
let extDir = Gio.file_new_for_path(Me.path);
let tmpDir = createTmp(name, true);
let bckDir = createTmp(Me.uuid, true);
stream.write_bytes(body.flatten().get_as_bytes(), null);
stream.close(null);
unzipFile(zipFile, tmpDir, err => {
if (err) {
return notifyInstallResult(err);
}
try {
FileUtils.recursivelyMoveDir(extDir, bckDir);
FileUtils.recursivelyMoveDir(tmpDir, extDir);
if (fromSettings) {
let [success, out, err, exitCode] = GLib.spawn_command_line_sync('pidof "gnome-shell-extension-prefs"');
if (success && !exitCode) {
GLib.spawn_command_line_sync('kill -9 ' + imports.byteArray.toString(out));
}
}
} catch (e) {
FileUtils.recursivelyDeleteDir(extDir, false);
FileUtils.recursivelyMoveDir(bckDir, extDir);
return notifyInstallResult(e.message);
}
notifyInstallResult(null);
});
});
}
function notifyInstallResult(err) {
if (err) {
notifyError(err);
} else if (imports.gi.Meta.is_wayland_compositor()) {
notify(
_('Update successful, please log out/in'),
{ text: _('Log out'), func: () => new imports.misc.systemActions.getDefault().activateLogout() }
);
} else {
notify(
_('Update successful, please restart GNOME Shell'),
{ text: _('Restart GNOME Shell'), func: () => imports.gi.Meta.restart(_("Restarting GNOME Shell...")) }
);
}
}
function createTmp(name, isDir) {
let tmp = Gio.file_new_for_path('/tmp/' + name);
if (isDir && tmp.query_exists(null)) {
FileUtils.recursivelyDeleteDir(tmp, false);
}
return tmp;
}
function unzipFile(zipFile, destDir, cb) {
let [success, pid] = GLib.spawn_async(
null,
['unzip', '-uod', destDir.get_path(), '--', zipFile.get_path()],
null,
GLib.SpawnFlags.SEARCH_PATH | GLib.SpawnFlags.DO_NOT_REAP_CHILD | GLib.SpawnFlags.STDOUT_TO_DEV_NULL,
null
);
if (!success) {
return cb('unzip spawn error');
}
GLib.child_watch_add(GLib.PRIORITY_DEFAULT, pid, (pid, status) => {
GLib.spawn_close_pid(pid);
if (status != 0) {
return cb('extraction error')
}
return cb(null);
});
}
function createGet(url, params) {
return Soup.form_request_new_from_hash('GET', url, params || {});
}
function getHttpMessageResponseBody(message, cb) {
if (!httpSession) {
httpSession = new Soup.Session();
httpSession.user_agent = Me.metadata.uuid;
}
httpSession.queue_message(message, (httpSession, message) => {
try {
if (!message.response_body || !message.response_body.data) {
return cb('No data received');
}
return cb(null, message.response_body);
} catch (e) {
return cb(e.message);
}
});
}
//!end-update