check_reqs.js 19 KB

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