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.

190 lines
5.4 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. let
  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. requireDotFile = require('require-dot-file'),
  18. // admin files
  19. github = require('../../config/admin/github.js'),
  20. release = require('../../config/admin/release'),
  21. project = require('../../config/project/release'),
  22. // oAuth configuration for GitHub
  23. oAuth = fs.existsSync(__dirname + '/../../config/admin/oauth.js')
  24. ? require('../../config/admin/oauth')
  25. : false,
  26. // shorthand
  27. version = project.version
  28. ;
  29. module.exports = function(callback) {
  30. let
  31. index = -1,
  32. total = release.components.length,
  33. timer,
  34. stream,
  35. stepRepo
  36. ;
  37. if(!oAuth) {
  38. console.error('Must add oauth token for GitHub in tasks/config/admin/oauth.js');
  39. return;
  40. }
  41. // Do Git commands synchronously per component, to avoid issues
  42. stepRepo = function() {
  43. index = index + 1;
  44. if(index >= total) {
  45. callback();
  46. return;
  47. }
  48. let
  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. 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 ' + component + ' 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 ' + component);
  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. async function createRelease(version) {
  123. if(version) {
  124. releaseOptions.target_commitish = version;
  125. }
  126. console.info('-----------------------------');
  127. console.info(releaseOptions);
  128. console.info('-----------------------------');
  129. try {
  130. await github.repos.createRelease(releaseOptions)
  131. }
  132. catch(e) {
  133. console.error(`Release creation failed. Most likely already released "${releaseOptions.tag_name}"`);
  134. };
  135. nextRepo();
  136. }
  137. // Steps to next repository
  138. function nextRepo() {
  139. console.log('Sleeping for 1 second...');
  140. // avoid rate throttling
  141. global.clearTimeout(timer);
  142. timer = global.setTimeout(stepRepo, 100);
  143. }
  144. if(localRepoSetup) {
  145. setConfig();
  146. }
  147. else {
  148. console.error('Repository must be setup before running update components');
  149. }
  150. };
  151. stepRepo();
  152. };