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.

169 lines
4.1 KiB

  1. /*******************************
  2. Init Dist Repos
  3. *******************************/
  4. /*
  5. This task pulls the latest version of distribution from GitHub
  6. * Creates new repo if doesnt exist (locally & GitHub)
  7. * Adds remote it doesnt exists
  8. * Pulls latest changes from repo
  9. */
  10. var
  11. gulp = require('gulp'),
  12. // node dependencies
  13. console = require('better-console'),
  14. del = require('del'),
  15. fs = require('fs'),
  16. path = require('path'),
  17. git = require('gulp-git'),
  18. githubAPI = require('github'),
  19. mkdirp = require('mkdirp'),
  20. // admin files
  21. github = require('../../config/admin/github.js'),
  22. release = require('../../config/admin/release'),
  23. project = require('../../config/project/release'),
  24. // oAuth configuration for GitHub
  25. oAuth = fs.existsSync(__dirname + '/../../config/admin/oauth.js')
  26. ? require('../../config/admin/oauth')
  27. : false,
  28. // shorthand
  29. version = project.version
  30. ;
  31. module.exports = function(callback) {
  32. var
  33. index = -1,
  34. total = release.distributions.length,
  35. timer,
  36. stream,
  37. stepRepo
  38. ;
  39. if(!oAuth) {
  40. console.error('Must add oauth token for GitHub in tasks/config/admin/oauth.js');
  41. return;
  42. }
  43. // Do Git commands synchronously per component, to avoid issues
  44. stepRepo = function() {
  45. index = index + 1;
  46. if(index >= total) {
  47. callback();
  48. return;
  49. }
  50. var
  51. component = release.distributions[index],
  52. lowerCaseComponent = component.toLowerCase(),
  53. outputDirectory = path.resolve(release.outputRoot + lowerCaseComponent),
  54. repoName = release.distRepoRoot + component,
  55. gitOptions = { cwd: outputDirectory },
  56. pullOptions = { args: '-q', cwd: outputDirectory, quiet: true },
  57. resetOptions = { args: '-q --hard', cwd: outputDirectory, quiet: true },
  58. gitURL = 'git@github.com:' + release.org + '/' + repoName + '.git',
  59. repoURL = 'https://github.com/' + release.org + '/' + repoName + '/',
  60. localRepoSetup = fs.existsSync(path.join(outputDirectory, '.git'))
  61. ;
  62. console.log('Processing repository: ' + outputDirectory);
  63. // create folder if doesn't exist
  64. if( !fs.existsSync(outputDirectory) ) {
  65. mkdirp.sync(outputDirectory);
  66. }
  67. // clean folder
  68. if(release.outputRoot.search('../repos') == 0) {
  69. console.info('Cleaning dir', outputDirectory);
  70. del.sync([outputDirectory + '**/*'], {silent: true, force: true});
  71. }
  72. // set-up local repo
  73. function setupRepo() {
  74. if(localRepoSetup) {
  75. addRemote();
  76. }
  77. else {
  78. initRepo();
  79. }
  80. }
  81. function initRepo() {
  82. console.info('Initializing repository for ' + component);
  83. git.init(gitOptions, function(error) {
  84. if(error) {
  85. console.error('Error initializing repo', error);
  86. }
  87. addRemote();
  88. });
  89. }
  90. function createRepo() {
  91. console.info('Creating GitHub repo ' + repoURL);
  92. github.repos.createFromOrg({
  93. org : release.org,
  94. name : repoName,
  95. homepage : release.homepage
  96. }, function() {
  97. setupRepo();
  98. });
  99. }
  100. function addRemote() {
  101. console.info('Adding remote origin as ' + gitURL);
  102. git.addRemote('origin', gitURL, gitOptions, function(){
  103. pullFiles();
  104. });
  105. }
  106. function pullFiles() {
  107. console.info('Pulling ' + component + ' files');
  108. git.pull('origin', 'master', pullOptions, function(error) {
  109. resetFiles();
  110. });
  111. }
  112. function resetFiles() {
  113. console.info('Resetting files to head');
  114. git.reset('HEAD', resetOptions, function(error) {
  115. nextRepo();
  116. });
  117. }
  118. function nextRepo() {
  119. //console.log('Sleeping for 1 second...');
  120. // avoid rate throttling
  121. global.clearTimeout(timer);
  122. timer = global.setTimeout(function() {
  123. stepRepo()
  124. }, 0);
  125. }
  126. if(localRepoSetup) {
  127. pullFiles();
  128. }
  129. else {
  130. setupRepo();
  131. // createRepo() only use to create remote repo (easier to do manually)
  132. }
  133. };
  134. stepRepo();
  135. };