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.

172 lines
4.8 KiB

10 years ago
10 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() {
  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. return;
  47. }
  48. var
  49. component = release.components[index]
  50. outputDirectory = path.resolve(path.join(release.outputRoot, component)),
  51. capitalizedComponent = component.charAt(0).toUpperCase() + component.slice(1),
  52. repoName = release.componentRepoRoot + capitalizedComponent,
  53. gitURL = 'https://github.com/' + release.org + '/' + repoName + '.git',
  54. repoURL = 'https://github.com/' + release.org + '/' + repoName + '/',
  55. commitArgs = (oAuth.name !== undefined && oAuth.email !== undefined)
  56. ? '--author "' + oAuth.name + ' <' + oAuth.email + '>"'
  57. : '',
  58. componentPackage = fs.existsSync(outputDirectory + 'package.json' )
  59. ? require(outputDirectory + 'package.json')
  60. : false,
  61. isNewVersion = (version && componentPackage.version != version),
  62. commitMessage = (isNewVersion)
  63. ? 'Updated component to version ' + version
  64. : 'Updated files from main repo',
  65. gitOptions = { cwd: outputDirectory },
  66. commitOptions = { args: commitArgs, cwd: outputDirectory },
  67. releaseOptions = { tag_name: version, owner: release.org, repo: repoName },
  68. fileModeOptions = { args : 'config core.fileMode false', cwd: outputDirectory },
  69. usernameOptions = { args : 'config user.name "' + oAuth.name + '"', cwd: outputDirectory },
  70. emailOptions = { args : 'config user.email "' + oAuth.email + '"', 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 changess 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. createRelease();
  111. });
  112. }
  113. // create release on GitHub.com
  114. function createRelease() {
  115. console.log('Tagging release as ', version);
  116. github.releases.createRelease(releaseOptions, function() {
  117. nextRepo();
  118. });
  119. }
  120. // Steps to next repository
  121. function nextRepo() {
  122. console.log('Sleeping for 1 second...');
  123. // avoid rate throttling
  124. global.clearTimeout(timer);
  125. timer = global.setTimeout(stepRepo, 1000);
  126. }
  127. if(localRepoSetup) {
  128. setConfig();
  129. }
  130. else {
  131. console.error('Repository must be setup before running update components');
  132. }
  133. };
  134. stepRepo();
  135. };