diff --git a/main/Keys.scala b/main/Keys.scala index 6c31c3ec3..6837788cf 100644 --- a/main/Keys.scala +++ b/main/Keys.scala @@ -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.") diff --git a/main/Project.scala b/main/Project.scala index 559cbdfb2..de86988c4 100644 --- a/main/Project.scala +++ b/main/Project.scala @@ -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 = {