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

versions.js 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 child_process = require('child_process');
  19. var Q = require('q');
  20. exports.get_apple_ios_version = function () {
  21. var d = Q.defer();
  22. child_process.exec('xcodebuild -showsdks', function (error, stdout, stderr) {
  23. if (error) {
  24. d.reject(stderr);
  25. } else {
  26. d.resolve(stdout);
  27. }
  28. });
  29. return d.promise.then(function (output) {
  30. var regex = /[0-9]*\.[0-9]*/;
  31. var versions = [];
  32. var regexIOS = /^iOS \d+/;
  33. output = output.split('\n');
  34. for (var i = 0; i < output.length; i++) {
  35. if (output[i].trim().match(regexIOS)) {
  36. versions[versions.length] = parseFloat(output[i].match(regex)[0]);
  37. }
  38. }
  39. versions.sort();
  40. console.log(versions[0]);
  41. return Q();
  42. }, function (stderr) {
  43. return Q.reject(stderr);
  44. });
  45. };
  46. exports.get_apple_osx_version = function () {
  47. var d = Q.defer();
  48. child_process.exec('xcodebuild -showsdks', function (error, stdout, stderr) {
  49. if (error) {
  50. d.reject(stderr);
  51. } else {
  52. d.resolve(stdout);
  53. }
  54. });
  55. return d.promise.then(function (output) {
  56. var regex = /[0-9]*\.[0-9]*/;
  57. var versions = [];
  58. var regexOSX = /^macOS \d+/;
  59. output = output.split('\n');
  60. for (var i = 0; i < output.length; i++) {
  61. if (output[i].trim().match(regexOSX)) {
  62. versions[versions.length] = parseFloat(output[i].match(regex)[0]);
  63. }
  64. }
  65. versions.sort();
  66. console.log(versions[0]);
  67. return Q();
  68. }, function (stderr) {
  69. return Q.reject(stderr);
  70. });
  71. };
  72. exports.get_apple_xcode_version = function () {
  73. var d = Q.defer();
  74. child_process.exec('xcodebuild -version', function (error, stdout, stderr) {
  75. var versionMatch = /Xcode (.*)/.exec(stdout);
  76. if (error || !versionMatch) {
  77. d.reject(stderr);
  78. } else {
  79. d.resolve(versionMatch[1]);
  80. }
  81. });
  82. return d.promise;
  83. };
  84. /**
  85. * Gets ios-deploy util version
  86. * @return {Promise} Promise that either resolved with ios-deploy version
  87. * or rejected in case of error
  88. */
  89. exports.get_ios_deploy_version = function () {
  90. var d = Q.defer();
  91. child_process.exec('ios-deploy --version', function (error, stdout, stderr) {
  92. if (error) {
  93. d.reject(stderr);
  94. } else {
  95. d.resolve(stdout);
  96. }
  97. });
  98. return d.promise;
  99. };
  100. /**
  101. * Gets pod (CocoaPods) util version
  102. * @return {Promise} Promise that either resolved with pod version
  103. * or rejected in case of error
  104. */
  105. exports.get_cocoapods_version = function () {
  106. var d = Q.defer();
  107. child_process.exec('pod --version', function (error, stdout, stderr) {
  108. if (error) {
  109. d.reject(stderr);
  110. } else {
  111. d.resolve(stdout);
  112. }
  113. });
  114. return d.promise;
  115. };
  116. /**
  117. * Gets ios-sim util version
  118. * @return {Promise} Promise that either resolved with ios-sim version
  119. * or rejected in case of error
  120. */
  121. exports.get_ios_sim_version = function () {
  122. var d = Q.defer();
  123. child_process.exec('ios-sim --version', function (error, stdout, stderr) {
  124. if (error) {
  125. d.reject(stderr);
  126. } else {
  127. d.resolve(stdout);
  128. }
  129. });
  130. return d.promise;
  131. };
  132. /**
  133. * Gets specific tool version
  134. * @param {String} toolName Tool name to check. Known tools are 'xcodebuild', 'ios-sim' and 'ios-deploy'
  135. * @return {Promise} Promise that either resolved with tool version
  136. * or rejected in case of error
  137. */
  138. exports.get_tool_version = function (toolName) {
  139. switch (toolName) {
  140. case 'xcodebuild': return exports.get_apple_xcode_version();
  141. case 'ios-sim': return exports.get_ios_sim_version();
  142. case 'ios-deploy': return exports.get_ios_deploy_version();
  143. case 'pod': return exports.get_cocoapods_version();
  144. default: return Q.reject(toolName + ' is not valid tool name. Valid names are: \'xcodebuild\', \'ios-sim\', \'ios-deploy\', and \'pod\'');
  145. }
  146. };
  147. /**
  148. * Compares two semver-notated version strings. Returns number
  149. * that indicates equality of provided version strings.
  150. * @param {String} version1 Version to compare
  151. * @param {String} version2 Another version to compare
  152. * @return {Number} Negative number if first version is lower than the second,
  153. * positive otherwise and 0 if versions are equal.
  154. */
  155. exports.compareVersions = function (version1, version2) {
  156. function parseVer (version) {
  157. return version.split('.').map(function (value) {
  158. // try to convert version segment to Number
  159. var parsed = Number(value);
  160. // Number constructor is strict enough and will return NaN
  161. // if conversion fails. In this case we won't be able to compare versions properly
  162. if (isNaN(parsed)) {
  163. throw 'Version should contain only numbers and dots';
  164. }
  165. return parsed;
  166. });
  167. }
  168. var parsedVer1 = parseVer(version1);
  169. var parsedVer2 = parseVer(version2);
  170. // Compare corresponding segments of each version
  171. for (var i = 0; i < Math.max(parsedVer1.length, parsedVer2.length); i++) {
  172. // if segment is not specified, assume that it is 0
  173. // E.g. 3.1 is equal to 3.1.0
  174. var ret = (parsedVer1[i] || 0) - (parsedVer2[i] || 0);
  175. // if segments are not equal, we're finished
  176. if (ret !== 0) return ret;
  177. }
  178. return 0;
  179. };