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.

179 lines
4.9 KiB

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