load/unload hooks

This commit is contained in:
Mark Harrah 2011-04-26 21:19:56 -04:00
parent 9dec02ee51
commit 515386d973
2 changed files with 12 additions and 3 deletions

View File

@ -39,6 +39,8 @@ object Keys
val configuration = SettingKey[Configuration]("configuration", "Provides the current configuration of the referencing scope.")
val commands = SettingKey[Seq[Command]]("commands", "Defines commands to be registered when this project or build is the current selected one.")
val initialize = SettingKey[Unit]("initialize", "A convenience setting for performing side-effects during initialization.")
val onLoad = SettingKey[State => State]("on-load", "Transformation to apply to the build state when the build is loaded.")
val onUnload = SettingKey[State => State]("on-unload", "Transformation to apply to the build state when the build is unloaded.")
// Command keys
val logged = AttributeKey[Logger]("log", "Provides a Logger for commands.")

View File

@ -124,10 +124,17 @@ object Project extends Init[Scope] with ProjectExtra
def setProject(session: SessionSettings, structure: BuildStructure, s: State): State =
{
val newAttrs = s.attributes.put(stateBuildStructure, structure).put(sessionSettings, session)
val newState = s.copy(attributes = newAttrs)
updateCurrent(newState.runExitHooks())
val previousOnUnload = orIdentity(s get Keys.onUnload.key)
val unloaded = previousOnUnload(s.runExitHooks())
val (onLoad, onUnload) = getHooks(structure.data)
val newAttrs = unloaded.attributes.put(stateBuildStructure, structure).put(sessionSettings, session).put(Keys.onUnload.key, onUnload)
val newState = unloaded.copy(attributes = newAttrs)
onLoad(updateCurrent( newState ))
}
def orIdentity[T](opt: Option[T => T]): T => T = opt getOrElse idFun
def getHook[T](key: ScopedSetting[T => T], data: Settings[Scope]): T => T = orIdentity(key in GlobalScope get data)
def getHooks(data: Settings[Scope]): (State => State, State => State) = (getHook(Keys.onLoad, data), getHook(Keys.onUnload, data))
def current(state: State): ProjectRef = session(state).current
def updateCurrent(s0: State): State =
{