You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

184 lines
5.1 KiB

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