move version rationalization to it's own class

This commit is contained in:
Spencer Alger
2015-07-02 10:17:19 -07:00
parent cfe5ef44a2
commit f52a59acc6
4 changed files with 76 additions and 53 deletions

View File

@ -2,6 +2,8 @@ var utils = require('../utils');
var _ = require('lodash'); var _ = require('lodash');
var join = require('path').join; var join = require('path').join;
var Version = require('../../scripts/Version');
var defaultOpts = { var defaultOpts = {
directory: join(__dirname, '..', '..', '.esvm'), directory: join(__dirname, '..', '..', '.esvm'),
nodes: 1, nodes: 1,
@ -23,39 +25,28 @@ var defaultOpts = {
* @param {[type]} target - the grunt target to configure * @param {[type]} target - the grunt target to configure
*/ */
function setConfig(ref, target) { function setConfig(ref, target) {
var minorV = String(ref).replace(/^v/, '').replace(/(\d+\.\d+)\..+/, '$1'); var v = Version.fromBranch(String(ref).replace(/^v/, '').replace(/(\d+\.\d+)\..+/, '$1'));
var config = target.options.config = (target.options.config || {});
switch (minorV) { if (v.satisfies('^1.2')) {
case '0.90': _.merge(config, {
case '1.0':
case '1.1':
// no special treatment
break;
case '1.2':
case '1.3':
case '1.4':
case '1.5':
case '1.6':
case '1.x':
target.options.config = _.merge({
'node.bench': true, 'node.bench': true,
'script.disable_dynamic': false 'script.disable_dynamic': false
}, target.options.config); });
break; }
default:
target.options.config = _.merge({ if (v.satisfies('>=1.6')) {
_.merge(config, {
'node.bench': true, 'node.bench': true,
'script.inline': true, 'script.inline': true,
'script.indexed': true 'script.indexed': true
}, target.options.config); });
break;
} }
_.defaultsDeep(target.options, defaultOpts);
target.options = _.merge({}, defaultOpts, target.options); if (v.satisfies('>=1.6')) {
delete config['discovery.zen.ping_timeout'];
if (minorV === 'master') {
delete target.options.config['discovery.zen.ping_timeout'];
} }
if (target.options.branch && !target.options.version) { if (target.options.branch && !target.options.version) {

View File

@ -72,7 +72,7 @@
"nock": "~0.28.3", "nock": "~0.28.3",
"open": "0.0.4", "open": "0.0.4",
"optimist": "~0.6.0", "optimist": "~0.6.0",
"semver": "~4.1.0", "semver": "^4.3.6",
"sinon": "~1.12.2", "sinon": "~1.12.2",
"split": "~0.3.2", "split": "~0.3.2",
"through2": "~0.6.3", "through2": "~0.6.3",
@ -84,7 +84,7 @@
"bluebird": "^2.9.14", "bluebird": "^2.9.14",
"chalk": "^1.0.0", "chalk": "^1.0.0",
"forever-agent": "^0.6.0", "forever-agent": "^0.6.0",
"lodash": "^3.9.3", "lodash": "^3.10.0",
"lodash-compat": "^3.0.0" "lodash-compat": "^3.0.0"
}, },
"repository": { "repository": {

56
scripts/Version.js Normal file
View File

@ -0,0 +1,56 @@
var _ = require('lodash');
var package = require('../package.json');
var branches = package.config.supported_es_branches;
var semver = require('semver');
var maxMinorVersion = function (majorV) {
var versions = branches.map(function (v) { return v + '.0'; });
return new Version(semver.maxSatisfying(versions, '^' + majorV));
};
function Version(v) {
this.version = v;
this.major = semver.major(v);
this.minor = semver.minor(v);
this.patch = semver.patch(v);
}
Version.fromBranch = function (branch) {
var m;
// master === the highest version number
if (branch === 'master') return new Version('2.0.0');
// n.m -> n.m.0
if (m = branch.match(/^\d+\.\d+$/)) return new Version(branch + '.0');
// n.x -> n.(maxVersion + 1).0
if (m = branch.match(/^(\d+)\.x$/i)) return maxMinorVersion(m[1]).increment('minor');
throw new Error('unable to convert branch "' + branch + '" to semver');
};
Version.prototype.increment = function (which) {
return new Version(semver.inc(this.version, which));
};
Version.prototype.satisfies = function (range) {
return semver.satisfies(this.version, range);
};
// merge a list of option objects, each of which has a "version" key dictating
// the range of versions those options should be included in. Options are merged
// in the order of the array
Version.prototype.mergeOpts = function (opts, defaults) {
var self = this;
return opts
.filter(function (rule) {
return self.satisfies(rule.version);
})
.reduce(function (overrides, rule) {
return _.merge(overrides, _.omit(rule, 'version'));
}, defaults ? _.clone(defaults) : {});
};
module.exports = Version;

View File

@ -9,9 +9,9 @@ module.exports = function (branch, done) {
var async = require('async'); var async = require('async');
var chalk = require('chalk'); var chalk = require('chalk');
var path = require('path'); var path = require('path');
var semver = require('semver');
var fromRoot = path.join.bind(path, require('find-root')(__dirname)); var fromRoot = path.join.bind(path, require('find-root')(__dirname));
var templates = require('./templates'); var templates = require('./templates');
var Version = require('../Version');
var urlParamRE = /\{(\w+)\}/g; var urlParamRE = /\{(\w+)\}/g;
var files; // populated in readSpecFiles var files; // populated in readSpecFiles
@ -19,34 +19,10 @@ module.exports = function (branch, done) {
var docVars; // slightly modified clone of apiSpec for the docs var docVars; // slightly modified clone of apiSpec for the docs
var branchSuffix = utils.branchSuffix(branch); var branchSuffix = utils.branchSuffix(branch);
var maxMinorVersion = (function () {
var branches = require(fromRoot('package.json')).config.supported_es_branches;
var top = branches.map(function (v) { return v + '.0'; }).sort(semver.compare).pop();
return top.split('.')[1];
}());
var branchAsVersion = (function () {
var m;
// master === the highest version number
if (branch === 'master') return '2.0.0';
// n.m -> n.m.0
if (m = branch.match(/^\d+\.\d+$/)) return branch + '.0';
// n.x -> n.(maxVersion + 1).0
if (m = branch.match(/^(\d+)\.x$/i)) return m[1] + '.' + (+maxMinorVersion + 1) + '.0';
throw new Error('unable to convert branch "' + branch + '" to semver');
}());
var esDir = fromRoot('src/_elasticsearch_' + _.snakeCase(branch)); var esDir = fromRoot('src/_elasticsearch_' + _.snakeCase(branch));
var overrides = require('./overrides') var version = Version.fromBranch(branch);
.filter(function (rule) { var overrides = version.mergeOpts(require('./overrides'), {
return semver.satisfies(branchAsVersion, rule.version);
})
.reduce(function (overrides, rule) {
return _.merge(overrides, _.omit(rule, 'version'));
}, {
aliases: {}, aliases: {},
paramAsBody: {}, paramAsBody: {},
clientActionModifier: false clientActionModifier: false