2015-06-15 71 views
5

如何聲明2個自定義設置,例如A和B,在Global配置範圍中定義B,其內容依賴於A,在幾個配置範圍中對A定義不同以這樣的方式,即使B只定義一次,每個配置中B的結果值是不同的?如何依賴於「當前」配置中的設置

請考慮下面的例子targetHost,在remote定義不同於其他的配置,並根據其scriptContent

object MyBuild extends Build { 

    lazy val remote = config("remote") describedAs ("configuration for remote environement ") 

    lazy val targetHost = settingKey[String]("private hostname of master server") 

    lazy val scriptContent = settingKey[String]("Some deployment script") 

    lazy val root: Project = Project("meme", file(".")). 
    settings(
     name := "hello", 

     targetHost := "localhost", 
     targetHost in remote := "snoopy", 

     scriptContent := s""" 
      # some bash deployment here 
      /usr/local/uberDeploy.sh ${targetHost.value} 
     """ 
    )  
} 

我想scriptContent有兩種配置範圍不同的值,但由於它取決於就在Global範圍targetHost其價值始終是相同的:

> scriptContent 
[info] 
[info]    # some bash deployment here 
[info]    /usr/local/uberDeploy.sh localhost 
[info] 
> remote:scriptContent 
[info] 
[info]    # some bash deployment here 
[info]    /usr/local/uberDeploy.sh localhost 
[info] 

而我想獲取以下信息:

> scriptContent 
[info] 
[info]    # some bash deployment here 
[info]    /usr/local/uberDeploy.sh localhost 
[info] 
> remote:scriptContent 
[info] 
[info]    # some bash deployment here 
[info]    /usr/local/uberDeploy.sh snoopy 
[info] 
+0

我猜你需要明確指定任務的範圍。例如,在兩個範圍內實現兩個'scriptContent',一個是'targetHost in remote',另一個是簡單的'targetHost'。 – marios

回答

0

找到了!我的問題實際上是重複的(對不起...),最相關的答案在這裏:How can i make an SBT key see settings for the current configuration?

=>我們需要多次應用設置,每個作用域一次,以強制重新評估的scriptSetting內容:

import sbt._ 
import sbt.Keys._ 

object MyBuild extends Build { 

    lazy val remote = config("remote") describedAs ("configuration for remote environement ") 

    lazy val targetHost = settingKey[String]("private hostname of master server") 

    lazy val scriptContent = settingKey[String]("Some deployment script") 

    lazy val scriptSettings = Seq(
     scriptContent := s""" 
      # some bash deployment here 
      /usr/local/uberDeploy.sh ${targetHost.value} 
     """ 
    ) 

    lazy val root: Project = Project("meme", file(".")) 
    .settings(
     name := "hello",  
     targetHost := "localhost", 
     targetHost in remote := "snoopy" 
     )  
    .settings(scriptSettings: _*) 
    .settings(inConfig(remote)(scriptSettings) :_*) 
} 

產生預期的結果:

> scriptContent 
[info] 
[info]    # some bash deployment here 
[info]    /usr/local/uberDeploy.sh localhost 
[info] 
> remote:scriptContent 
[info] 
[info]    # some bash deployment here 
[info]    /usr/local/uberDeploy.sh snoopy 
[info] 
>