<commit_before>var CoreObject = require('core-object');
var Promise    = require('ember-cli/lib/ext/promise');
var ssh2       = require('ssh2');
var fs         = require('fs');
var path       = require('path');
var untildify  = require('untildify');
var readFile   = Promise.denodeify(fs.readFile);

var ACTIVE_REVISION_EXT = '.active-revision';

// GENERAL TODO: Figure out how to handle pipeline failure instead of
// just doing Promise.reject

module.exports = CoreObject.extend({
  init: function(options) {
    var plugin = options.plugin;

    this._plugin = plugin;
    this._client = new ssh2.Client();
  },

  upload: function(options) {
    var that           = this;
    var plugin         = this._plugin;
    var allowOverwrite = options.allowOverwrite;
    var revisionKey    = options.revisionKey;
    var filePath       = options.filePath;
    var filename       = path.join(options.remoteDir, options.filePattern + ':' + revisionKey);
    var promisesArray  = [];

    options.autoClose = false;

    options['hosts'].forEach(function (host) {
      options['host'] = host;
      var promise = that.fetchRevisions(options).then(function(revisions) {
        return that._validateRevision(revisions, revisionKey, allowOverwrite);
      })
      .then(readFile.bind(that, filePath))
      .then(function(fileContents) {
        return that._writeFile(fileContents, filename);
      })
      .then(function() {
        plugin.log('✔  ' + filename, { verbose: true });
        that._client.end();

        return Promise.resolve();
      });
      promisesArray.push(promise);
    });
    return Promise.all(promisesArray);
  },

  activate: function(options) {
    var that          = this;
    var plugin        = that._plugin;
    var remoteDir     = options.remoteDir;
    var revisionKey   = options.revisionKey;
    var revisionPath  = path.join(options.remoteDir, options.filePattern + ":" + revisionKey);
    var indexPath     = path.join(options.remoteDir, options.filePattern);
    var promisesArray = [];

    options['hosts'].forEach(function (host) {
      options['host'] = host;
      var promise = that.fetchRevisions(options).then(function(revisions) {
        var revisionExists = that._revisionExists(revisions, revisionKey);

        if (revisionExists) {
          return Promise.all([
            that._copyFile(revisionPath, indexPath),
            that._markActiveRevision(remoteDir, revisionKey)
          ])
          .then(function() {
            plugin.log('✔  ' + revisionPath + " => " + indexPath, { verbose: true });
            return Promise.resolve();
          })
          .catch(function(err) {
            that._client.end();
            return Promise.reject(err);
          })
        } else {
          return Promise.reject('REVISION <' + revisionKey + '> NOT FOUND!');
        }
      });
      promisesArray.push(promise);
    });
    return Promise.all(promiseArray);
  },

  fetchRevisions: function(options) {<commit_msg>Added log for running revision activation<commit_after>var CoreObject = require('core-object');
var Promise    = require('ember-cli/lib/ext/promise');
var ssh2       = require('ssh2');
var fs         = require('fs');
var path       = require('path');
var untildify  = require('untildify');
var readFile   = Promise.denodeify(fs.readFile);

var ACTIVE_REVISION_EXT = '.active-revision';

// GENERAL TODO: Figure out how to handle pipeline failure instead of
// just doing Promise.reject

module.exports = CoreObject.extend({
  init: function(options) {
    var plugin = options.plugin;

    this._plugin = plugin;
    this._client = new ssh2.Client();
  },

  upload: function(options) {
    var that           = this;
    var plugin         = this._plugin;
    var allowOverwrite = options.allowOverwrite;
    var revisionKey    = options.revisionKey;
    var filePath       = options.filePath;
    var filename       = path.join(options.remoteDir, options.filePattern + ':' + revisionKey);
    var promisesArray  = [];

    options.autoClose = false;

    options['hosts'].forEach(function (host) {
      options['host'] = host;
      var promise = that.fetchRevisions(options).then(function(revisions) {
        return that._validateRevision(revisions, revisionKey, allowOverwrite);
      })
      .then(readFile.bind(that, filePath))
      .then(function(fileContents) {
        return that._writeFile(fileContents, filename);
      })
      .then(function() {
        plugin.log('✔  ' + filename, { verbose: true });
        that._client.end();

        return Promise.resolve();
      });
      promisesArray.push(promise);
    });
    return Promise.all(promisesArray);
  },

  activate: function(options) {
    var that          = this;
    var plugin        = that._plugin;
    var remoteDir     = options.remoteDir;
    var revisionKey   = options.revisionKey;
    var revisionPath  = path.join(options.remoteDir, options.filePattern + ":" + revisionKey);
    var indexPath     = path.join(options.remoteDir, options.filePattern);
    var promisesArray = [];
    plugin.log('✔  Running revision activation...', { verbose: true });

    options['hosts'].forEach(function (host) {
      options['host'] = host;
      var promise = that.fetchRevisions(options).then(function(revisions) {
        var revisionExists = that._revisionExists(revisions, revisionKey);

        if (revisionExists) {
          return Promise.all([
            that._copyFile(revisionPath, indexPath),
            that._markActiveRevision(remoteDir, revisionKey)
          ])
          .then(function() {
            plugin.log('✔  ' + revisionPath + " => " + indexPath, { verbose: true });
            return Promise.resolve();
          })
          .catch(function(err) {
            that._client.end();
            return Promise.reject(err);
          })
        } else {
          return Promise.reject('REVISION <' + revisionKey + '> NOT FOUND!');
        }
      });
      promisesArray.push(promise);
    });
    return Promise.all(promiseArray);
  },

  fetchRevisions: function(options) {
{'fraction_non_alphanumeric': 0.12900032992411745, 'fraction_numerical': 0.0009897723523589574, 'mean_word_length': 2.606781677572873, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4836897', 'n_tokens_mistral': 1735, 'n_tokens_neox': 1701, 'n_words': 353}