try/finally with SBT tasks -
i have existing task called mytask
, implementation don't control.
i want redefine in way: mytask := { val x = prepare() try mytask.value cleanup(x) }
as know, code wouldn't work, don't control when mytask.value
executed.
prepare
can called def.sequential()
, , cleanup
andfinally
construct. problem how cleanup
can return value of prepare()
.
def.sequential{ def.task{ prepare() }, mytask }.andfinally(cleanup(???))
one workaround use global variables, dirty hack.
any ideas?
i've tried use global variables, , works ok, though isn't elegant way implement it.
i have:
- project/mytasks.scala
- build.sbt
snippet in mytasks.scala
:
object mytasks { var x = option.empty[string] def prepare(): string = ??? def cleanup(x: string): unit = ??? }
snippet in build.sbt
:
mytask := def.sequential{ def.task{ mytasks.x = some(mytasks.prepare()) }, mytask }.andfinally { mytasks.cleanup(mytasks.x.get) mytasks.x = none }.value
in way, can state prepare
, , bypass sbt limitations.
Comments
Post a Comment