check_reqs.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. #!/usr/bin/env node
  2. /*
  3. Licensed to the Apache Software Foundation (ASF) under one
  4. or more contributor license agreements. See the NOTICE file
  5. distributed with this work for additional information
  6. regarding copyright ownership. The ASF licenses this file
  7. to you under the Apache License, Version 2.0 (the
  8. "License"); you may not use this file except in compliance
  9. with the License. You may obtain a copy of the License at
  10. http://www.apache.org/licenses/LICENSE-2.0
  11. Unless required by applicable law or agreed to in writing,
  12. software distributed under the License is distributed on an
  13. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. KIND, either express or implied. See the License for the
  15. specific language governing permissions and limitations
  16. under the License.
  17. */
  18. var shelljs = require('shelljs');
  19. var child_process = require('child_process');
  20. var Q = require('q');
  21. var path = require('path');
  22. var fs = require('fs');
  23. var os = require('os');
  24. var REPO_ROOT = path.join(__dirname, '..', '..', '..', '..');
  25. var PROJECT_ROOT = path.join(__dirname, '..', '..');
  26. var CordovaError = require('cordova-common').CordovaError;
  27. var superspawn = require('cordova-common').superspawn;
  28. var android_sdk = require('./android_sdk');
  29. function forgivingWhichSync (cmd) {
  30. try {
  31. return fs.realpathSync(shelljs.which(cmd));
  32. } catch (e) {
  33. return '';
  34. }
  35. }
  36. module.exports.isWindows = function () {
  37. return (os.platform() === 'win32');
  38. };
  39. module.exports.isDarwin = function () {
  40. return (os.platform() === 'darwin');
  41. };
  42. // Get valid target from framework/project.properties if run from this repo
  43. // Otherwise get target from project.properties file within a generated cordova-android project
  44. module.exports.get_target = function () {
  45. function extractFromFile (filePath) {
  46. var target = shelljs.grep(/\btarget=/, filePath);
  47. if (!target) {
  48. throw new Error('Could not find android target within: ' + filePath);
  49. }
  50. return target.split('=')[1].trim();
  51. }
  52. var repo_file = path.join(REPO_ROOT, 'framework', 'project.properties');
  53. if (fs.existsSync(repo_file)) {
  54. return extractFromFile(repo_file);
  55. }
  56. var project_file = path.join(PROJECT_ROOT, 'project.properties');
  57. if (fs.existsSync(project_file)) {
  58. // if no target found, we're probably in a project and project.properties is in PROJECT_ROOT.
  59. return extractFromFile(project_file);
  60. }
  61. throw new Error('Could not find android target in either ' + repo_file + ' nor ' + project_file);
  62. };
  63. // Returns a promise. Called only by build and clean commands.
  64. module.exports.check_ant = function () {
  65. return superspawn.spawn('ant', ['-version']).then(function (output) {
  66. // Parse Ant version from command output
  67. return /version ((?:\d+\.)+(?:\d+))/i.exec(output)[1];
  68. }).catch(function (err) {
  69. if (err) {
  70. throw new CordovaError('Failed to run `ant -version`. Make sure you have `ant` on your $PATH.');
  71. }
  72. });
  73. };
  74. module.exports.get_gradle_wrapper = function () {
  75. var androidStudioPath;
  76. var i = 0;
  77. var foundStudio = false;
  78. var program_dir;
  79. // OK, This hack only works on Windows, not on Mac OS or Linux. We will be deleting this eventually!
  80. if (module.exports.isWindows()) {
  81. var result = child_process.spawnSync(path.join(__dirname, 'getASPath.bat'));
  82. // console.log('result.stdout =' + result.stdout.toString());
  83. // console.log('result.stderr =' + result.stderr.toString());
  84. if (result.stderr.toString().length > 0) {
  85. var androidPath = path.join(process.env['ProgramFiles'], 'Android') + '/';
  86. if (fs.existsSync(androidPath)) {
  87. program_dir = fs.readdirSync(androidPath);
  88. while (i < program_dir.length && !foundStudio) {
  89. if (program_dir[i].startsWith('Android Studio')) {
  90. foundStudio = true;
  91. androidStudioPath = path.join(process.env['ProgramFiles'], 'Android', program_dir[i], 'gradle');
  92. } else { ++i; }
  93. }
  94. }
  95. } else {
  96. // console.log('got android studio path from registry');
  97. // remove the (os independent) new line char at the end of stdout
  98. // add gradle to match the above.
  99. androidStudioPath = path.join(result.stdout.toString().split('\r\n')[0], 'gradle');
  100. }
  101. }
  102. if (androidStudioPath !== null && fs.existsSync(androidStudioPath)) {
  103. var dirs = fs.readdirSync(androidStudioPath);
  104. if (dirs[0].split('-')[0] === 'gradle') {
  105. return path.join(androidStudioPath, dirs[0], 'bin', 'gradle');
  106. }
  107. } else {
  108. // OK, let's try to check for Gradle!
  109. return forgivingWhichSync('gradle');
  110. }
  111. };
  112. // Returns a promise. Called only by build and clean commands.
  113. module.exports.check_gradle = function () {
  114. var sdkDir = process.env['ANDROID_HOME'];
  115. var d = Q.defer();
  116. if (!sdkDir) {
  117. return Q.reject(new CordovaError('Could not find gradle wrapper within Android SDK. Could not find Android SDK directory.\n' +
  118. 'Might need to install Android SDK or set up \'ANDROID_HOME\' env variable.'));
  119. }
  120. var gradlePath = module.exports.get_gradle_wrapper();
  121. if (gradlePath.length !== 0) { d.resolve(gradlePath); } else {
  122. d.reject(new CordovaError('Could not find an installed version of Gradle either in Android Studio,\n' +
  123. 'or on your system to install the gradle wrapper. Please include gradle \n' +
  124. 'in your path, or install Android Studio'));
  125. }
  126. return d.promise;
  127. };
  128. // Returns a promise.
  129. module.exports.check_java = function () {
  130. var javacPath = forgivingWhichSync('javac');
  131. var hasJavaHome = !!process.env['JAVA_HOME'];
  132. return Q().then(function () {
  133. if (hasJavaHome) {
  134. // Windows java installer doesn't add javac to PATH, nor set JAVA_HOME (ugh).
  135. if (!javacPath) {
  136. process.env['PATH'] += path.delimiter + path.join(process.env['JAVA_HOME'], 'bin');
  137. }
  138. } else {
  139. if (javacPath) {
  140. // OS X has a command for finding JAVA_HOME.
  141. var find_java = '/usr/libexec/java_home';
  142. var default_java_error_msg = 'Failed to find \'JAVA_HOME\' environment variable. Try setting it manually.';
  143. if (fs.existsSync(find_java)) {
  144. return superspawn.spawn(find_java).then(function (stdout) {
  145. process.env['JAVA_HOME'] = stdout.trim();
  146. }).catch(function (err) {
  147. if (err) {
  148. throw new CordovaError(default_java_error_msg);
  149. }
  150. });
  151. } else {
  152. // See if we can derive it from javac's location.
  153. // fs.realpathSync is require on Ubuntu, which symplinks from /usr/bin -> JDK
  154. var maybeJavaHome = path.dirname(path.dirname(javacPath));
  155. if (fs.existsSync(path.join(maybeJavaHome, 'lib', 'tools.jar'))) {
  156. process.env['JAVA_HOME'] = maybeJavaHome;
  157. } else {
  158. throw new CordovaError(default_java_error_msg);
  159. }
  160. }
  161. } else if (module.exports.isWindows()) {
  162. // Try to auto-detect java in the default install paths.
  163. var oldSilent = shelljs.config.silent;
  164. shelljs.config.silent = true;
  165. var firstJdkDir =
  166. shelljs.ls(process.env['ProgramFiles'] + '\\java\\jdk*')[0] ||
  167. shelljs.ls('C:\\Program Files\\java\\jdk*')[0] ||
  168. shelljs.ls('C:\\Program Files (x86)\\java\\jdk*')[0];
  169. shelljs.config.silent = oldSilent;
  170. if (firstJdkDir) {
  171. // shelljs always uses / in paths.
  172. firstJdkDir = firstJdkDir.replace(/\//g, path.sep);
  173. if (!javacPath) {
  174. process.env['PATH'] += path.delimiter + path.join(firstJdkDir, 'bin');
  175. }
  176. process.env['JAVA_HOME'] = firstJdkDir;
  177. }
  178. }
  179. }
  180. }).then(function () {
  181. return Q.denodeify(child_process.exec)('javac -version')
  182. .then(outputs => {
  183. // outputs contains two entries: stdout and stderr
  184. // Java <= 8 writes version info to stderr, Java >= 9 to stdout
  185. const output = outputs.join('').trim();
  186. const match = /javac\s+([\d.]+)/i.exec(output);
  187. return match && match[1];
  188. }, () => {
  189. var msg =
  190. 'Failed to run "javac -version", make sure that you have a JDK version 8 installed.\n' +
  191. 'You can get it from the following location:\n' +
  192. 'https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html';
  193. if (process.env['JAVA_HOME']) {
  194. msg += '\n\n';
  195. msg += 'Your JAVA_HOME is invalid: ' + process.env['JAVA_HOME'];
  196. }
  197. throw new CordovaError(msg);
  198. });
  199. });
  200. };
  201. // Returns a promise.
  202. module.exports.check_android = function () {
  203. return Q().then(function () {
  204. var androidCmdPath = forgivingWhichSync('android');
  205. var adbInPath = forgivingWhichSync('adb');
  206. var avdmanagerInPath = forgivingWhichSync('avdmanager');
  207. var hasAndroidHome = !!process.env['ANDROID_HOME'] && fs.existsSync(process.env['ANDROID_HOME']);
  208. function maybeSetAndroidHome (value) {
  209. if (!hasAndroidHome && fs.existsSync(value)) {
  210. hasAndroidHome = true;
  211. process.env['ANDROID_HOME'] = value;
  212. }
  213. }
  214. // First ensure ANDROID_HOME is set
  215. // If we have no hints (nothing in PATH), try a few default locations
  216. if (!hasAndroidHome && !androidCmdPath && !adbInPath && !avdmanagerInPath) {
  217. if (process.env['ANDROID_SDK_ROOT']) {
  218. // Quick fix to set ANDROID_HOME according to ANDROID_SDK_ROOT
  219. // if ANDROID_HOME is **not** defined and
  220. // ANDROID_SDK_ROOT **is** defined
  221. // according to environment variables as documented in:
  222. // https://developer.android.com/studio/command-line/variables
  223. maybeSetAndroidHome(path.join(process.env['ANDROID_SDK_ROOT']));
  224. }
  225. if (module.exports.isWindows()) {
  226. // Android Studio 1.0 installer
  227. maybeSetAndroidHome(path.join(process.env['LOCALAPPDATA'], 'Android', 'sdk'));
  228. maybeSetAndroidHome(path.join(process.env['ProgramFiles'], 'Android', 'sdk'));
  229. // Android Studio pre-1.0 installer
  230. maybeSetAndroidHome(path.join(process.env['LOCALAPPDATA'], 'Android', 'android-studio', 'sdk'));
  231. maybeSetAndroidHome(path.join(process.env['ProgramFiles'], 'Android', 'android-studio', 'sdk'));
  232. // Stand-alone installer
  233. maybeSetAndroidHome(path.join(process.env['LOCALAPPDATA'], 'Android', 'android-sdk'));
  234. maybeSetAndroidHome(path.join(process.env['ProgramFiles'], 'Android', 'android-sdk'));
  235. } else if (module.exports.isDarwin()) {
  236. // Android Studio 1.0 installer
  237. maybeSetAndroidHome(path.join(process.env['HOME'], 'Library', 'Android', 'sdk'));
  238. // Android Studio pre-1.0 installer
  239. maybeSetAndroidHome('/Applications/Android Studio.app/sdk');
  240. // Stand-alone zip file that user might think to put under /Applications
  241. maybeSetAndroidHome('/Applications/android-sdk-macosx');
  242. maybeSetAndroidHome('/Applications/android-sdk');
  243. }
  244. if (process.env['HOME']) {
  245. // Stand-alone zip file that user might think to put under their home directory
  246. maybeSetAndroidHome(path.join(process.env['HOME'], 'android-sdk-macosx'));
  247. maybeSetAndroidHome(path.join(process.env['HOME'], 'android-sdk'));
  248. }
  249. }
  250. if (!hasAndroidHome) {
  251. // If we dont have ANDROID_HOME, but we do have some tools on the PATH, try to infer from the tooling PATH.
  252. var parentDir, grandParentDir;
  253. if (androidCmdPath) {
  254. parentDir = path.dirname(androidCmdPath);
  255. grandParentDir = path.dirname(parentDir);
  256. if (path.basename(parentDir) === 'tools' || fs.existsSync(path.join(grandParentDir, 'tools', 'android'))) {
  257. maybeSetAndroidHome(grandParentDir);
  258. } else {
  259. throw new CordovaError('Failed to find \'ANDROID_HOME\' environment variable. Try setting it manually.\n' +
  260. 'Detected \'android\' command at ' + parentDir + ' but no \'tools\' directory found near.\n' +
  261. 'Try reinstall Android SDK or update your PATH to include valid path to SDK' + path.sep + 'tools directory.');
  262. }
  263. }
  264. if (adbInPath) {
  265. parentDir = path.dirname(adbInPath);
  266. grandParentDir = path.dirname(parentDir);
  267. if (path.basename(parentDir) === 'platform-tools') {
  268. maybeSetAndroidHome(grandParentDir);
  269. } else {
  270. throw new CordovaError('Failed to find \'ANDROID_HOME\' environment variable. Try setting it manually.\n' +
  271. 'Detected \'adb\' command at ' + parentDir + ' but no \'platform-tools\' directory found near.\n' +
  272. 'Try reinstall Android SDK or update your PATH to include valid path to SDK' + path.sep + 'platform-tools directory.');
  273. }
  274. }
  275. if (avdmanagerInPath) {
  276. parentDir = path.dirname(avdmanagerInPath);
  277. grandParentDir = path.dirname(parentDir);
  278. if (path.basename(parentDir) === 'bin' && path.basename(grandParentDir) === 'tools') {
  279. maybeSetAndroidHome(path.dirname(grandParentDir));
  280. } else {
  281. throw new CordovaError('Failed to find \'ANDROID_HOME\' environment variable. Try setting it manually.\n' +
  282. 'Detected \'avdmanager\' command at ' + parentDir + ' but no \'tools' + path.sep + 'bin\' directory found near.\n' +
  283. 'Try reinstall Android SDK or update your PATH to include valid path to SDK' + path.sep + 'tools' + path.sep + 'bin directory.');
  284. }
  285. }
  286. }
  287. if (!process.env['ANDROID_HOME']) {
  288. throw new CordovaError('Failed to find \'ANDROID_HOME\' environment variable. Try setting it manually.\n' +
  289. 'Failed to find \'android\' command in your \'PATH\'. Try update your \'PATH\' to include path to valid SDK directory.');
  290. }
  291. if (!fs.existsSync(process.env['ANDROID_HOME'])) {
  292. throw new CordovaError('\'ANDROID_HOME\' environment variable is set to non-existent path: ' + process.env['ANDROID_HOME'] +
  293. '\nTry update it manually to point to valid SDK directory.');
  294. }
  295. // Next let's make sure relevant parts of the SDK tooling is in our PATH
  296. if (hasAndroidHome && !androidCmdPath) {
  297. process.env['PATH'] += path.delimiter + path.join(process.env['ANDROID_HOME'], 'tools');
  298. }
  299. if (hasAndroidHome && !adbInPath) {
  300. process.env['PATH'] += path.delimiter + path.join(process.env['ANDROID_HOME'], 'platform-tools');
  301. }
  302. if (hasAndroidHome && !avdmanagerInPath) {
  303. process.env['PATH'] += path.delimiter + path.join(process.env['ANDROID_HOME'], 'tools', 'bin');
  304. }
  305. return hasAndroidHome;
  306. });
  307. };
  308. // TODO: is this actually needed?
  309. module.exports.getAbsoluteAndroidCmd = function () {
  310. var cmd = forgivingWhichSync('android');
  311. if (cmd.length === 0) {
  312. cmd = forgivingWhichSync('sdkmanager');
  313. }
  314. if (module.exports.isWindows()) {
  315. return '"' + cmd + '"';
  316. }
  317. return cmd.replace(/(\s)/g, '\\$1');
  318. };
  319. module.exports.check_android_target = function (originalError) {
  320. // valid_target can look like:
  321. // android-19
  322. // android-L
  323. // Google Inc.:Google APIs:20
  324. // Google Inc.:Glass Development Kit Preview:20
  325. var desired_api_level = module.exports.get_target();
  326. return android_sdk.list_targets().then(function (targets) {
  327. if (targets.indexOf(desired_api_level) >= 0) {
  328. return targets;
  329. }
  330. var androidCmd = module.exports.getAbsoluteAndroidCmd();
  331. var msg = 'Please install Android target / API level: "' + desired_api_level + '".\n\n' +
  332. 'Hint: Open the SDK manager by running: ' + androidCmd + '\n' +
  333. 'You will require:\n' +
  334. '1. "SDK Platform" for API level ' + desired_api_level + '\n' +
  335. '2. "Android SDK Platform-tools (latest)\n' +
  336. '3. "Android SDK Build-tools" (latest)';
  337. if (originalError) {
  338. msg = originalError + '\n' + msg;
  339. }
  340. throw new CordovaError(msg);
  341. });
  342. };
  343. // Returns a promise.
  344. module.exports.run = function () {
  345. return Q.all([this.check_java(), this.check_android()]).then(function (values) {
  346. console.log('Checking Java JDK and Android SDK versions');
  347. console.log('ANDROID_SDK_ROOT=' + process.env['ANDROID_SDK_ROOT'] + ' (recommended setting)');
  348. console.log('ANDROID_HOME=' + process.env['ANDROID_HOME'] + ' (DEPRECATED)');
  349. if (!String(values[0]).startsWith('1.8.')) {
  350. throw new CordovaError(
  351. 'Requirements check failed for JDK 8 (\'1.8.*\')! Detected version: ' + values[0] + '\n' +
  352. 'Check your ANDROID_SDK_ROOT / JAVA_HOME / PATH environment variables.'
  353. );
  354. }
  355. if (!values[1]) {
  356. throw new CordovaError('Requirements check failed for Android SDK! Android SDK was not detected.');
  357. }
  358. });
  359. };
  360. /**
  361. * Object thar represents one of requirements for current platform.
  362. * @param {String} id The unique identifier for this requirements.
  363. * @param {String} name The name of requirements. Human-readable field.
  364. * @param {String} version The version of requirement installed. In some cases could be an array of strings
  365. * (for example, check_android_target returns an array of android targets installed)
  366. * @param {Boolean} installed Indicates whether the requirement is installed or not
  367. */
  368. var Requirement = function (id, name, version, installed) {
  369. this.id = id;
  370. this.name = name;
  371. this.installed = installed || false;
  372. this.metadata = {
  373. version: version
  374. };
  375. };
  376. /**
  377. * Methods that runs all checks one by one and returns a result of checks
  378. * as an array of Requirement objects. This method intended to be used by cordova-lib check_reqs method
  379. *
  380. * @return Promise<Requirement[]> Array of requirements. Due to implementation, promise is always fulfilled.
  381. */
  382. module.exports.check_all = function () {
  383. var requirements = [
  384. new Requirement('java', 'Java JDK'),
  385. new Requirement('androidSdk', 'Android SDK'),
  386. new Requirement('androidTarget', 'Android target'),
  387. new Requirement('gradle', 'Gradle')
  388. ];
  389. var checkFns = [
  390. this.check_java,
  391. this.check_android,
  392. this.check_android_target,
  393. this.check_gradle
  394. ];
  395. // Then execute requirement checks one-by-one
  396. return checkFns.reduce(function (promise, checkFn, idx) {
  397. // Update each requirement with results
  398. var requirement = requirements[idx];
  399. return promise.then(checkFn).then(function (version) {
  400. requirement.installed = true;
  401. requirement.metadata.version = version;
  402. }, function (err) {
  403. requirement.metadata.reason = err instanceof Error ? err.message : err;
  404. });
  405. }, Q()).then(function () {
  406. // When chain is completed, return requirements array to upstream API
  407. return requirements;
  408. });
  409. };