From 5dc17a5bb380f88901ec1d85ef62b77fa6b408f9 Mon Sep 17 00:00:00 2001 From: Alexandre Archambault Date: Thu, 25 Jun 2015 00:18:52 +0100 Subject: [PATCH] Better minification --- .../main/scala/coursier/core/Exclusions.scala | 38 ++++++++++++++++++- .../src/main/scala/coursier/core/Orders.scala | 14 ++----- 2 files changed, 39 insertions(+), 13 deletions(-) diff --git a/core/src/main/scala/coursier/core/Exclusions.scala b/core/src/main/scala/coursier/core/Exclusions.scala index 742961223..997c9bc61 100644 --- a/core/src/main/scala/coursier/core/Exclusions.scala +++ b/core/src/main/scala/coursier/core/Exclusions.scala @@ -8,7 +8,7 @@ object Exclusions { .partition{case (org, name) => org == "*" || name == "*" } val all = wildCards - .contains(("*", "*")) + .contains(one.head) val excludeByOrg = wildCards .collect{case (org, "*") if org != "*" => org } @@ -35,7 +35,7 @@ object Exclusions { val (all, excludeByOrg, excludeByName, remaining) = partition(exclusions) - if (all) Set(("*", "*")) + if (all) one else { val filteredRemaining = remaining .filter{case (org, name) => @@ -49,5 +49,39 @@ object Exclusions { } } + val zero = Set.empty[(String, String)] + val one = Set(("*", "*")) + + def join(x: Set[(String, String)], y: Set[(String, String)]): Set[(String, String)] = + minimize(x ++ y) + + def meet(x: Set[(String, String)], y: Set[(String, String)]): Set[(String, String)] = { + + val ((xAll, xExcludeByOrg, xExcludeByName, xRemaining), (yAll, yExcludeByOrg, yExcludeByName, yRemaining)) = + (partition(x), partition(y)) + + val all = xAll && yAll + + if (all) one + else { + val excludeByOrg = + if (xAll) yExcludeByOrg + else if (yAll) xExcludeByOrg + else xExcludeByOrg intersect yExcludeByOrg + val excludeByName = + if (xAll) yExcludeByName + else if (yAll) xExcludeByName + else xExcludeByName intersect yExcludeByName + + val remaining = + xRemaining.filter{case (org, name) => yAll || yExcludeByOrg(org) || yExcludeByName(name)} ++ + yRemaining.filter{case (org, name) => xAll || xExcludeByOrg(org) || xExcludeByName(name)} ++ + (xRemaining intersect yRemaining) + + excludeByOrg.map((_, "*")) ++ + excludeByName.map(("*", _)) ++ + remaining + } + } } diff --git a/core/src/main/scala/coursier/core/Orders.scala b/core/src/main/scala/coursier/core/Orders.scala index 569596bae..dd5c7a28a 100644 --- a/core/src/main/scala/coursier/core/Orders.scala +++ b/core/src/main/scala/coursier/core/Orders.scala @@ -105,23 +105,15 @@ object Orders { def minDependenciesUnsafe(dependencies: Set[Dependency]): Set[Dependency] = { val groupedDependencies = dependencies .groupBy(dep => (dep.optional, dep.scope)) + .mapValues(deps => deps.head.copy(exclusions = deps.foldLeft(Exclusions.one)((acc, dep) => Exclusions.meet(acc, dep.exclusions)))) .toList - /* - * Iterates over all pairs (xDep, yDep) from `dependencies`. - * If xDep < yDep (all that yDep brings is already brought by xDep), remove yDep. - * - * The (partial) order on dependencies is made of the ones on scope, optional, and exclusions. - */ - val remove = for { - List(((xOpt, xScope), xDeps), ((yOpt, yScope), yDeps)) <- groupedDependencies.combinations(2) + List(((xOpt, xScope), xDep), ((yOpt, yScope), yDep)) <- groupedDependencies.combinations(2) optCmp <- optionalPartialOrder.cmp(xOpt, yOpt).iterator scopeCmp <- mavenScopePartialOrder.cmp(xScope, yScope).iterator if optCmp*scopeCmp >= 0 - xDep <- xDeps.iterator - yDep <- yDeps.iterator exclCmp <- exclusionsPartialOrder.cmp(xDep.exclusions, yDep.exclusions).iterator if optCmp*exclCmp >= 0 if scopeCmp*exclCmp >= 0 @@ -130,7 +122,7 @@ object Orders { if xIsMin || yIsMin // should be always true, unless xDep == yDep, which shouldn't happen } yield if (xIsMin) yDep else xDep - dependencies -- remove + groupedDependencies.map(_._2).toSet -- remove } /**