如何在构建时获取 SBT 暂存目录?

2024-01-03

如何在构建时获取 SBT 暂存目录?

我想做一个远程存储库的棘手克隆,并且stagingDirectorySBT 似乎很合适。

如何获取“Build.scala”内的目录?

SBT源代码:http://www.scala-sbt.org/0.13.1/sxr/sbt/BuildPaths.scala.html#sbt.BuildPaths.stagingDirectory http://www.scala-sbt.org/0.13.1/sxr/sbt/BuildPaths.scala.html#sbt.BuildPaths.stagingDirectory

=======

根本问题与问题不直接相关。我想在 SBT 中使用 git 依赖项的子目录。 SBT 不提供开箱即用的功能,因此我编写了一个简单的包装器:

object Git {

  def clone(cloneFrom: String, branch: String, subdirectory: String) = {
    val uniqueHash = Hash.halfHashString(cloneFrom + branch)
    val cloneTo = file(sys.props("user.home")) / ".sbt" / "staging" / uniqueHash

    val clonedDir = creates(cloneTo) {
      Resolvers.run("git", "clone", cloneFrom, cloneTo.absolutePath)
      Resolvers.run(Some(cloneTo), "git", "checkout", "-q", branch)
    }

    clonedDir / subdirectory
  }
}

usage:

lazy val myDependency = Git.clone(cloneFrom = "git://...someproject.git", branch = "v2.4", subdirectory = "someModule")


从您的链接查看 API,您可以使用两种方法getGlobalBase and getStagingDirectory,两者都采用状态。

import sbt._
import Keys._
import sbt.BuildPaths._

object MyBuild extends Build {

  val outputStaging = taskKey[Unit]("Outputs staging")

  lazy val root = project.in(file(".")).settings(
    outputStaging := {
      val s = state.value
      println(getStagingDirectory(s, getGlobalBase(s)))

    }
  )

}

Edit

在您上次发表评论后,我认为您正在寻找自定义解析器 http://www.scala-sbt.org/0.13.2/docs/Extending/Build-Loaders.html#custom-resolver。自定义解析器可以访问解决信息 http://www.scala-sbt.org/0.13.2/api/index.html#sbt.BuildLoader%24%24ResolveInfo对象,它有一个属性称为staging.

例如,这就是您如何实现您正在寻找的内容(实际上无需访问staging直接地):

object MyBuild extends Build {

  lazy val root = project.in(file(".")).dependsOn(RootProject(uri("dir+git://github.com/lpiepiora/test-repo.git#branch=master&dir=subdir")))

  override def buildLoaders = BuildLoader.resolve(myCustomGitResolver) +: super.buildLoaders

  def myCustomGitResolver(info: BuildLoader.ResolveInfo): Option[() => File] =
    if(info.uri.getScheme != "dir+git") None
    else {
      import RichURI.fromURI
      val uri = info.uri
      val (branch, directory) = parseOutBranchNameAndDir(uri.getFragment)
      val gitResolveInfo = new BuildLoader.ResolveInfo(
        uri.copy(scheme = "git", fragment = branch), info.staging, info.config, info.state
      )
      println(uri.copy(scheme = "git", fragment = branch))
      Resolvers.git(gitResolveInfo).map(fn => () => fn() / directory)
    }

  // just an ugly way to get the branch and the folder
  // you may want something more sophisticated
  private def parseOutBranchNameAndDir(fragment: String): (String, String) = {
    val Array(branch, dir) = fragment.split('&')
    (branch.split('=')(1), dir.split('=')(1))
  }

}

我们的想法是,我们委托给预定义的 git 解析器,然后让它完成工作,完成后,我们将返回一个子目录:fn() / directory.

这是一个示例,当然您可以坚持获取存储库的逻辑。这暂存目录将在解析器方法中提供给您。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在构建时获取 SBT 暂存目录? 的相关文章

随机推荐