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.

182 lines
5.1 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 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. 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. distributionPackage = fs.existsSync(outputDirectory + 'package.json' )
  59. ? require(outputDirectory + 'package.json')
  60. : false,
  61. isNewVersion = (version && distributionPackage.version != version),
  62. commitMessage = (isNewVersion)
  63. ? 'Updated distribution 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. versionOptions = { args : 'rev-parse --verify HEAD', cwd: outputDirectory },
  72. localRepoSetup = fs.existsSync(path.join(outputDirectory, '.git')),
  73. canProceed = true
  74. ;
  75. console.info('Processing repository:' + outputDirectory);
  76. function setConfig() {
  77. git.exec(fileModeOptions, function() {
  78. git.exec(usernameOptions, function () {
  79. git.exec(emailOptions, function () {
  80. commitFiles();
  81. });
  82. });
  83. });
  84. }
  85. // standard path
  86. function commitFiles() {
  87. // commit files
  88. console.info('Committing ' + distribution + ' files', commitArgs);
  89. gulp.src('./', gitOptions)
  90. .pipe(git.add(gitOptions))
  91. .pipe(git.commit(commitMessage, commitOptions))
  92. .on('error', function(error) {
  93. // canProceed = false; bug in git commit <https://github.com/stevelacy/gulp-git/issues/49>
  94. })
  95. .on('finish', function(callback) {
  96. if(canProceed) {
  97. pushFiles();
  98. }
  99. else {
  100. console.info('Nothing new to commit');
  101. nextRepo();
  102. }
  103. })
  104. ;
  105. }
  106. // push changes to remote
  107. function pushFiles() {
  108. console.info('Pushing files for ' + distribution);
  109. git.push('origin', 'master', { args: '', cwd: outputDirectory }, function(error) {
  110. console.info('Push completed successfully');
  111. getSHA();
  112. });
  113. }
  114. // gets SHA of last commit
  115. function getSHA() {
  116. git.exec(versionOptions, function(error, version) {
  117. version = version.trim();
  118. createRelease(version);
  119. });
  120. }
  121. // create release on GitHub.com
  122. function createRelease(version) {
  123. if(version) {
  124. releaseOptions.target_commitish = version;
  125. }
  126. github.releases.createRelease(releaseOptions, function() {
  127. nextRepo();
  128. });
  129. }
  130. // Steps to next repository
  131. function nextRepo() {
  132. console.log('Sleeping for 1 second...');
  133. // avoid rate throttling
  134. global.clearTimeout(timer);
  135. timer = global.setTimeout(stepRepo, 500);
  136. }
  137. if(localRepoSetup) {
  138. setConfig();
  139. }
  140. else {
  141. console.error('Repository must be setup before running update distributions');
  142. }
  143. };
  144. stepRepo();
  145. };