2

如何访问所有 sbt Setting[T] 的映射或任何包含所有设置依赖项的容器?

我想将它们全部以及它们的依赖项打印出来。输出应如下所示:

setting(ScopedKey(Scope(Select(ThisBuild),Global,Global,Global),managed-directory))
    ScopedKey(Scope(Select(ThisBuild),Global,Global,Global),base-directory)
        ScopedKey(Scope(Select(ThisBuild),Global,Global,Global),this-project)
...

下面是我用来执行此操作的 SBT 构建定义。

this-project并非在所有设置中都找到该密钥。它存储在哪里?

import sbt._
import sbt.Logger
import Keys._
import PlayProject._

object TheBuild extends Build {

  lazy val howItWorksProject = Project(
    id = "howItWorks",
    base = file("./modules/howItWorks")
  )

  val projDef: ProjectDefinition[_] = howItWorksProject
  var count  = 0

  def printSettingsDependencies(setting: Setting[_], settings: Seq[Setting[_]], indent: String): Unit = {
    println("debug printSettingsDependencies start" + (count+=1))
    println(setting)
    println("debug setting =" + setting)
    println("debug setting.dependencies = " + setting.dependencies)

    setting.dependencies.foreach {
      n =>
        println("debug it = " + n)
        println("debug finding ...")
        val settingOption = settings.find {
          it =>
            println("\t"+it.key.key.label + "?=" + n.key.label)
            it.key.key.label == n.key.label
        }
        println("debug found = "+settingOption)
        println(indent + settingOption)
        printSettingsDependencies(settingOption.get, settings, indent + indent)
    }
  }
}

inspect this-project输出:

> inspect this-project
[info] Setting: sbt.ResolvedProject = Project(id: howItWorks, base: C:\Users\pawel\workspace\kody\ria-template\modules\howItWorks, aggregate: List(), dependencies: List(), delegates: List(), configurations: List(compile, runtime, test, provided, optional))
[info] Description:
[info]  Provides the current project for the referencing scope.
[info] Provided by:
[info]  {file:/C:/Users/pawel/workspace/kody/ria-template/}howItWorks/*:this-project
[info] Reverse dependencies:
[info]  *:ivy-configurations
[info]  *:base-directory
[info]  *:name
[info] Delegates:
[info]  *:this-project
[info]  {.}/*:this-project
[info]  */*:this-project

inspect base-directory输出:

> inspect base-directory
[info] Setting: java.io.File = C:\Users\pawel\workspace\kody\ria-template\modules\howItWorks
[info] Description:
[info]  The base directory.  Depending on the scope, this is the base directory for the build, project, configuration, or task.
[info] Provided by:
[info]  {file:/C:/Users/pawel/workspace/kody/ria-template/}howItWorks/*:base-directory
[info] Dependencies:
[info]  *:this-project
[info] Reverse dependencies:
[info]  *:ivy-paths
[info]  *:target
[info]  *:unmanaged-base
[info]  *:runner
[info]  *:source-directory
[info] Delegates:
[info]  *:base-directory
[info]  {.}/*:base-directory
[info]  */*:base-directory
[info] Related:
[info]  {.}/*:base-directory

show sbt-version

> show sbt-version
[info] 0.11.3
4

1 回答 1

2

我认为这是你需要的:

import sbt._
import Keys._

object TheBuild extends Build {

    lazy val allSettings = TaskKey[Seq[sbt.Project.Setting[_]]]("all-settings", "")

    lazy val printIt = TaskKey[Unit]("print-it", "")

    lazy val testSettings = Defaults.defaultSettings ++ Seq(
        allSettings <<= (state) map { s =>
            val extracted = Project.extract(s)
            import extracted._
            structure.settings
        },

        printIt <<= (allSettings) map { all =>
            // there are more than one base-directories with different scopes, not all of them have the dependencies
            val baseDirectories = all.filter(s => s.key.key.label == "base-directory" && !s.dependencies.isEmpty)
            // pick the first one to demonstrate this works
            printSettingsDependencies(baseDirectories(0), all, "\t")
        })

    def printSettingsDependencies(setting: Setting[_], settings: Seq[Setting[_]], indent: String): Unit = {
        println(indent + setting)

        setting.dependencies.foreach { n =>
            val settingOption = settings.find { _.key.key.label == n.key.label }
            if (settingOption.isDefined) {
                printSettingsDependencies(settingOption.get, settings, indent + indent)
            }
        }
    }

    lazy val howItWorksProject: Project = Project("howItWorks", file(".")) settings (testSettings: _*)
}

输出:

> print-it
    setting(ScopedKey(Scope(Select(ProjectRef(file:/Users/balagez/Sites/test/,howItWorks)),Global,Global,Global),base-directory)) at NoPosition
        setting(ScopedKey(Scope(Select(ProjectRef(file:/Users/balagez/Sites/test/,howItWorks)),Global,Global,Global),this-project)) at NoPosition
[success] Total time: 0 s, completed Oct 12, 2012 7:50:29 PM
于 2012-10-12T17:54:46.953 回答