incrementBuildNum.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. module.exports = function(context) {
  2. var util = require('util'),
  3. fs = require('fs'),
  4. path = require('path'),
  5. xml2js = require('xml2js');
  6. var platforms = context.opts.platforms,
  7. cliCommand = context.cmdLine,
  8. fileName = 'config.xml',
  9. filePath = path.normalize(path.join(context.opts.projectRoot, fileName)),
  10. parser = new xml2js.Parser(),
  11. changeVersion,
  12. platformName,
  13. needRewrite = false,
  14. finishMessage = [];
  15. // hook configuration
  16. var increment = !(cliCommand.indexOf('--no-inc') > -1),
  17. platformVersion = !(cliCommand.indexOf('--no-platform-inc') > -1),
  18. incrementVersion = (cliCommand.indexOf('--inc-version') > -1);
  19. if(!increment) {
  20. console.log('--no-inc flag detected. No build increment executed.');
  21. return;
  22. }
  23. fs.readFile(filePath, { encoding:'utf8' }, function(err, data) {
  24. if(err) throw err;
  25. parser.parseString(data, function (err, result) {
  26. if(err) throw err;
  27. parseConfig(result);
  28. });
  29. });
  30. function parseConfig(configOpts) {
  31. if (platformVersion) {
  32. platforms.forEach(function (platform) {
  33. if(setPlatformInfo(platform))
  34. configOpts = handleResult(configOpts);
  35. });
  36. }
  37. if (incrementVersion) {
  38. changeVersion = 'version';
  39. platformName = 'App';
  40. configOpts = handleResult(configOpts);
  41. }
  42. if(needRewrite) {
  43. rewriteConfig(configOpts);
  44. } else {
  45. console.log(fileName + ' build numbers not changed');
  46. }
  47. }
  48. function rewriteConfig(result) {
  49. fs.writeFile(filePath, buildXML(result), { encoding:'utf8' }, function(err) {
  50. if(err) throw err;
  51. finishMessage.push('Saved in ' + fileName);
  52. finishMessage.forEach( function(line) { console.log(line); } );
  53. });
  54. }
  55. function setPlatformInfo(platform) {
  56. switch (platform) {
  57. case 'android':
  58. changeVersion = 'android-versionCode';
  59. platformName = 'Android';
  60. break;
  61. case 'ios':
  62. changeVersion = 'ios-CFBundleVersion';
  63. platformName = 'iOS';
  64. break;
  65. case 'osx':
  66. changeVersion = 'osx-CFBundleVersion';
  67. platformName = 'OS X';
  68. break;
  69. case 'windows':
  70. changeVersion = 'windows-packageVersion';
  71. platformName = 'Windows';
  72. break;
  73. default:
  74. console.log('This hook supports Android, iOS, OS X, and Windows currently, ' + platform + ' not supported');
  75. return false;
  76. }
  77. return true;
  78. }
  79. function handleResult(result) {
  80. var newVersion = null;
  81. if(result.widget.$[changeVersion]) {
  82. newVersion = processVersionCode(result.widget.$[changeVersion]);
  83. if (newVersion) result.widget.$[changeVersion] = newVersion;
  84. else finishMessage.push(platformName + ' version code still "' + result.widget.$[changeVersion] + '"');
  85. } else {
  86. finishMessage.push(platformName + ' version code not found');
  87. }
  88. if(newVersion) {
  89. needRewrite = true;
  90. finishMessage.push(platformName + ' build number incremented to "' + newVersion + '"');
  91. }
  92. return result;
  93. }
  94. function buildXML(obj) {
  95. var builder = new xml2js.Builder();
  96. builder.options.renderOpts.indent = '\t';
  97. var x = builder.buildObject(obj);
  98. return x.toString();
  99. }
  100. function processVersionCode(code) {
  101. if (!code) return null;
  102. var newCode = code.replace(/[0-9]+$/, newVersion);
  103. if (newCode == code) return null; //Version not changed, no match
  104. return newCode;
  105. }
  106. function newVersion(match, offset, original) {
  107. if(!match) return null;
  108. try {
  109. var l = match.length;
  110. match = parseInt(match) + 1;
  111. return pad(match, l);
  112. } catch (e) {
  113. return null;
  114. }
  115. }
  116. function pad(code, origLen) {
  117. code = code.toString();
  118. if (code.length >= origLen) return code;
  119. while(code.length < origLen) {
  120. code = '0' + code;
  121. }
  122. return code;
  123. }
  124. }