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.

205 lines
5.6 KiB

  1. /*******************************
  2. Update Repos
  3. *******************************/
  4. /*
  5. This task update all SUI individual component repos with new versions of components
  6. * Creates new repo if doesnt exist (locally & GitHub)
  7. * Adds remote it doesnt exists
  8. * Commits changes
  9. * Pulls latest changes from repo
  10. * Merges changes if necessary
  11. * Tag new releases if version changed in main repo
  12. * Pushes changes to GitHub
  13. */
  14. var
  15. gulp = require('gulp'),
  16. // node dependencies
  17. fs = require('fs'),
  18. git = require('gulp-git'),
  19. githubAPI = require('github'),
  20. requireDotFile = require('require-dot-file'),
  21. // admin files
  22. github = require('../config/admin/github.js'),
  23. oAuth = require('../config/admin/oauth.js'),
  24. release = require('../config/admin/release'),
  25. package = requireDotFile('package.json'),
  26. version = package.version
  27. ;
  28. module.exports = function() {
  29. var
  30. index = -1,
  31. total = release.components.length,
  32. stream,
  33. stepRepo
  34. ;
  35. console.log('Handling git');
  36. // Do Git commands synchronously per component, to avoid issues
  37. stepRepo = function() {
  38. index = index + 1;
  39. if(index >= total) {
  40. return;
  41. }
  42. var
  43. component = release.components[index],
  44. outputDirectory = release.outputRoot + component + '/',
  45. capitalizedComponent = component.charAt(0).toUpperCase() + component.slice(1),
  46. repoName = release.repoRoot + capitalizedComponent,
  47. gitOptions = { cwd: outputDirectory },
  48. quietOptions = { args: '-q', cwd: outputDirectory },
  49. isRepository = fs.existsSync(outputDirectory + '.git/'),
  50. gitURL = 'https://github.com/' + release.org + '/' + repoName + '.git',
  51. repoURL = 'https://github.com/' + release.org + '/' + repoName + '/',
  52. componentPackage = fs.existsSync(outputDirectory + 'package.json' )
  53. ? require(outputDirectory + 'package.json')
  54. : false,
  55. isNewVersion = (version && componentPackage.version != version),
  56. mergeMessage = 'Merged from upstream',
  57. commitMessage = (isNewVersion)
  58. ? 'Updated component to version ' + version
  59. : 'Updated component release from Semantic-UI (Automatic)',
  60. commitArgs = (oAuth.name !== undefined && oAuth.email !== undefined)
  61. ? '--author "' + oAuth.name + ' <' + oAuth.email + '>"'
  62. : ''
  63. ;
  64. console.log('Processing repository:' + outputDirectory);
  65. // standard path
  66. function commitFiles() {
  67. // commit files
  68. console.log('Committing files', commitArgs);
  69. gulp.src('**/*', gitOptions)
  70. .pipe(git.add(gitOptions))
  71. .pipe(git.commit(commitMessage, { args: commitArgs, cwd: outputDirectory }))
  72. .on('error', function(error) {
  73. console.log('Nothing new to commit');
  74. stepRepo();
  75. })
  76. .on('finish', function(callback) {
  77. pullFiles();
  78. })
  79. ;
  80. }
  81. function pullFiles() {
  82. console.log('Pulling files');
  83. git.pull('origin', 'master', { args: '', cwd: outputDirectory }, function(error) {
  84. if(error && error.message.search("Couldn't find remote ref") != -1) {
  85. createRepo();
  86. }
  87. else {
  88. console.log('Pull completed successfully');
  89. mergeCommit();
  90. }
  91. });
  92. }
  93. function mergeCommit() {
  94. // commit files
  95. console.log('Adding merge commit', commitArgs);
  96. gulp.src('', gitOptions)
  97. .pipe(git.add(gitOptions))
  98. .pipe(git.commit(mergeMessage, { args: commitArgs, cwd: outputDirectory }))
  99. .on('error', function(error) {
  100. console.log('Nothing new to merge', error);
  101. })
  102. .on('finish', function(callback) {
  103. tagFiles();
  104. })
  105. ;
  106. }
  107. function tagFiles() {
  108. console.log('Tagging new version ', version);
  109. git.tag(version, 'Updated version from semantic-ui (automatic)', function (err) {
  110. pushFiles();
  111. });
  112. }
  113. function pushFiles() {
  114. console.log('Pushing files');
  115. git.push('origin', 'master', { args: '', cwd: outputDirectory }, function(error) {
  116. if(error && error.message.search("Couldn't find remote ref") == -1) {
  117. createRepo();
  118. }
  119. console.log('Push completed successfully');
  120. stepRepo();
  121. });
  122. }
  123. // set-up path
  124. function createRepo() {
  125. console.log('Creating repository ' + repoURL);
  126. github.repos.createFromOrg({
  127. org : release.org,
  128. name : repoName,
  129. homepage : release.homepage
  130. }, function() {
  131. if(isRepository) {
  132. addRemote();
  133. }
  134. else {
  135. initRepo();
  136. }
  137. });
  138. }
  139. function initRepo() {
  140. console.log('Initializing repository in ' + outputDirectory);
  141. git.init(gitOptions, function(error) {
  142. if(error) {
  143. console.error('Error initializing repo');
  144. return;
  145. }
  146. addRemote();
  147. });
  148. }
  149. function addRemote() {
  150. console.log('Adding remote origin as ' + gitURL);
  151. git.addRemote('origin', gitURL, gitOptions, firstPushFiles);
  152. }
  153. function firstPushFiles() {
  154. console.log('Pushing files');
  155. git.push('origin', 'master', { args: '-u', cwd: outputDirectory }, function(error) {
  156. if(error) {
  157. console.log(error);
  158. pullFiles();
  159. }
  160. else {
  161. console.log('First push completed successfully');
  162. stepRepo();
  163. }
  164. });
  165. }
  166. if(isRepository) {
  167. commitFiles();
  168. }
  169. else {
  170. createRepo();
  171. }
  172. };
  173. return stepRepo();
  174. };