forked from larryaasen/upgrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappcast.dart
302 lines (268 loc) · 9.46 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
/*
* Copyright (c) 2018 Larry Aasen. All rights reserved.
*/
import 'dart:convert' show utf8;
import 'dart:io';
import 'package:device_info/device_info.dart';
import 'package:flutter/foundation.dart';
import 'package:http/http.dart' as http;
import 'package:version/version.dart';
import 'package:xml/xml.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 for mock testing.
http.Client? client;
Appcast({
this.client,
}) {
client ??= http.Client();
}
/// The items in the Appcast.
List<AppcastItem>? items;
late AndroidDeviceInfo _androidInfo;
late IosDeviceInfo _iosInfo;
String? osVersionString;
/// 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)) {
if (bestItem == null ||
Version.parse(item.versionString!) >
Version.parse(bestItem!.versionString!)) {
bestItem = item;
}
}
});
return bestItem;
}
/// Download the Appcast from [appCastURL].
Future<List<AppcastItem>?> parseAppcastItemsFromUri(String appCastURL) async {
await _getDeviceInfo();
http.Response response;
try {
response = await client!.get(Uri.parse(appCastURL));
} catch (e) {
print(e);
return null;
}
final contents = utf8.decode(response.bodyBytes);
return parseItemsFromXMLString(contents);
}
/// Load the Appcast from [file].
Future<List<AppcastItem>?> parseAppcastItemsFromFile(File file) async {
await _getDeviceInfo();
final contents = await file.readAsString();
return parseItemsFromXMLString(contents);
}
List<AppcastItem>? parseItemsFromXMLString(String xmlString) {
items = null;
if (xmlString.isEmpty) {
return null;
}
try {
// Parse the XML
final document = XmlDocument.parse(xmlString);
var items = <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.text;
} else if (name == AppcastConstants.ElementDescription) {
itemDescription = childNode.text;
} 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.text;
} else if (name == AppcastConstants.ElementMinimumSystemVersion) {
minimumSystemVersion = childNode.text;
} else if (name == AppcastConstants.ElementPubDate) {
dateString = childNode.text;
} else if (name == AppcastConstants.ElementReleaseNotesLink) {
releaseNotesLink = childNode.text;
} 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.text;
}
}
});
if (itemVersion == null) {
newVersion = enclosureVersion;
} else {
newVersion = itemVersion;
}
// There must be a version
if (newVersion == null || newVersion.isEmpty) {
return null;
}
final item = AppcastItem(
title: title,
itemDescription: itemDescription,
dateString: dateString,
maximumSystemVersion: maximumSystemVersion,
minimumSystemVersion: minimumSystemVersion,
osString: osString,
releaseNotesURL: releaseNotesLink,
tags: tags,
fileURL: fileURL,
versionString: newVersion,
);
items.add(item);
});
this.items = items;
} catch (e) {
print(e);
}
return items;
}
Future<bool> _getDeviceInfo() async {
final deviceInfo = DeviceInfoPlugin();
if (Platform.isAndroid) {
_androidInfo = await deviceInfo.androidInfo;
osVersionString = _androidInfo.version.baseOS;
} else if (Platform.isIOS) {
_iosInfo = await deviceInfo.iosInfo;
osVersionString = _iosInfo.systemVersion;
}
// If the OS version string is not valid, don't use it.
try {
Version.parse(osVersionString!);
} catch (e) {
osVersionString = null;
}
return true;
}
}
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);
bool hostSupportsItem({String? osVersion, String? currentPlatform}) {
var supported = true;
if (osString != null && osString!.isNotEmpty) {
final platformEnum = 'TargetPlatform.' + osString!;
currentPlatform = currentPlatform == null
? defaultTargetPlatform.toString()
: 'TargetPlatform.' + currentPlatform;
supported = platformEnum.toLowerCase() == currentPlatform.toLowerCase();
}
if (supported && osVersion != null && osVersion.isNotEmpty) {
var osVersionValue;
try {
osVersionValue = Version.parse(osVersion);
} catch (e) {
print('appcast.hostSupportsItem: invalid osVerion: $e');
return false;
}
if (maximumSystemVersion != null) {
final maxVersion = Version.parse(maximumSystemVersion!);
if (osVersionValue > maxVersion) {
supported = false;
}
}
if (supported && minimumSystemVersion != null) {
final minVersion = Version.parse(minimumSystemVersion!);
if (osVersionValue < minVersion) {
supported = false;
}
}
}
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';
}