Repositorio del curso CCOM4030 el semestre B91 del proyecto Paz para la Mujer

check_reqs.js 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF licenses this file
  6. to you under the Apache License, Version 2.0 (the
  7. "License"); you may not use this file except in compliance
  8. with the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing,
  11. software distributed under the License is distributed on an
  12. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. KIND, either express or implied. See the License for the
  14. specific language governing permissions and limitations
  15. under the License.
  16. */
  17. 'use strict';
  18. const Q = require('q');
  19. const shell = require('shelljs');
  20. const util = require('util');
  21. const versions = require('./versions');
  22. const SUPPORTED_OS_PLATFORMS = [ 'darwin' ];
  23. const XCODEBUILD_MIN_VERSION = '9.0.0';
  24. const XCODEBUILD_NOT_FOUND_MESSAGE =
  25. 'Please install version ' + XCODEBUILD_MIN_VERSION + ' or greater from App Store';
  26. const IOS_DEPLOY_MIN_VERSION = '1.9.2';
  27. const IOS_DEPLOY_NOT_FOUND_MESSAGE =
  28. 'Please download, build and install version ' + IOS_DEPLOY_MIN_VERSION + ' or greater' +
  29. ' from https://github.com/ios-control/ios-deploy into your path, or do \'npm install -g ios-deploy\'';
  30. const COCOAPODS_MIN_VERSION = '1.0.1';
  31. const COCOAPODS_NOT_FOUND_MESSAGE =
  32. 'Please install version ' + COCOAPODS_MIN_VERSION + ' or greater from https://cocoapods.org/';
  33. const COCOAPODS_NOT_SYNCED_MESSAGE =
  34. 'The CocoaPods repo has not been synced yet, this will take a long time (approximately 500MB as of Sept 2016). Please run `pod setup` first to sync the repo.';
  35. const COCOAPODS_SYNCED_MIN_SIZE = 475; // in megabytes
  36. const COCOAPODS_SYNC_ERROR_MESSAGE =
  37. 'The CocoaPods repo has been created, but there appears to be a sync error. The repo size should be at least ' + COCOAPODS_SYNCED_MIN_SIZE + '. Please run `pod setup --verbose` to sync the repo.';
  38. const COCOAPODS_REPO_NOT_FOUND_MESSAGE = 'The CocoaPods repo at ~/.cocoapods was not found.';
  39. /**
  40. * Checks if xcode util is available
  41. * @return {Promise} Returns a promise either resolved with xcode version or rejected
  42. */
  43. module.exports.run = module.exports.check_xcodebuild = function () {
  44. return checkTool('xcodebuild', XCODEBUILD_MIN_VERSION, XCODEBUILD_NOT_FOUND_MESSAGE);
  45. };
  46. /**
  47. * Checks if ios-deploy util is available
  48. * @return {Promise} Returns a promise either resolved with ios-deploy version or rejected
  49. */
  50. module.exports.check_ios_deploy = function () {
  51. return checkTool('ios-deploy', IOS_DEPLOY_MIN_VERSION, IOS_DEPLOY_NOT_FOUND_MESSAGE);
  52. };
  53. module.exports.check_os = function () {
  54. // Build iOS apps available for OSX platform only, so we reject on others platforms
  55. return os_platform_is_supported() ?
  56. Q.resolve(process.platform) :
  57. Q.reject('Cordova tooling for iOS requires Apple macOS');
  58. };
  59. function os_platform_is_supported () {
  60. return (SUPPORTED_OS_PLATFORMS.indexOf(process.platform) !== -1);
  61. }
  62. function check_cocoapod_tool (toolChecker) {
  63. toolChecker = toolChecker || checkTool;
  64. if (os_platform_is_supported()) { // CB-12856
  65. return toolChecker('pod', COCOAPODS_MIN_VERSION, COCOAPODS_NOT_FOUND_MESSAGE, 'CocoaPods');
  66. } else {
  67. return Q.resolve({
  68. 'ignore': true,
  69. 'ignoreMessage': `CocoaPods check and installation ignored on ${process.platform}`
  70. });
  71. }
  72. }
  73. /**
  74. * Checks if cocoapods repo size is what is expected
  75. * @return {Promise} Returns a promise either resolved or rejected
  76. */
  77. module.exports.check_cocoapods_repo_size = function () {
  78. return check_cocoapod_tool()
  79. .then(function (toolOptions) {
  80. // check size of ~/.cocoapods repo
  81. let commandString = util.format('du -sh %s/.cocoapods', process.env.HOME);
  82. let command = shell.exec(commandString, { silent: true });
  83. // command.output is e.g "750M path/to/.cocoapods", we just scan the number
  84. let size = toolOptions.ignore ? 0 : parseFloat(command.output);
  85. if (toolOptions.ignore || command.code === 0) { // success, parse output
  86. return Q.resolve(size, toolOptions);
  87. } else { // error, perhaps not found
  88. return Q.reject(util.format('%s (%s)', COCOAPODS_REPO_NOT_FOUND_MESSAGE, command.output));
  89. }
  90. })
  91. .then(function (repoSize, toolOptions) {
  92. if (toolOptions.ignore || COCOAPODS_SYNCED_MIN_SIZE <= repoSize) { // success, expected size
  93. return Q.resolve(toolOptions);
  94. } else {
  95. return Q.reject(COCOAPODS_SYNC_ERROR_MESSAGE);
  96. }
  97. });
  98. };
  99. /**
  100. * Checks if cocoapods is available, and whether the repo is synced (because it takes a long time to download)
  101. * @return {Promise} Returns a promise either resolved or rejected
  102. */
  103. module.exports.check_cocoapods = function (toolChecker) {
  104. return check_cocoapod_tool(toolChecker)
  105. // check whether the cocoapods repo has been synced through `pod repo` command
  106. // a value of '0 repos' means it hasn't been synced
  107. .then(function (toolOptions) {
  108. let code = shell.exec('pod repo | grep -e "^0 repos"', { silent: true }).code;
  109. let repoIsSynced = (code !== 0);
  110. if (toolOptions.ignore || repoIsSynced) {
  111. // return check_cocoapods_repo_size();
  112. // we could check the repo size above, but it takes too long.
  113. return Q.resolve(toolOptions);
  114. } else {
  115. return Q.reject(COCOAPODS_NOT_SYNCED_MESSAGE);
  116. }
  117. });
  118. };
  119. /**
  120. * Checks if specific tool is available.
  121. * @param {String} tool Tool name to check. Known tools are 'xcodebuild' and 'ios-deploy'
  122. * @param {Number} minVersion Min allowed tool version.
  123. * @param {String} message Message that will be used to reject promise.
  124. * @param {String} toolFriendlyName Friendly name of the tool, to report to the user. Optional.
  125. * @return {Promise} Returns a promise either resolved with tool version or rejected
  126. */
  127. function checkTool (tool, minVersion, message, toolFriendlyName) {
  128. toolFriendlyName = toolFriendlyName || tool;
  129. // Check whether tool command is available at all
  130. let tool_command = shell.which(tool);
  131. if (!tool_command) {
  132. return Q.reject(toolFriendlyName + ' was not found. ' + (message || ''));
  133. }
  134. // check if tool version is greater than specified one
  135. return versions.get_tool_version(tool).then(function (version) {
  136. version = version.trim();
  137. return versions.compareVersions(version, minVersion) >= 0 ?
  138. Q.resolve({ 'version': version }) :
  139. Q.reject('Cordova needs ' + toolFriendlyName + ' version ' + minVersion +
  140. ' or greater, you have version ' + version + '. ' + (message || ''));
  141. });
  142. }
  143. /**
  144. * Object that represents one of requirements for current platform.
  145. * @param {String} id The unique identifier for this requirements.
  146. * @param {String} name The name of requirements. Human-readable field.
  147. * @param {Boolean} isFatal Marks the requirement as fatal. If such requirement will fail
  148. * next requirements' checks will be skipped.
  149. */
  150. let Requirement = function (id, name, isFatal) {
  151. this.id = id;
  152. this.name = name;
  153. this.installed = false;
  154. this.metadata = {};
  155. this.isFatal = isFatal || false;
  156. };
  157. /**
  158. * Methods that runs all checks one by one and returns a result of checks
  159. * as an array of Requirement objects. This method intended to be used by cordova-lib check_reqs method
  160. *
  161. * @return Promise<Requirement[]> Array of requirements. Due to implementation, promise is always fulfilled.
  162. */
  163. module.exports.check_all = function () {
  164. const requirements = [
  165. new Requirement('os', 'Apple macOS', true),
  166. new Requirement('xcode', 'Xcode'),
  167. new Requirement('ios-deploy', 'ios-deploy'),
  168. new Requirement('CocoaPods', 'CocoaPods')
  169. ];
  170. let result = [];
  171. let fatalIsHit = false;
  172. let checkFns = [
  173. module.exports.check_os,
  174. module.exports.check_xcodebuild,
  175. module.exports.check_ios_deploy,
  176. module.exports.check_cocoapods
  177. ];
  178. // Then execute requirement checks one-by-one
  179. return checkFns.reduce(function (promise, checkFn, idx) {
  180. return promise.then(function () {
  181. // If fatal requirement is failed,
  182. // we don't need to check others
  183. if (fatalIsHit) return Q();
  184. let requirement = requirements[idx];
  185. return checkFn()
  186. .then(function (version) {
  187. requirement.installed = true;
  188. requirement.metadata.version = version;
  189. result.push(requirement);
  190. }, function (err) {
  191. if (requirement.isFatal) fatalIsHit = true;
  192. requirement.metadata.reason = err;
  193. result.push(requirement);
  194. });
  195. });
  196. }, Q())
  197. .then(function () {
  198. // When chain is completed, return requirements array to upstream API
  199. return result;
  200. });
  201. };