github.com/fanux/shipyard@v0.0.0-20161009071005-6515ce223235/controller/static/semantic/tasks/admin/distributions/update.js (about)

     1  /*******************************
     2            Update Repos
     3  *******************************/
     4  
     5  /*
     6  
     7   This task update all SUI individual distribution repos with new versions of distributions
     8  
     9    * Commits changes from create repo
    10    * Pushes changes to GitHub
    11    * Tag new releases if version changed in main repo
    12  
    13  */
    14  
    15  var
    16    gulp           = require('gulp'),
    17  
    18    // node dependencies
    19    console        = require('better-console'),
    20    fs             = require('fs'),
    21    path           = require('path'),
    22    git            = require('gulp-git'),
    23    githubAPI      = require('github'),
    24    requireDotFile = require('require-dot-file'),
    25  
    26    // admin files
    27    github         = require('../../config/admin/github.js'),
    28    release        = require('../../config/admin/release'),
    29    project        = require('../../config/project/release'),
    30  
    31  
    32    // oAuth configuration for GitHub
    33    oAuth          = fs.existsSync(__dirname + '/../../config/admin/oauth.js')
    34      ? require('../../config/admin/oauth')
    35      : false,
    36  
    37    // shorthand
    38    version = project.version
    39  ;
    40  
    41  module.exports = function(callback) {
    42  
    43    var
    44      index = -1,
    45      total = release.distributions.length,
    46      timer,
    47      stream,
    48      stepRepo
    49    ;
    50  
    51    if(!oAuth) {
    52      console.error('Must add oauth token for GitHub in tasks/config/admin/oauth.js');
    53      return;
    54    }
    55  
    56    // Do Git commands synchronously per distribution, to avoid issues
    57    stepRepo = function() {
    58  
    59      index = index + 1;
    60      if(index >= total) {
    61        callback();
    62        return;
    63      }
    64  
    65      var
    66        distribution         = release.distributions[index],
    67        outputDirectory      = path.resolve(path.join(release.outputRoot, distribution.toLowerCase() )),
    68        repoName             = release.distRepoRoot + distribution,
    69  
    70        gitURL               = 'https://github.com/' + release.org + '/' + repoName + '.git',
    71        repoURL              = 'https://github.com/' + release.org + '/' + repoName + '/',
    72  
    73        commitArgs = (oAuth.name !== undefined && oAuth.email !== undefined)
    74          ? '--author "' + oAuth.name + ' <' + oAuth.email + '>"'
    75          : '',
    76  
    77        distributionPackage = fs.existsSync(outputDirectory + 'package.json' )
    78          ? require(outputDirectory + 'package.json')
    79          : false,
    80  
    81        isNewVersion  = (version && distributionPackage.version != version),
    82  
    83        commitMessage = (isNewVersion)
    84          ? 'Updated distribution to version ' + version
    85          : 'Updated files from main repo',
    86  
    87        gitOptions      = { cwd: outputDirectory },
    88        commitOptions   = { args: commitArgs, cwd: outputDirectory },
    89        releaseOptions  = { tag_name: version, owner: release.org, repo: repoName },
    90  
    91        fileModeOptions = { args : 'config core.fileMode false', cwd: outputDirectory },
    92        usernameOptions = { args : 'config user.name "' + oAuth.name + '"', cwd: outputDirectory },
    93        emailOptions    = { args : 'config user.email "' + oAuth.email + '"', cwd: outputDirectory },
    94        versionOptions =  { args : 'rev-parse --verify HEAD', cwd: outputDirectory },
    95  
    96        localRepoSetup  = fs.existsSync(path.join(outputDirectory, '.git')),
    97        canProceed      = true
    98      ;
    99  
   100  
   101      console.info('Processing repository:' + outputDirectory);
   102  
   103      function setConfig() {
   104        git.exec(fileModeOptions, function() {
   105          git.exec(usernameOptions, function () {
   106            git.exec(emailOptions, function () {
   107              commitFiles();
   108            });
   109          });
   110        });
   111      }
   112  
   113      // standard path
   114      function commitFiles() {
   115        // commit files
   116        console.info('Committing ' + distribution + ' files', commitArgs);
   117        gulp.src('**/*', gitOptions)
   118          .pipe(git.add(gitOptions))
   119          .pipe(git.commit(commitMessage, commitOptions))
   120          .on('error', function(error) {
   121            // canProceed = false; bug in git commit <https://github.com/stevelacy/gulp-git/issues/49>
   122          })
   123          .on('finish', function(callback) {
   124            if(canProceed) {
   125              pushFiles();
   126            }
   127            else {
   128              console.info('Nothing new to commit');
   129              nextRepo();
   130            }
   131          })
   132        ;
   133      }
   134  
   135      // push changes to remote
   136      function pushFiles() {
   137        console.info('Pushing files for ' + distribution);
   138        git.push('origin', 'master', { args: '', cwd: outputDirectory }, function(error) {
   139          console.info('Push completed successfully');
   140          getSHA();
   141        });
   142      }
   143  
   144      // gets SHA of last commit
   145      function getSHA() {
   146        git.exec(versionOptions, function(error, version) {
   147          version = version.trim();
   148          createRelease(version);
   149        });
   150      }
   151  
   152      // create release on GitHub.com
   153      function createRelease(version) {
   154        if(version) {
   155          releaseOptions.target_commitish = version;
   156        }
   157        github.releases.createRelease(releaseOptions, function() {
   158          nextRepo();
   159        });
   160      }
   161  
   162      // Steps to next repository
   163      function nextRepo() {
   164        console.log('Sleeping for 1 second...');
   165        // avoid rate throttling
   166        global.clearTimeout(timer);
   167        timer = global.setTimeout(stepRepo, 500);
   168      }
   169  
   170  
   171      if(localRepoSetup) {
   172        setConfig();
   173      }
   174      else {
   175        console.error('Repository must be setup before running update distributions');
   176      }
   177  
   178    };
   179  
   180    stepRepo();
   181  
   182  };