Cleanup ConfigIndex

This commit is contained in:
Dale Wijnand
2018-12-22 17:05:51 +00:00
parent 3d924978c2
commit 30dca6b818
+16 -24
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)
} }
@@ -131,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(
@@ -155,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
} }
@@ -179,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],