Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

versions.js 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. var semver = require('semver');
  21. exports.get_apple_ios_version = function () {
  22. var d = Q.defer();
  23. child_process.exec('xcodebuild -showsdks', function (error, stdout, stderr) {
  24. if (error) {
  25. d.reject(stderr);
  26. } else {
  27. d.resolve(stdout);
  28. }
  29. });
  30. return d.promise.then(function (output) {
  31. var regex = /[0-9]*\.[0-9]*/;
  32. var versions = [];
  33. var regexIOS = /^iOS \d+/;
  34. output = output.split('\n');
  35. for (var i = 0; i < output.length; i++) {
  36. if (output[i].trim().match(regexIOS)) {
  37. versions[versions.length] = parseFloat(output[i].match(regex)[0]);
  38. }
  39. }
  40. versions.sort();
  41. console.log(versions[0]);
  42. return Q();
  43. }, function (stderr) {
  44. return Q.reject(stderr);
  45. });
  46. };
  47. exports.get_apple_osx_version = function () {
  48. var d = Q.defer();
  49. child_process.exec('xcodebuild -showsdks', function (error, stdout, stderr) {
  50. if (error) {
  51. d.reject(stderr);
  52. } else {
  53. d.resolve(stdout);
  54. }
  55. });
  56. return d.promise.then(function (output) {
  57. var regex = /[0-9]*\.[0-9]*/;
  58. var versions = [];
  59. var regexOSX = /^macOS \d+/;
  60. output = output.split('\n');
  61. for (var i = 0; i < output.length; i++) {
  62. if (output[i].trim().match(regexOSX)) {
  63. versions[versions.length] = parseFloat(output[i].match(regex)[0]);
  64. }
  65. }
  66. versions.sort();
  67. console.log(versions[0]);
  68. return Q();
  69. }, function (stderr) {
  70. return Q.reject(stderr);
  71. });
  72. };
  73. exports.get_apple_xcode_version = function () {
  74. var d = Q.defer();
  75. child_process.exec('xcodebuild -version', function (error, stdout, stderr) {
  76. var versionMatch = /Xcode (.*)/.exec(stdout);
  77. if (error || !versionMatch) {
  78. d.reject(stderr);
  79. } else {
  80. d.resolve(versionMatch[1]);
  81. }
  82. });
  83. return d.promise;
  84. };
  85. /**
  86. * Gets ios-deploy util version
  87. * @return {Promise} Promise that either resolved with ios-deploy version
  88. * or rejected in case of error
  89. */
  90. exports.get_ios_deploy_version = function () {
  91. var d = Q.defer();
  92. child_process.exec('ios-deploy --version', function (error, stdout, stderr) {
  93. if (error) {
  94. d.reject(stderr);
  95. } else {
  96. d.resolve(stdout);
  97. }
  98. });
  99. return d.promise;
  100. };
  101. /**
  102. * Gets pod (CocoaPods) util version
  103. * @return {Promise} Promise that either resolved with pod version
  104. * or rejected in case of error
  105. */
  106. exports.get_cocoapods_version = function () {
  107. var d = Q.defer();
  108. child_process.exec('pod --version', function (error, stdout, stderr) {
  109. if (error) {
  110. d.reject(stderr);
  111. } else {
  112. d.resolve(stdout);
  113. }
  114. });
  115. return d.promise;
  116. };
  117. /**
  118. * Gets ios-sim util version
  119. * @return {Promise} Promise that either resolved with ios-sim version
  120. * or rejected in case of error
  121. */
  122. exports.get_ios_sim_version = function () {
  123. var d = Q.defer();
  124. child_process.exec('ios-sim --version', function (error, stdout, stderr) {
  125. if (error) {
  126. d.reject(stderr);
  127. } else {
  128. d.resolve(stdout);
  129. }
  130. });
  131. return d.promise;
  132. };
  133. /**
  134. * Gets specific tool version
  135. * @param {String} toolName Tool name to check. Known tools are 'xcodebuild', 'ios-sim' and 'ios-deploy'
  136. * @return {Promise} Promise that either resolved with tool version
  137. * or rejected in case of error
  138. */
  139. exports.get_tool_version = function (toolName) {
  140. switch (toolName) {
  141. case 'xcodebuild': return exports.get_apple_xcode_version();
  142. case 'ios-sim': return exports.get_ios_sim_version();
  143. case 'ios-deploy': return exports.get_ios_deploy_version();
  144. case 'pod': return exports.get_cocoapods_version();
  145. default: return Q.reject(toolName + ' is not valid tool name. Valid names are: \'xcodebuild\', \'ios-sim\', \'ios-deploy\', and \'pod\'');
  146. }
  147. };
  148. /**
  149. * Compares two version strings that can be coerced to semver.
  150. *
  151. * @param {String} version1 Version to compare
  152. * @param {String} version2 Another version to compare
  153. * @return {Number} Negative number if first version is lower than the second,
  154. * positive otherwise and 0 if versions are equal.
  155. */
  156. exports.compareVersions = (...args) => {
  157. const coerceToSemverIfInvalid = v => {
  158. const semverVersion = semver.parse(v) || semver.coerce(v);
  159. if (!semverVersion) throw new TypeError(`Invalid Version: ${v}`);
  160. return semverVersion;
  161. };
  162. const semverVersions = args.map(coerceToSemverIfInvalid);
  163. return semver.compare(...semverVersions);
  164. };