This repository has been archived by the owner on Jan 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathvalidateBuildEnvironment.js
349 lines (334 loc) · 11.6 KB
/
validateBuildEnvironment.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
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
338
339
340
341
342
343
344
345
346
347
348
349
'use strict';
/* jshint esnext: true */
const exec = require('child-process-promise').exec;
const path = require('path');
const fs = require('fs-extra-promise');
const thaliConfig = require('../package.json');
const os = require('os');
const http = require('http');
const url = require('url');
const assert = require('assert');
const versions =
{
xcode: '8.3.3',
xcodeCommandLineTools: ' ',
macOS: '10.12.6',
node: '6.11.1',
npm: '3.10.10',
brew: '1.2.',
ruby: '2.4.1p111',
wget: '1.18',
jxcore: '0.3.1.14',
androidHome: ' ',
androidBuildTools: thaliConfig.thaliInstall.androidConfig.buildToolsVersion,
androidPlatform: thaliConfig.thaliInstall.androidConfig.compileSdkVersion,
// We don't have an easy way to identify the version of the support libraries
// we have but if they were installed recently enough then they will have
// what we need.
androidSupportLibraries: '47.0.0',
python: '2.7.10',
cordova: '6.5.0',
java: '1.8.0_141',
git: '2.13.1',
swiftlint: '0.20.1',
carthage: '0.24.0',
sinopiaNode: ' ',
sinopiaJxCore: ' '
};
module.exports.versions = versions;
function sinopiaVersionCheck(sinopiaUrl) {
return new Promise((resolve, reject) => {
var parsedUrl = url.parse(sinopiaUrl);
if (parsedUrl.protocol !== 'http:') {
return Promise.reject();
}
var testThaliUrl =
sinopiaUrl + (sinopiaUrl.endsWith('/') ? '' : '/') + 'thali';
http.get(testThaliUrl, (res) => {
let result = '';
res.on('data', (chunk) => result += chunk);
res.on('end', () => {
try {
var thaliResponse = JSON.parse(result);
thaliResponse.name === 'thali' ? resolve() : reject();
} catch (e) {
reject(e);
}
});
res.on('error', (err) => reject(err));
});
});
}
function boolToPromise(result) {
return result ? Promise.resolve() : Promise.reject();
}
const commandsAndResults =
{
xcode: {
platform: ['darwin'],
versionCheck: 'xcodebuild -version',
versionValidate:
(result, version) =>
boolToPromise(result.startsWith('Xcode '+ version + '\n'))
},
xcodeCommandLineTools: {
platform: ['darwin'],
// I couldn't find any reliable way to validate which versions of the
// tools are installed. The best I could do was find out which directory
// they are supposed to be in. I tried http://stackoverflow.com/questions/15371925/how-to-check-if-command-line-tools-is-installed
// and xcode-select -p returns a directory inside of XCode and none of
// the pkgutil commands worked properly on my machine.
// Also note that in some circumstances this directory can also contain
// a subdirectory called SDKs. It's not clear if it's always there so we
// don't check for it.
versionCheck: () => fs.readdirAsync('/Library/Developer/CommandLineTools'),
versionValidate:
(result) => boolToPromise(result && result.length >= 2 &&
result.indexOf('Library') !== -1 &&
result.indexOf('usr') !== -1)
},
macOS: {
platform: ['darwin'],
versionCheck: 'sw_vers -productVersion',
versionValidate:
(result, version) => boolToPromise(version === result.trim())
},
node: {
versionCheck: 'node -v',
versionValidate:
(result, version) => boolToPromise('v' + version === result.trim())
},
npm: {
versionCheck: 'npm -v',
versionValidate:
(result, version) => boolToPromise(version === result.trim())
},
brew: {
platform: ['darwin'],
versionCheck: 'brew -v',
versionValidate:
(result, version) =>
boolToPromise(result.startsWith('Homebrew ' + version))
},
ruby: {
platform: ['darwin'],
versionCheck: 'ruby -v',
versionValidate:
(result, version) =>
boolToPromise(result.startsWith('ruby ' + version + ' '))
},
wget: {
versionCheck: 'wget -V',
versionValidate:
(result, version) =>
boolToPromise(result.startsWith('GNU Wget ' + version + ' '))
},
jxcore: {
versionCheck: 'jx -jxv',
versionValidate:
(result, version) => boolToPromise('v' + version === result.trim())
},
androidHome: {
versionCheck: () => process.env.ANDROID_HOME,
versionValidate:
(result) => {
if (result) {
return fs.readdirAsync(result);
} else {
return Promise.reject();
}
}
},
androidBuildTools: {
versionCheck: () => fs.readdirAsync(path.join(process.env.ANDROID_HOME,
'build-tools')),
versionValidate:
(result, version) => boolToPromise(result.indexOf(version) !== -1)
},
androidPlatform: {
versionCheck: () => fs.readdirAsync(path.join(process.env.ANDROID_HOME,
'platforms')),
versionValidate:
(result, version) => boolToPromise(result.indexOf(version) !== -1)
},
androidSupportLibraries: {
versionCheck: () => {
const sourcePropertiesLocation =
path.join(process.env.ANDROID_HOME,
'extras/android/m2repository/source.properties');
return fs.readFileAsync(sourcePropertiesLocation, 'utf8')
.then((sourcePropertiesFileContents) => {
const regEx =
sourcePropertiesFileContents.match(/^Pkg\.Revision=(.*)$/m);
if (!regEx[1]) {
return Promise.reject();
}
return Promise.resolve(regEx[1]);
});
},
versionValidate:
(result, version) => boolToPromise(version === result.trim())
},
python: {
versionCheck: 'python -V',
checkStdErr: true, // http://bugs.python.org/issue28160 - fixed in 3.4
versionValidate:
(result, version) => boolToPromise('Python ' + version === result.trim())
},
cordova: {
versionCheck: 'cordova -v',
versionValidate:
(result, version) => boolToPromise(version === result.trim())
},
java: {
versionCheck: 'java -version',
checkStdErr: true, // http://bugs.java.com/bugdatabase/view_bug.do?bug_id=JDK-8166116
versionValidate:
(result, version) => boolToPromise(result.startsWith('java version "' +
version + '"\n'))
},
git: {
versionCheck: 'git --version',
versionValidate:
(result, version) => boolToPromise(
'git version ' + version === result.trim())
},
swiftlint: {
platform: ['darwin'],
versionCheck: 'swiftlint version',
versionValidate:
(result, version) => boolToPromise(version === result.trim())
},
carthage: {
// We can run into the situation when carthage proposes new updates
// In this case output is similar to:
// *** Please update to the latest Carthage version: 0.20.1. You currently are on 0.20.0
// 0.20.0
// This is why we're reading only the last line from output
platform: ['darwin'],
versionCheck: 'carthage version | tail -n 1',
versionValidate:
(result, version) => boolToPromise(version === result.trim())
},
sinopiaNode: {
versionCheck: 'npm get registry',
versionValidate:
(result) => sinopiaVersionCheck(result.trim())
},
sinopiaJxCore: {
// The first time jx npm is run it can do an install, to simplify things
// we just call it twice so the second call will be normal and give us the
// url
versionCheck: 'jx npm get registry > /dev/null && jx npm get registry',
versionValidate:
(result) => sinopiaVersionCheck(result.trim())
}
};
function execAndCheck(command, checkStdErr, version, validator) {
return exec(command)
.then((result) => {
const output = checkStdErr ? result.stderr : result.stdout;
return validator(output, version);
})
.then(() => Promise.resolve(true))
.catch(() => Promise.reject(new Error('Command: ' + command + ' failed')));
}
function promiseTry(fn) {
try {
return Promise.resolve(fn());
} catch (error) {
return Promise.reject(error);
}
}
/**
* Checks if the named object is installed with the named version, if any. If
* versionNumber isn't given then we default to checking the versions global
* object.
* @param {string} objectName Name of the object to validate
* @param {string} [versionNumber] An optional string specifying the desired
* version. If omitted we will check the versions structure.
* @returns {Promise<Error|boolean>} If the desired object is found at the
* desired version then a resolve will be returned set to true. Otherwise an
* error will be returned specifying what went wrong.
*/
function checkVersion(objectName, versionNumber) {
const desiredVersion = versionNumber ? versionNumber : versions[objectName];
const commandAndResult = commandsAndResults[objectName];
if (!commandAndResult) {
return Promise.reject(
new Error('Unrecognized objectName in commandsAndResults'));
}
if (!desiredVersion) {
return Promise.reject(new Error('Unrecognized objectName in versions'));
}
if (commandAndResult.platform &&
!commandAndResult.platform.includes(os.platform()))
{
return Promise.reject(new Error('Requested object is not supported on ' +
'this platform'));
}
if (typeof commandAndResult.versionCheck === 'function') {
return promiseTry(commandAndResult.versionCheck)
.catch(() =>
Promise.reject(new Error('Version Check failed on ' + objectName)))
.then((versionCheckResult) =>
commandAndResult.versionValidate(versionCheckResult, desiredVersion))
.then(() => Promise.resolve(true))
.catch(() => Promise.reject(
new Error('Version not installed of ' + objectName)));
}
return execAndCheck(commandAndResult.versionCheck,
commandAndResult.checkStdErr,
desiredVersion,
commandAndResult.versionValidate);
}
module.exports.checkVersion = checkVersion;
function processCommandsAndResults(commandsAndResults) {
let passed = true;
let errorMessage = '';
let runningPromise = Object.getOwnPropertyNames(commandsAndResults)
.reduce((runningPromise, name) => {
if (commandsAndResults[name].platform &&
!commandsAndResults[name].platform.includes(os.platform())) {
return runningPromise;
}
return runningPromise.then(() =>
checkVersion(name)
.catch((err) => {
passed = false;
errorMessage += '\n' + 'Object Name: ' + name + ' : ' + err;
return Promise.resolve();
}));
}, Promise.resolve());
return runningPromise
.then(() => passed ? Promise.resolve() : Promise.reject(errorMessage));
}
// Detects if we were called from the command line
if (require.main === module) {
if (os.platform() !== 'darwin') {
console.log('WARNING: WE ONLY SUPPORT OS/X AS A BUILD PLATFORM, USING ANY' +
'OTHER PLATFORM IS NOT OFFICIALLY SUPPORTED. WE STILL CHECK A FEW ' +
'THINGS BUT YOU ARE REALLY ON YOUR OWN');
}
assert.deepStrictEqual(Object.getOwnPropertyNames(versions),
Object.getOwnPropertyNames(commandsAndResults),
'Versions and commandsAndResults keys must be equal');
processCommandsAndResults(commandsAndResults)
.then(() => {
// Good to clean this up in case we have changed the version of jxcore
const home = process.env.HOME;
const jx = path.join(home, '.jx');
const jxc = path.join(home, '.jxc');
const nodeGyp = path.join(home, '.node-gyp');
return Promise.all([fs.removeAsync(jx), fs.removeAsync(jxc),
fs.removeAsync(nodeGyp)]);
})
.then(() => {
console.log('Environment validated');
process.exit(0);
})
.catch((err) => {
console.log('Environment not valid: ' + err);
process.exit(-1);
});
}