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.

180 lines
5.0 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
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() {
  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. return;
  47. }
  48. var
  49. distribution = release.distributions[index],
  50. outputDirectory = path.resolve(path.join(release.outputRoot, distribution.toLowerCase() )),
  51. repoName = release.distRepoRoot + distribution,
  52. gitURL = 'https://github.com/' + release.org + '/' + repoName + '.git',
  53. repoURL = 'https://github.com/' + release.org + '/' + repoName + '/',
  54. commitArgs = (oAuth.name !== undefined && oAuth.email !== undefined)
  55. ? '--author "' + oAuth.name + ' <' + oAuth.email + '>"'
  56. : '',
  57. distributionPackage = fs.existsSync(outputDirectory + 'package.json' )
  58. ? require(outputDirectory + 'package.json')
  59. : false,
  60. isNewVersion = (version && distributionPackage.version != version),
  61. commitMessage = (isNewVersion)
  62. ? 'Updated distribution to version ' + version
  63. : 'Updated files from main repo',
  64. gitOptions = { cwd: outputDirectory },
  65. commitOptions = { args: commitArgs, cwd: outputDirectory },
  66. releaseOptions = { tag_name: version, owner: release.org, repo: repoName },
  67. fileModeOptions = { args : 'config core.fileMode false', cwd: outputDirectory },
  68. usernameOptions = { args : 'config user.name "' + oAuth.name + '"', cwd: outputDirectory },
  69. emailOptions = { args : 'config user.email "' + oAuth.email + '"', 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 changess 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. createRelease();
  110. });
  111. }
  112. // create release on GitHub.com
  113. function createRelease() {
  114. console.log('Tagging release as ', version);
  115. github.releases.createRelease(releaseOptions, function() {
  116. nextRepo();
  117. tagFiles();
  118. });
  119. tagFiles();
  120. }
  121. // Tags files locally
  122. function tagFiles() {
  123. console.info('Tagging new version ' + distribution, version);
  124. git.tag(version, 'Updated version from semantic-ui (automatic)', function (err) {
  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. };