scalasbtsbt-release

execute a "release" command from inside a task in sbt


I am using "sbt-releases" and need to execute "release" from inside a task, but the problem is that "release" is a command not task.

so is it possible to execute a command from inside a task in sbt ?

Why I need to execute a release from inside a task ?

I want to create a task to execute a release if some condition satisfied, else trigger a normal build. I tried to achieve that by through changing the releaseProcess in sbt but the problem is that it is a SettingKey not a taskKey, and tasks can't be used inside settings because settings gets initialized once in the project loading.


Solution

  • I created a command eventually. Thanks @Seth Tisue

    lazy val myproject = (project in file("myproject").
    settings(
      commands += Command.command("releaseIfRequired") { state =>
        val (stateAfterTask, condition) = Project.extract(state).runTask(conditionTask, state)
    
        if (condition) {
          Parser.parse(" with-defaults", ReleaseKeys.releaseCommand.parser(stateAfterTask)) match {
            case Right(cmd) => cmd()
            case Left(msg) => throw sys.error(s"Error triggering release command:\n$msg")
          }
        }
        else {
          Project.extract(stateAfterTask).runTask(build, stateAfterTask)._1
        }
      }
    )