考虑定义一个自定义任务以从远程存储库下载 .scalafmt.conf
lazy val remoteScalafmtConfig = taskKey[Unit]("Fetch .scalafmt from external repository")
remoteScalafmtConfig := {
import scala.sys.process._
streams.value.log.info("Downloading .scalafmt.conf config from remote repository")
val remoteScalafmtFile = "https://some/external/repo/.scalafmt.conf"
val baseDir = (Compile / baseDirectory).value
url(s"$remoteScalafmtFile") #> (baseDir / ".scalafmt.conf") !
}
然后让compile
任务依赖于 remoteProtoFiles
这样的任务
compile in Compile := (compile in Compile).dependsOn(remoteScalafmtConfig).value
现在执行应该在编译执行之前sbt compile
下载到项目的基本目录中。.scalafmt.conf
我们可以创建一个 sbt 自动插件来分发给每个项目:
package example
import sbt._
import Keys._
object ScalafmtRemoteConfigPlugin extends AutoPlugin {
object autoImport {
lazy val remoteScalafmtConfig = taskKey[Unit]("Fetch .scalafmt from external repository")
}
import autoImport._
override lazy val projectSettings = Seq(
remoteScalafmtConfig := remoteScalafmtConfigImpl.value,
compile in Compile := (compile in Compile).dependsOn(remoteScalafmtConfig).value
)
lazy val remoteScalafmtConfigImpl = Def.task {
import scala.sys.process._
streams.value.log.info("Downloading .scalafmt config from remote repository...")
val remoteScalafmtFile = "https://github.com/guardian/tip/blob/master/.scalafmt.conf"
val baseDir = (Compile / baseDirectory).value
url(s"$remoteScalafmtFile") #> (baseDir / ".scalafmt.conf") !
}
}
现在导入插件project/plugins.sbt
并启用 via将在执行后enablePlugins(ScalafmtRemoteConfigPlugin)
自动下载。.scalafmt
sbt compile