versions.js 5.9 KB

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