Skip to content

Instantly share code, notes, and snippets.

@andiogenes
Created December 12, 2025 11:59
Show Gist options
  • Select an option

  • Save andiogenes/99bafc13b19907a8dac32bceace11b36 to your computer and use it in GitHub Desktop.

Select an option

Save andiogenes/99bafc13b19907a8dac32bceace11b36 to your computer and use it in GitHub Desktop.
import sbt.*
/** Creates [[Def.Setting]] from [[SettingKey]] manipulating with [[State]], by assigning an action
* that will perform previous action assigned to this key, and then evaluate parameter `hook`,
* which may access and modify setting's state.
*
* @example
* {{{
* lazy val someProject = (project in file("some"))
* .settings(
* addHook(Global / onLoad) { s =>
* println(s"Loaded! Current state: $s")
* s
* }
* )
* }}}
*/
def addHook(settingKey: SettingKey[State => State])(hook: State => State) =
settingKey := settingKey.value.andThen(hook)
/** Version of [[addHook]] for `hooks` that only perform side effect.
*
* @example
* {{{
* lazy val someProject = (project in file("some"))
* .settings(
* addIdempotentHook(Global / onLoad) {
* println("Loaded! No access to the state.")
* }
* )
* }}}
*/
def addIdempotentHook(settingKey: SettingKey[State => State])(hook: => Unit) =
addHook(settingKey)(s => { hook; s })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment