forked from larryaasen/upgrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
appcast.dart
337 lines (300 loc) · 10.9 KB
/
appcast.dart
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
// ignore_for_file: constant_identifier_names
/*
* Copyright (c) 2018-2023 Larry Aasen. All rights reserved.
*/
import 'dart:convert' show utf8;
import 'package:http/http.dart' as http;
import 'package:version/version.dart';
import 'package:xml/xml.dart';
import 'upgrade_os.dart';
import 'upgrader_device.dart';
/// The [Appcast] class is used to download an Appcast, based on the Sparkle
/// framework by Andy Matuschak.
/// Documentation: https://sparkle-project.org/documentation/publishing/
/// An Appcast is an RSS feed with one channel that has a collection of items
/// that each describe one app version.
class Appcast {
/// Provide an HTTP Client that can be replaced during testing.
final http.Client client;
/// Provide [UpgraderOS] that can be replaced during testing.
final UpgraderOS upgraderOS;
/// Provide [UpgraderDevice] that ca be replaced during testing.
final UpgraderDevice upgraderDevice;
Appcast({
http.Client? client,
UpgraderOS? upgraderOS,
UpgraderDevice? upgraderDevice,
}) : client = client ?? http.Client(),
upgraderOS = upgraderOS ?? UpgraderOS(),
upgraderDevice = upgraderDevice ?? UpgraderDevice();
/// The items in the Appcast.
List<AppcastItem>? items;
String? osVersionString;
/// Returns the latest critical item in the Appcast.
AppcastItem? bestCriticalItem() {
if (items == null) {
return null;
}
AppcastItem? bestItem;
items!.forEach((AppcastItem item) {
if (item.hostSupportsItem(
osVersion: osVersionString,
currentPlatform: upgraderOS.current) &&
item.isCriticalUpdate) {
if (bestItem == null) {
bestItem = item;
} else {
try {
final itemVersion = Version.parse(item.versionString!);
final bestItemVersion = Version.parse(bestItem!.versionString!);
if (itemVersion > bestItemVersion) {
bestItem = item;
}
} on Exception catch (e) {
print('upgrader: criticalUpdateItem invalid version: $e');
}
}
}
});
return bestItem;
}
/// Returns the latest item in the Appcast based on OS, OS version, and app
/// version.
AppcastItem? bestItem() {
if (items == null) {
return null;
}
AppcastItem? bestItem;
items!.forEach((AppcastItem item) {
if (item.hostSupportsItem(
osVersion: osVersionString, currentPlatform: upgraderOS.current)) {
if (bestItem == null) {
bestItem = item;
} else {
try {
final itemVersion = Version.parse(item.versionString!);
final bestItemVersion = Version.parse(bestItem!.versionString!);
if (itemVersion > bestItemVersion) {
bestItem = item;
}
} on Exception catch (e) {
print('upgrader: bestItem invalid version: $e');
}
}
}
});
return bestItem;
}
/// Download the Appcast from [appCastURL].
Future<List<AppcastItem>?> parseAppcastItemsFromUri(String appCastURL) async {
http.Response response;
try {
response = await client.get(Uri.parse(appCastURL));
} catch (e) {
print('upgrader: parseAppcastItemsFromUri exception: $e');
return null;
}
final contents = utf8.decode(response.bodyBytes);
return parseAppcastItems(contents);
}
/// Parse the Appcast from XML string.
Future<List<AppcastItem>?> parseAppcastItems(String contents) async {
osVersionString = await upgraderDevice.getOsVersionString(upgraderOS);
return parseItemsFromXMLString(contents);
}
List<AppcastItem>? parseItemsFromXMLString(String xmlString) {
items = null;
if (xmlString.isEmpty) {
return null;
}
try {
// Parse the XML
final document = XmlDocument.parse(xmlString);
// Ensure the root element is valid
document.rootElement;
var localItems = <AppcastItem>[];
// look for all item elements in the rss/channel
document.findAllElements('item').forEach((XmlElement itemElement) {
String? title;
String? itemDescription;
String? dateString;
String? fileURL;
String? maximumSystemVersion;
String? minimumSystemVersion;
String? osString;
String? releaseNotesLink;
final tags = <String>[];
String? newVersion;
String? itemVersion;
String? enclosureVersion;
itemElement.children.forEach((XmlNode childNode) {
if (childNode is XmlElement) {
final name = childNode.name.toString();
if (name == AppcastConstants.ElementTitle) {
title = childNode.innerText;
} else if (name == AppcastConstants.ElementDescription) {
itemDescription = childNode.innerText;
} else if (name == AppcastConstants.ElementEnclosure) {
childNode.attributes.forEach((XmlAttribute attribute) {
if (attribute.name.toString() ==
AppcastConstants.AttributeVersion) {
enclosureVersion = attribute.value;
} else if (attribute.name.toString() ==
AppcastConstants.AttributeOsType) {
osString = attribute.value;
} else if (attribute.name.toString() ==
AppcastConstants.AttributeURL) {
fileURL = attribute.value;
}
});
} else if (name == AppcastConstants.ElementMaximumSystemVersion) {
maximumSystemVersion = childNode.innerText;
} else if (name == AppcastConstants.ElementMinimumSystemVersion) {
minimumSystemVersion = childNode.innerText;
} else if (name == AppcastConstants.ElementPubDate) {
dateString = childNode.innerText;
} else if (name == AppcastConstants.ElementReleaseNotesLink) {
releaseNotesLink = childNode.innerText;
} else if (name == AppcastConstants.ElementTags) {
childNode.children.forEach((XmlNode tagChildNode) {
if (tagChildNode is XmlElement) {
final tagName = tagChildNode.name.toString();
tags.add(tagName);
}
});
} else if (name == AppcastConstants.AttributeVersion) {
itemVersion = childNode.innerText;
}
}
});
if (itemVersion == null) {
newVersion = enclosureVersion;
} else {
newVersion = itemVersion;
}
// There must be a version
if (newVersion == null || newVersion.isEmpty) {
return;
}
final item = AppcastItem(
title: title,
itemDescription: itemDescription,
dateString: dateString,
maximumSystemVersion: maximumSystemVersion,
minimumSystemVersion: minimumSystemVersion,
osString: osString,
releaseNotesURL: releaseNotesLink,
tags: tags,
fileURL: fileURL,
versionString: newVersion,
);
localItems.add(item);
});
items = localItems;
} catch (e) {
print('upgrader: parseItemsFromXMLString exception: $e');
}
return items;
}
}
class AppcastItem {
final String? title;
final String? dateString;
final String? itemDescription;
final String? releaseNotesURL;
final String? minimumSystemVersion;
final String? maximumSystemVersion;
final String? fileURL;
final int? contentLength;
final String? versionString;
final String? osString;
final String? displayVersionString;
final String? infoURL;
final List<String>? tags;
AppcastItem({
this.title,
this.dateString,
this.itemDescription,
this.releaseNotesURL,
this.minimumSystemVersion,
this.maximumSystemVersion,
this.fileURL,
this.contentLength,
this.versionString,
this.osString,
this.displayVersionString,
this.infoURL,
this.tags,
});
/// Returns true if the tags ([AppcastConstants.ElementTags]) contains
/// critical update ([AppcastConstants.ElementCriticalUpdate]).
bool get isCriticalUpdate => tags == null
? false
: tags!.contains(AppcastConstants.ElementCriticalUpdate);
/// Does the host support this item? If so is [osVersion] supported?
bool hostSupportsItem({String? osVersion, required String currentPlatform}) {
assert(currentPlatform.isNotEmpty);
bool supported = true;
if (osString != null && osString!.isNotEmpty) {
final platformEnum = 'TargetPlatform.${osString!}';
currentPlatform = 'TargetPlatform.$currentPlatform';
supported = platformEnum.toLowerCase() == currentPlatform.toLowerCase();
}
if (supported && osVersion != null && osVersion.isNotEmpty) {
Version osVersionValue;
try {
osVersionValue = Version.parse(osVersion);
} catch (e) {
print('upgrader: hostSupportsItem invalid osVersion: $e');
return false;
}
if (maximumSystemVersion != null) {
try {
final maxVersion = Version.parse(maximumSystemVersion!);
if (osVersionValue > maxVersion) {
supported = false;
}
} on Exception catch (e) {
print('upgrader: hostSupportsItem invalid maximumSystemVersion: $e');
}
}
if (supported && minimumSystemVersion != null) {
try {
final minVersion = Version.parse(minimumSystemVersion!);
if (osVersionValue < minVersion) {
supported = false;
}
} on Exception catch (e) {
print('upgrader: hostSupportsItem invalid minimumSystemVersion: $e');
}
}
}
return supported;
}
}
/// These constants taken from:
/// https://github.com/sparkle-project/Sparkle/blob/master/Sparkle/SUConstants.m
class AppcastConstants {
static const String AttributeDeltaFrom = 'sparkle:deltaFrom';
static const String AttributeDSASignature = 'sparkle:dsaSignature';
static const String AttributeEDSignature = 'sparkle:edSignature';
static const String AttributeShortVersionString =
'sparkle:shortVersionString';
static const String AttributeVersion = 'sparkle:version';
static const String AttributeOsType = 'sparkle:os';
static const String ElementCriticalUpdate = 'sparkle:criticalUpdate';
static const String ElementDeltas = 'sparkle:deltas';
static const String ElementMinimumSystemVersion =
'sparkle:minimumSystemVersion';
static const String ElementMaximumSystemVersion =
'sparkle:maximumSystemVersion';
static const String ElementReleaseNotesLink = 'sparkle:releaseNotesLink';
static const String ElementTags = 'sparkle:tags';
static const String AttributeURL = 'url';
static const String AttributeLength = 'length';
static const String ElementDescription = 'description';
static const String ElementEnclosure = 'enclosure';
static const String ElementLink = 'link';
static const String ElementPubDate = 'pubDate';
static const String ElementTitle = 'title';
}