update.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*******************************
  2. Update Repos
  3. *******************************/
  4. /*
  5. This task update all SUI individual component repos with new versions of components
  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.components.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 component, to avoid issues
  41. stepRepo = function() {
  42. index = index + 1;
  43. if(index >= total) {
  44. callback();
  45. return;
  46. }
  47. var
  48. component = release.components[index],
  49. outputDirectory = path.resolve(path.join(release.outputRoot, component)),
  50. capitalizedComponent = component.charAt(0).toUpperCase() + component.slice(1),
  51. repoName = release.componentRepoRoot + capitalizedComponent,
  52. gitURL = 'https://github.com/' + release.org + '/' + repoName + '.git',
  53. repoURL = 'https://github.com/' + release.org + '/' + repoName + '/',
  54. commitArgs = (oAuth.name !== undefined && oAuth.email !== undefined)
  55. ? '--author "' + oAuth.name + ' <' + oAuth.email + '>"'
  56. : '',
  57. componentPackage = fs.existsSync(outputDirectory + 'package.json' )
  58. ? require(outputDirectory + 'package.json')
  59. : false,
  60. isNewVersion = (version && componentPackage.version != version),
  61. commitMessage = (isNewVersion)
  62. ? 'Updated component to version ' + version
  63. : 'Updated files from main repo',
  64. gitOptions = { cwd: outputDirectory },
  65. commitOptions = { args: commitArgs, cwd: outputDirectory },
  66. releaseOptions = { tag_name: version, owner: release.org, repo: repoName },
  67. fileModeOptions = { args : 'config core.fileMode false', cwd: outputDirectory },
  68. usernameOptions = { args : 'config user.name "' + oAuth.name + '"', cwd: outputDirectory },
  69. emailOptions = { args : 'config user.email "' + oAuth.email + '"', cwd: outputDirectory },
  70. versionOptions = { args : 'rev-parse --verify HEAD', cwd: outputDirectory },
  71. localRepoSetup = fs.existsSync(path.join(outputDirectory, '.git')),
  72. canProceed = true
  73. ;
  74. console.info('Processing repository:' + outputDirectory);
  75. function setConfig() {
  76. git.exec(fileModeOptions, function() {
  77. git.exec(usernameOptions, function () {
  78. git.exec(emailOptions, function () {
  79. commitFiles();
  80. });
  81. });
  82. });
  83. }
  84. // standard path
  85. function commitFiles() {
  86. // commit files
  87. console.info('Committing ' + component + ' files', commitArgs);
  88. gulp.src('./', gitOptions)
  89. .pipe(git.add(gitOptions))
  90. .pipe(git.commit(commitMessage, commitOptions))
  91. .on('error', function(error) {
  92. // canProceed = false; bug in git commit <https://github.com/stevelacy/gulp-git/issues/49>
  93. })
  94. .on('finish', function(callback) {
  95. if(canProceed) {
  96. pushFiles();
  97. }
  98. else {
  99. console.info('Nothing new to commit');
  100. nextRepo();
  101. }
  102. })
  103. ;
  104. }
  105. // push changes to remote
  106. function pushFiles() {
  107. console.info('Pushing files for ' + component);
  108. git.push('origin', 'master', { args: '', cwd: outputDirectory }, function(error) {
  109. console.info('Push completed successfully');
  110. getSHA();
  111. });
  112. }
  113. // gets SHA of last commit
  114. function getSHA() {
  115. git.exec(versionOptions, function(error, version) {
  116. version = version.trim();
  117. createRelease(version);
  118. });
  119. }
  120. // create release on GitHub.com
  121. function createRelease(version) {
  122. if(version) {
  123. releaseOptions.target_commitish = version;
  124. }
  125. github.repos.createRelease(releaseOptions, function() {
  126. nextRepo();
  127. });
  128. }
  129. // Steps to next repository
  130. function nextRepo() {
  131. console.log('Sleeping for 1 second...');
  132. // avoid rate throttling
  133. global.clearTimeout(timer);
  134. timer = global.setTimeout(stepRepo, 100);
  135. }
  136. if(localRepoSetup) {
  137. setConfig();
  138. }
  139. else {
  140. console.error('Repository must be setup before running update components');
  141. }
  142. };
  143. stepRepo();
  144. };