update.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*******************************
  2. Update Repos
  3. *******************************/
  4. /*
  5. This task update all SUI individual distribution repos with new versions of distributions
  6. * Commits changes from create repo
  7. * Pushes changes to GitHub
  8. * Tag new releases if version changed in main repo
  9. */
  10. var
  11. gulp = require('gulp'),
  12. // node dependencies
  13. console = require('better-console'),
  14. fs = require('fs'),
  15. path = require('path'),
  16. git = require('gulp-git'),
  17. // admin files
  18. github = require('../../config/admin/github.js'),
  19. release = require('../../config/admin/release'),
  20. project = require('../../config/project/release'),
  21. // oAuth configuration for GitHub
  22. oAuth = fs.existsSync(__dirname + '/../../config/admin/oauth.js')
  23. ? require('../../config/admin/oauth')
  24. : false,
  25. // shorthand
  26. version = project.version
  27. ;
  28. module.exports = function(callback) {
  29. var
  30. index = -1,
  31. total = release.distributions.length,
  32. timer,
  33. stream,
  34. stepRepo
  35. ;
  36. if(!oAuth) {
  37. console.error('Must add oauth token for GitHub in tasks/config/admin/oauth.js');
  38. return;
  39. }
  40. // Do Git commands synchronously per distribution, to avoid issues
  41. stepRepo = function() {
  42. index = index + 1;
  43. if(index >= total) {
  44. callback();
  45. return;
  46. }
  47. var
  48. distribution = release.distributions[index],
  49. outputDirectory = path.resolve(path.join(release.outputRoot, distribution.toLowerCase() )),
  50. repoName = release.distRepoRoot + distribution,
  51. commitArgs = (oAuth.name !== undefined && oAuth.email !== undefined)
  52. ? '--author "' + oAuth.name + ' <' + oAuth.email + '>"'
  53. : '',
  54. distributionPackage = fs.existsSync(outputDirectory + 'package.json' )
  55. ? require(outputDirectory + 'package.json')
  56. : false,
  57. isNewVersion = (version && distributionPackage.version != version),
  58. commitMessage = (isNewVersion)
  59. ? 'Updated distribution to version ' + version
  60. : 'Updated files from main repo',
  61. gitOptions = { cwd: outputDirectory },
  62. commitOptions = { args: commitArgs, cwd: outputDirectory },
  63. releaseOptions = { tag_name: version, owner: release.org, repo: repoName },
  64. fileModeOptions = { args : 'config core.fileMode false', cwd: outputDirectory },
  65. usernameOptions = { args : 'config user.name "' + oAuth.name + '"', cwd: outputDirectory },
  66. emailOptions = { args : 'config user.email "' + oAuth.email + '"', cwd: outputDirectory },
  67. versionOptions = { args : 'rev-parse --verify HEAD', cwd: outputDirectory },
  68. localRepoSetup = fs.existsSync(path.join(outputDirectory, '.git')),
  69. canProceed = true
  70. ;
  71. console.info('Processing repository:' + outputDirectory);
  72. function setConfig() {
  73. git.exec(fileModeOptions, function() {
  74. git.exec(usernameOptions, function () {
  75. git.exec(emailOptions, function () {
  76. commitFiles();
  77. });
  78. });
  79. });
  80. }
  81. // standard path
  82. function commitFiles() {
  83. // commit files
  84. console.info('Committing ' + distribution + ' files', commitArgs);
  85. gulp.src('./', gitOptions)
  86. .pipe(git.add(gitOptions))
  87. .pipe(git.commit(commitMessage, commitOptions))
  88. .on('error', function(error) {
  89. // canProceed = false; bug in git commit <https://github.com/stevelacy/gulp-git/issues/49>
  90. })
  91. .on('finish', function(callback) {
  92. if(canProceed) {
  93. pushFiles();
  94. }
  95. else {
  96. console.info('Nothing new to commit');
  97. nextRepo();
  98. }
  99. })
  100. ;
  101. }
  102. // push changes to remote
  103. function pushFiles() {
  104. console.info('Pushing files for ' + distribution);
  105. git.push('origin', 'master', { args: '', cwd: outputDirectory }, function(error) {
  106. console.info('Push completed successfully');
  107. getSHA();
  108. });
  109. }
  110. // gets SHA of last commit
  111. function getSHA() {
  112. git.exec(versionOptions, function(error, version) {
  113. version = version.trim();
  114. createRelease(version);
  115. });
  116. }
  117. // create release on GitHub.com
  118. function createRelease(version) {
  119. if(version) {
  120. releaseOptions.target_commitish = version;
  121. }
  122. github.repos.createRelease(releaseOptions, function() {
  123. nextRepo();
  124. });
  125. }
  126. // Steps to next repository
  127. function nextRepo() {
  128. console.log('Sleeping for 1 second...');
  129. // avoid rate throttling
  130. global.clearTimeout(timer);
  131. timer = global.setTimeout(stepRepo, 100);
  132. }
  133. if(localRepoSetup) {
  134. setConfig();
  135. }
  136. else {
  137. console.error('Repository must be setup before running update distributions');
  138. }
  139. };
  140. stepRepo();
  141. };