Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
668 views
in Technique[技术] by (71.8m points)

scala - Write concise sbt configurations based on combinations of predefined configuration parameters

I'm attempting to remove as much unnecessary strings from my build.sbt as possible

sbt offers way to use Provide, Test, etc. as configuration parameters, like so:

"org.apache.spark" %% "spark-core" % "3.0.0" % Provided,

or

"org.mockito" %% "mockito-scala" % "1.16.3" % Test,

But how can one rewite more complex configurations, like:

"org.scalatest" %% "scalatest" % "3.2.3" % "test,it",

or

.dependsOn(util % "compile->compile;test->test")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

sbt is (bastardized) Scala code, so you can always write some functions or extension methods like:

def configs(head: Configuration, tail: Configuration*) =
  (head :: tail.toList).mkString(",")


implicit final class DependsOnProject(project: Project) {

  private val testConfigurations = Set("test", "fun", "it")
  private def findCompileAndTestConfigs(p: Project) =
      (p.configurations.map(_.name).toSet intersect testConfigurations) + "compile"

  private val thisProjectsConfigs = findCompileAndTestConfigs(project)
  private def generateDepsForProject(p: Project) =
      p % (thisProjectsConfigs intersect findCompileAndTestConfigs(p) map (c => s"$c->$c") mkString ";")

  def compileAndTestDependsOn(projects: Project*): Project =
    project dependsOn (projects.map(generateDepsForProject): _*)
}
"org.scalatest" %% "scalatest" % "3.2.3" % configs(Test, It)

projectA.compileAndTestDependsOn(util)

It's just an example, you can write your own utilities that suits your needs. You can also just assign these Strings to vals and use named values instead.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...