如何在构建时获取 SBT 暂存目录? [英] How do I get SBT staging directory at build time?

查看:43
本文介绍了如何在构建时获取 SBT 暂存目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do I get SBT staging directory at build time?

我想对远程存储库进行棘手的克隆,SBT 的 stagingDirectory 似乎很合适.

I want to do a tricky clone of a remote repo, and the stagingDirectory of SBT seems to be a nice fit.

如何获取Build.scala"中的目录?

How do I get the directory inside "Build.scala" ?

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

========

与问题不直接相关的潜在问题.我想在 SBT 中使用 git 依赖项的子目录.SBT 没有开箱即用,所以我写了一个简单的包装器:

Underlying problem NOT directly relevant to the question. I wanted to use a subdirectory of a git dependency in SBT. SBT doesn't provide this out of the box so I wrote a simple wrapper:

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
  }
}

用法:

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

推荐答案

从你的链接看API,有两种方法你可以使用getGlobalBasegetStagingDirectory,都采取状态.

Looking at the API from your link, there are two methods you can use getGlobalBase and getStagingDirectory, both take state.

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)))

    }
  )

}

编辑

在您最后的评论之后,我认为您正在寻找 自定义解析器.自定义解析器可以访问 ResolveInfo 对象,它有一个名为 staging 的属性.

Edit

After your last comment, I think you're looking for a custom resolver. The custom resolver has an access to a ResolveInfo object, which has a property called staging.

例如,您可以通过以下方式实现您的目标(实际上无需直接访问 staging):

For example this is how you could achieve what you're looking for (actually without accessing staging directly):

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.

The idea is that we delegate to the predefined git resolver, and we let it do it's work, after it's done, we'll return a subdirectory: fn() / directory.

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

This is an example and of course you could stick to your logic of getting the repository. The staging directory will be available to you in the resolver method.

这篇关于如何在构建时获取 SBT 暂存目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆