Introduce the upgrade flag to the constraint solver

and clean up options in general
This commit is contained in:
Slava Kim
2014-05-16 12:01:16 -07:00
parent 0760ffbc36
commit 7065cc50f6
2 changed files with 32 additions and 11 deletions

View File

@@ -102,16 +102,33 @@ ConstraintSolver.PackagesResolver = function (catalog, options) {
});
};
// dependencies - an array of string names of packages (not slices)
// constraints - an array of objects:
// - packageName - string name
// - version - string constraint (ex.: "1.2.3", ">=2.3.4", "=3.3.3")
// options:
// - breaking - set this flag to true if breaking upgrades are allowed
// - upgrade - set this flag to true if upgrading direct dependencies is more
// prioritized than keeping old versions the same
// - previousSolution - mapping from package name to a version that was used in
// the previous constraint solver run
ConstraintSolver.PackagesResolver.prototype.resolve =
function (dependencies, constraints, options) {
var self = this;
check(dependencies, [String]);
check(constraints, [{ packageName: String, version: String, type: String }]);
options = _.defaults(options || {}, {
_testing: false,
breaking: false
breaking: false,
upgrade: false
});
check(dependencies, [String]);
check(constraints, [{ packageName: String, version: String, type: String }]);
check(options, {
_testing: Match.Optional(Boolean),
breaking: Match.Optional(Boolean),
upgrade: Match.Optional(Boolean),
previousSolution: Match.Optional(Object)
});
if (options.previousSolution) {
@@ -266,9 +283,14 @@ ConstraintSolver.PackagesResolver.prototype._getResolverOptions =
var prevSolMapping = {};
_.each(rootDeps, function (dep) { isRootDep[dep] = true; });
_.each(prevSol, function (uv) { prevSolMapping[uv.name] = uv; });
// if the upgrade is preferred over preserving previous solution, pretend
// there are no previous solution
if (! options.upgrade)
_.each(prevSol, function (uv) { prevSolMapping[uv.name] = uv; });
resolverOptions.costFunction = function (state, options) {
options = options || {};
var choices = state.choices;
var constraints = state.constraints;
// very major, major, medium, minor costs
@@ -340,6 +362,7 @@ ConstraintSolver.PackagesResolver.prototype._getResolverOptions =
};
resolverOptions.estimateCostFunction = function (state, options) {
options = options || {};
var dependencies = state.dependencies;
var constraints = state.constraints;

View File

@@ -85,7 +85,6 @@ ConstraintSolver.Resolver.prototype.resolve =
}
}, options);
var rootDependencies = _.clone(dependencies);
// required for error reporting later
var constraintAncestor = {};
_.each(constraints, function (c) {
@@ -112,14 +111,13 @@ ConstraintSolver.Resolver.prototype.resolve =
return startState.choices;
var pq = new PriorityQueue();
var opts = { rootDependencies: rootDependencies };
var costFunction = options.costFunction;
var estimateCostFunction = options.estimateCostFunction;
var combineCostFunction = options.combineCostFunction;
var estimatedStartingCost =
combineCostFunction(costFunction(startState, opts),
estimateCostFunction(startState, opts));
combineCostFunction(costFunction(startState),
estimateCostFunction(startState));
pq.push(startState, [estimatedStartingCost, 0]);
@@ -142,8 +140,8 @@ ConstraintSolver.Resolver.prototype.resolve =
} else {
_.each(neighborsObj.neighbors, function (state) {
var tentativeCost =
combineCostFunction(costFunction(state, opts),
estimateCostFunction(state, opts));
combineCostFunction(costFunction(state),
estimateCostFunction(state));
pq.push(state, [tentativeCost, -state.choices.length]);
});