From c7dfbbf7051e919413b275fa005235df442d0451 Mon Sep 17 00:00:00 2001 From: Dan Sanduleac Date: Fri, 29 Nov 2013 09:37:53 +0000 Subject: [PATCH] Add ArtifactTypeFilter --- .../sbt/librarymanagement/IvyInterface.scala | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/librarymanagement/src/main/scala/sbt/librarymanagement/IvyInterface.scala b/librarymanagement/src/main/scala/sbt/librarymanagement/IvyInterface.scala index b582a51ed..62842755e 100644 --- a/librarymanagement/src/main/scala/sbt/librarymanagement/IvyInterface.scala +++ b/librarymanagement/src/main/scala/sbt/librarymanagement/IvyInterface.scala @@ -8,6 +8,8 @@ import java.net.{ URI, URL } import scala.xml.NodeSeq import org.apache.ivy.plugins.resolver.{ DependencyResolver, IBiblioResolver } import org.apache.ivy.util.url.CredentialsStore +import org.apache.ivy.core.module.descriptor +import org.apache.ivy.util.filter.{Filter => IvyFilter} import sbt.serialization._ /** Additional information about a project module */ @@ -31,6 +33,25 @@ object ExclusionRule { implicit val pickler: Pickler[ExclusionRule] with Unpickler[ExclusionRule] = PicklerUnpickler.generate[ExclusionRule] } +/** Work around the inadequacy of Ivy's ArtifactTypeFilter (that it cannot reverse a filter) + * @param types represents the artifact types that we should try to resolve for (as in the allowed values of + * `artifact[type]` from a dependency `` section). One can use this to filter + * source / doc artifacts. + * @param inverted whether to invert the types filter (i.e. allow only types NOT in the set) */ +case class ArtifactTypeFilter(types: Set[String], inverted: Boolean) { + def invert = copy(inverted = !inverted) + def apply(a: descriptor.Artifact): Boolean = (types contains a.getType) ^ inverted +} + +object ArtifactTypeFilter { + def allow(types: Set[String]) = ArtifactTypeFilter(types, false) + def forbid(types: Set[String]) = ArtifactTypeFilter(types, true) + + implicit def toIvyFilter(f: ArtifactTypeFilter): IvyFilter = new IvyFilter { + override def accept(o: Object): Boolean = Option(o) exists { case a: descriptor.Artifact => f.apply(a) } + } +} + final case class ModuleConfiguration(organization: String, name: String, revision: String, resolver: Resolver) object ModuleConfiguration { def apply(org: String, resolver: Resolver): ModuleConfiguration = apply(org, "*", "*", resolver)