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

前端 未结 2 1322
栀梦
栀梦 2020-12-20 00:09

I\'m writing an SBT Plugin that adds a Command and would like users to be able to configure this Command by setting variables in their build.sbt. What i

相关标签:
2条回答
  • 2020-12-20 00:43

    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/

    0 讨论(0)
  • 2020-12-20 00:52

    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!

    0 讨论(0)
提交回复
热议问题