Merge pull request #4500 from dwijnand/cleanup-ConfigIndex

Cleanup ConfigIndex
This commit is contained in:
eugene yokota 2018-12-22 14:25:12 -05:00 committed by GitHub
commit 96cd676cd9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 53 deletions

View File

@ -210,9 +210,7 @@ object Act {
| ((ZeroString ^^^ ParsedZero) <~ sep) | ((ZeroString ^^^ ParsedZero) <~ sep)
| ((ZeroIdent ^^^ ParsedZero) <~ sep) | ((ZeroIdent ^^^ ParsedZero) <~ sep)
| (value(examples(ID, confs, "configuration")) <~ oldSep) | (value(examples(ID, confs, "configuration")) <~ oldSep)
| (value(examples(CapitalizedID, idents, "configuration ident") map { | (value(examples(CapitalizedID, idents, "configuration ident") map fromIdent) <~ sep)
fromIdent(_)
}) <~ sep)
) ?? Omitted ) ?? Omitted
} }

View File

@ -41,11 +41,8 @@ object KeyIndex {
} yield { } yield {
val data = ids map { id => val data = ids map { id =>
val configs = configurations.getOrElse(id, Seq()) val configs = configurations.getOrElse(id, Seq())
val namedConfigs = configs.map { config => val configIdentToName = configs.map(config => config.id -> config.name).toMap
(config.name, ConfigData(Some(config.id), emptyAKeyIndex)) Option(id) -> new ConfigIndex(Map.empty, configIdentToName, emptyAKeyIndex)
}.toMap
val inverse = namedConfigs.map((ConfigIndex.invert _).tupled)
Option(id) -> new ConfigIndex(namedConfigs, inverse, emptyAKeyIndex)
} }
Option(uri) -> new ProjectIndex(data.toMap) Option(uri) -> new ProjectIndex(data.toMap)
} }
@ -59,13 +56,10 @@ object KeyIndex {
def configs(proj: Option[ResolvedReference]) = concat(_.configs(proj)) def configs(proj: Option[ResolvedReference]) = concat(_.configs(proj))
private[sbt] def configIdents(proj: Option[ResolvedReference]) = concat(_.configIdents(proj)) private[sbt] def configIdents(proj: Option[ResolvedReference]) = concat(_.configIdents(proj))
private[sbt] def fromConfigIdent(proj: Option[ResolvedReference])(configIdent: String): String = private[sbt] def fromConfigIdent(proj: Option[ResolvedReference])(configIdent: String): String =
(indices find { idx => indices.find(idx => idx.exists(proj)) match {
idx.exists(proj)
}) match {
case Some(idx) => idx.fromConfigIdent(proj)(configIdent) case Some(idx) => idx.fromConfigIdent(proj)(configIdent)
case _ => Scope.unguessConfigIdent(configIdent) case _ => Scope.unguessConfigIdent(configIdent)
} }
private[sbt] def guessedConfigIdents = concat(_.guessedConfigIdents)
def tasks(proj: Option[ResolvedReference], conf: Option[String]) = concat(_.tasks(proj, conf)) def tasks(proj: Option[ResolvedReference], conf: Option[String]) = concat(_.tasks(proj, conf))
def tasks(proj: Option[ResolvedReference], conf: Option[String], key: String) = def tasks(proj: Option[ResolvedReference], conf: Option[String], key: String) =
concat(_.tasks(proj, conf, key)) concat(_.tasks(proj, conf, key))
@ -114,7 +108,6 @@ trait KeyIndex {
): Set[String] ): Set[String]
private[sbt] def configIdents(project: Option[ResolvedReference]): Set[String] private[sbt] def configIdents(project: Option[ResolvedReference]): Set[String]
private[sbt] def fromConfigIdent(proj: Option[ResolvedReference])(configIdent: String): String private[sbt] def fromConfigIdent(proj: Option[ResolvedReference])(configIdent: String): String
private[sbt] def guessedConfigIdents: Set[(Option[ProjectReference], String, String)]
} }
trait ExtendableKeyIndex extends KeyIndex { trait ExtendableKeyIndex extends KeyIndex {
def add(scoped: ScopedKey[_]): ExtendableKeyIndex def add(scoped: ScopedKey[_]): ExtendableKeyIndex
@ -135,12 +128,13 @@ private[sbt] case class IdentifiableConfig(name: String, ident: Option[String])
private[sbt] case class ConfigData(ident: Option[String], keys: AKeyIndex) private[sbt] case class ConfigData(ident: Option[String], keys: AKeyIndex)
/* /*
* data contains the mapping between a configuration name and its ident and keys. * data contains the mapping between a configuration name and its keys.
* configIdentToName contains the mapping between a configuration ident and its name
* noConfigKeys contains the keys without a configuration. * noConfigKeys contains the keys without a configuration.
*/ */
private[sbt] final class ConfigIndex( private[sbt] final class ConfigIndex(
val data: Map[String, ConfigData], val data: Map[String, AKeyIndex],
val inverse: Map[String, String], val configIdentToName: Map[String, String],
val noConfigKeys: AKeyIndex val noConfigKeys: AKeyIndex
) { ) {
def add( def add(
@ -159,22 +153,21 @@ private[sbt] final class ConfigIndex(
task: Option[AttributeKey[_]], task: Option[AttributeKey[_]],
key: AttributeKey[_] key: AttributeKey[_]
): ConfigIndex = { ): ConfigIndex = {
val oldConfigData = data.getOrElse(config.name, ConfigData(None, emptyAKeyIndex)) val keyIndex = data.getOrElse(config.name, emptyAKeyIndex)
val newConfigData = ConfigData( val configIdent = config.ident.getOrElse(Scope.guessConfigIdent(config.name))
ident = oldConfigData.ident.orElse(config.ident), new ConfigIndex(
keys = oldConfigData.keys.add(task, key) data.updated(config.name, keyIndex.add(task, key)),
configIdentToName.updated(configIdent, config.name),
noConfigKeys
) )
val newData = data.updated(config.name, newConfigData)
val newInverse = (inverse.updated _).tupled(ConfigIndex.invert(config.name, newConfigData))
new ConfigIndex(newData, newInverse, noConfigKeys)
} }
def addKeyWithoutConfig(task: Option[AttributeKey[_]], key: AttributeKey[_]): ConfigIndex = { def addKeyWithoutConfig(task: Option[AttributeKey[_]], key: AttributeKey[_]): ConfigIndex = {
new ConfigIndex(data, inverse, noConfigKeys.add(task, key)) new ConfigIndex(data, configIdentToName, noConfigKeys.add(task, key))
} }
def keyIndex(conf: Option[String]): AKeyIndex = conf match { def keyIndex(conf: Option[String]): AKeyIndex = conf match {
case Some(c) => data.get(c).map(_.keys).getOrElse(emptyAKeyIndex) case Some(c) => data.get(c).getOrElse(emptyAKeyIndex)
case None => noConfigKeys case None => noConfigKeys
} }
@ -183,14 +176,9 @@ private[sbt] final class ConfigIndex(
// guess Configuration name from an identifier. // guess Configuration name from an identifier.
// There's a guessing involved because we could have scoped key that Project is not aware of. // There's a guessing involved because we could have scoped key that Project is not aware of.
private[sbt] def fromConfigIdent(ident: String): String = private[sbt] def fromConfigIdent(ident: String): String =
inverse.getOrElse(ident, Scope.unguessConfigIdent(ident)) configIdentToName.getOrElse(ident, Scope.unguessConfigIdent(ident))
}
private[sbt] object ConfigIndex {
def invert(name: String, data: ConfigData): (String, String) = data match {
case ConfigData(Some(ident), _) => ident -> name
case ConfigData(None, _) => Scope.guessConfigIdent(name) -> name
}
} }
private[sbt] object ConfigIndex
private[sbt] final class ProjectIndex(val data: Map[Option[String], ConfigIndex]) { private[sbt] final class ProjectIndex(val data: Map[Option[String], ConfigIndex]) {
def add( def add(
id: Option[String], id: Option[String],
@ -229,25 +217,6 @@ private[sbt] final class KeyIndex0(val data: BuildIndex) extends ExtendableKeyIn
private[sbt] def fromConfigIdent(proj: Option[ResolvedReference])(configIdent: String): String = private[sbt] def fromConfigIdent(proj: Option[ResolvedReference])(configIdent: String): String =
confIndex(proj).fromConfigIdent(configIdent) confIndex(proj).fromConfigIdent(configIdent)
private[sbt] def guessedConfigIdents: Set[(Option[ProjectReference], String, String)] = {
val guesses = for {
(build, projIndex) <- data.data
(project, confIndex) <- projIndex.data
(config, data) <- confIndex.data
if data.ident.isEmpty && !Scope.configIdents.contains(config)
} yield (projRef(build, project), config, Scope.guessConfigIdent(config))
guesses.toSet
}
private def projRef(build: Option[URI], project: Option[String]): Option[ProjectReference] = {
(build, project) match {
case (Some(uri), Some(proj)) => Some(ProjectRef(uri, proj))
case (Some(uri), None) => Some(RootProject(uri))
case (None, Some(proj)) => Some(LocalProject(proj))
case (None, None) => None
}
}
def tasks(proj: Option[ResolvedReference], conf: Option[String]): Set[AttributeKey[_]] = def tasks(proj: Option[ResolvedReference], conf: Option[String]): Set[AttributeKey[_]] =
keyIndex(proj, conf).tasks keyIndex(proj, conf).tasks
def tasks( def tasks(

View File

@ -7,6 +7,7 @@
import java.net.URI import java.net.URI
import org.scalatest.matchers.MatchResult
import org.scalatest.prop.PropertyChecks import org.scalatest.prop.PropertyChecks
import org.scalatest.{ Matchers, PropSpec } import org.scalatest.{ Matchers, PropSpec }
import sbt.Def._ import sbt.Def._
@ -71,7 +72,14 @@ class ParserSpec extends PropSpec with PropertyChecks with Matchers {
val structure = TestBuild.structure(env, settings, build.allProjects.head._1) val structure = TestBuild.structure(env, settings, build.allProjects.head._1)
val string = displayMasked(scopedKey, ScopeMask()) val string = displayMasked(scopedKey, ScopeMask())
val parser = makeParser(structure) val parser = makeParser(structure)
val result = DefaultParsers.result(parser, string).left.map(_().toString) string should { left =>
result shouldBe Right(scopedKey) val result = DefaultParsers.result(parser, left)
val resultStr = result.fold(_ => "<parse error>", _.toString)
MatchResult(
result == Right(scopedKey),
s"$left parsed back to $resultStr rather than $scopedKey",
s"$left parsed back to $scopedKey",
)
}
} }
} }