forked from crosswalk-project/crosswalk-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
192 lines (164 loc) · 6.05 KB
/
utils.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
// simple templating function;
// str: a string like "Hello {name}";
// data: object like {"name": "Elliot"}
function tpl(str, data) {
return str.replace(/\{([^\}]+)\}/g, function (sub, prop) {
return data[prop];
});
}
// async GET for JSON responses only;
// path: path to a file on the domain serving this script;
// cb: function with signature cb(error, responseText)
function asyncJsonGet(path, cb) {
var xhr = new XMLHttpRequest();
xhr.timeout = 15000;
xhr.ontimeout = function () {
cb(new Error('request timed out for path ' + path));
}
xhr.onreadystatechange = function () {
if (this.readyState == 4 && this.status === 200) {
cb(null, JSON.parse(this.responseText));
}
else if (this.status >= 400) {
cb(new Error('request for ' + path + ' failed; status was ' +
this.status));
}
}
xhr.open('GET', path, true);
xhr.send();
}
// compare version with basis, returning true if version >= basis;
// version needs the format P.Q.R.S
function isLaterOrEqualVersion(basis, version) {
var basisParts = basis.split('.');
var versionParts = version.split('.');
var ok = true;
for (var i = 0; i < basisParts.length; i++) {
if (versionParts[i] < basisParts[i]) {
ok = false;
break;
}
else if (versionParts[i] > basisParts[i]) {
break;
}
}
return ok;
}
// get the download URL for a Crosswalk OS/channel/version
// channel: 'stable', 'beta', 'canary'
// arch: 'arm', 'x86'
// OS: 'android', 'tizen-mobile', 'tizen-ivi', 'tizen-emulator', 'cordova',
// 'android-webview'
// version: e.g. '4.32.25.1'
function getXwalkDownloadUrl(OS, arch, channel, version) {
var file_prefix = 'crosswalk-';
// the Cordova and webview downloads are actually under the
// android/ folder, so realOS tracks which OS folder to link to
var realOS = OS;
// tizen emulator downloads are in the same directory as the
// tizen-mobile ones...
if (OS === 'tizen-emulator') {
OS = 'tizen-mobile';
file_prefix += 'emulator-support-';
}
// cordova downloads are under android...
else if (OS === 'cordova') {
realOS = 'android';
file_prefix += 'cordova-';
}
// webview downloads are under android...
else if (OS === 'android-webview') {
realOS = 'android';
file_prefix += 'webview-';
}
var download_url = 'https://download.01.org/crosswalk/releases/crosswalk/'
+ realOS + '/' + channel + '/' + version + '/';
// android: crosswalk-beta >= 5.34.104.1 and crosswalk-canary >= 6.34.106.0
// and crosswalk-stable >= 4.32.76.6
// will be architecture-independent, so once we move to those we need to
// remove |arch| from here and the checks below.
var androidStableArchIndependent = (OS === 'android' &&
channel === 'stable' &&
isLaterOrEqualVersion('4.32.76.6', version));
var androidBetaArchIndependent = (OS === 'android' &&
channel === 'beta' &&
isLaterOrEqualVersion('5.34.104.1', version));
var androidCanaryArchIndependent = (OS === 'android' &&
channel === 'canary' &&
isLaterOrEqualVersion('6.34.106.0', version));
if (OS === 'android' &&
!(androidBetaArchIndependent || androidCanaryArchIndependent || androidStableArchIndependent)) {
download_url += arch + '/';
}
else if (OS === 'cordova' || OS === 'android-webview') {
download_url += arch + '/';
}
download_url += file_prefix + version;
if (OS === 'android' || OS === 'cordova' || OS === 'android-webview') {
if (androidBetaArchIndependent || androidCanaryArchIndependent || androidStableArchIndependent) {
download_url += '.zip';
}
else if (arch === 'x86') {
download_url += '-x86.zip';
}
else if (arch === 'arm') {
download_url += '-arm.zip';
}
}
// as of tizen-mobile 5.32.88.0, suffix changed to 686
else if (OS === 'tizen-ivi' ||
(OS === 'tizen-mobile' && isLaterOrEqualVersion('5.32.88.0', version))) {
download_url += '-0.i686.rpm';
}
// older tizen-mobile
else {
download_url += '-0.i586.rpm';
}
return download_url;
}
// version is a full Crosswalk version number, e.g. "4.23.4.2";
// we just take the digits up to the first '.'
function getXwalkMajorVersion(version) {
var matches = /^(\d+)?\./.exec(version);
if (matches.length < 2) {
majorVersion = '0';
}
else {
majorVersion = matches[1];
}
return majorVersion;
}
// return a URL for the release notes for majorVersion in the wiki;
// NB this assumes that release notes are given a consistent name
// "Crosswalk N release notes"
function getReleaseNotesUrl(majorVersion) {
return '#wiki/Crosswalk-' + majorVersion + '-release-notes';
}
// create a static download link based on data in versions.js;
// link needs these attributes set:
// data-os, data-arch, data-channel
function populateStaticLink(link) {
os = link.getAttribute('data-os');
arch = link.getAttribute('data-arch');
channel = link.getAttribute('data-channel');
// versions.js only knows about 'tizen', not 'tizen-mobile'
// and 'tizen-ivi' and 'tizen-emulator'...
versionsOs = os;
if (/tizen/.test(os)) {
versionsOs = 'tizen';
}
version = versions[channel][versionsOs][arch];
url = getXwalkDownloadUrl(os, arch, channel, version);
link.setAttribute('href', url);
link.innerHTML = version;
}
// populate the static download links (i.e. <a> elements
// with data-role="static-download-link") using data in versions.js
function populateStaticLinks() {
var sel = 'a[data-role="static-download-link"]';
var staticLinks = document.querySelectorAll(sel);
var link, os, arch, channel, versionsOs, url, version;
for (var i = 0; i < staticLinks.length; i++) {
populateStaticLink(staticLinks.item(i));
}
}