SBT plugin - User defined configuration for Command via their build.sbt

允我心安 提交于 2019-11-29 12:18:02

Take a look at the example here: http://www.scala-sbt.org/release/docs/Extending/Plugins.html#example-plugin

In this example, a task and setting are defined:

val newTask = TaskKey[Unit]("new-task")
val newSetting = SettingKey[String]("new-setting")

val newSettings = Seq(
  newSetting := "test",
  newTask <<= newSetting map { str => println(str) }
)

A user of your plugin could then provide their own value for the newSetting setting in their build.sbt:

newSetting := "light"

EDIT

Here's another example, closer to what you're going for:

Build.scala:

import sbt._                                                
import Keys._                                               

object HelloBuild extends Build {                           

    val newSetting = SettingKey[String]("new-setting", "a new setting!")

    val myTask = TaskKey[State]("my-task")                  

    val mySettings = Seq(                                   
      newSetting := "default",                              
      myTask <<= (state, newSetting) map { (state, newSetting) =>  
        println("newSetting: " + newSetting)                
        state
      }
    )

    lazy val root =
      Project(id = "hello",
              base = file("."),
              settings = Project.defaultSettings ++ mySettings)            
}

With this configuration, you can run my-task at the sbt prompt, and you'll see newSetting: default printed to the console.

You can override this setting in build.sbt:

newSetting := "modified"

Now, when you run my-task at the sbt prompt, you'll see newSetting: modified printed to the console.

EDIT 2

Here's a stand-alone version of the example above: https://earldouglas.com/ext/stackoverflow.com/questions/17038663/

I've accepted @James's answer as it really helped me out. I moved away from using a Commands in favour of a Task (see this mailing list thread). In the end my plugin looked something like this:

package packge.to.my.plugin

import sbt.Keys._
import sbt._

object MyPlugin extends Plugin {

  import MyKeys._

  object MyKeys {
    val myTask = TaskKey[Unit]("runme", "This means you can run 'runme' in the SBT console")
    val newSetting = SettingKey[String]("newSetting")
  }

  override lazy val settings = Seq (
    newSetting := "light",
    myTask <<= (state, newSetting) map myCommand
  )

  def myCommand(state: State, newSetting: String) {
    //This code runs when the user types the "runme" command in the SBT console
    //newSetting is "light" here unless the user overrides in their build.sbt (see below)
    state.log.info(newSetting)
  }
}

To override the newSetting in the build.sbt of a project that uses this plugin:

import packge.to.my.plugin.MyKeys._

newSetting := "Something else"

The missing import statement had me stuck for a while!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!