|
| 1 | +package com.sandinh.sdsbt |
| 2 | + |
| 3 | +import sbt.* |
| 4 | +import sbt.Keys.* |
| 5 | +import sbt.Def.Initialize |
| 6 | +import sbt.VirtualAxis.jvm |
| 7 | +import sbt.internal.ProjectMatrix |
| 8 | +import sbtprojectmatrix.ProjectMatrixPlugin |
| 9 | +import scala.language.implicitConversions |
| 10 | + |
| 11 | +object SdMatrixPlugin extends AutoPlugin { |
| 12 | + override def trigger = allRequirements |
| 13 | + override def requires: Plugins = ProjectMatrixPlugin |
| 14 | + |
| 15 | + object autoImport { |
| 16 | + lazy val akkaVersion = settingKey[String]("akkaVersion") |
| 17 | + lazy val playVersion = settingKey[String]("playVersion") |
| 18 | + |
| 19 | + val (akka25, akka26) = (LibAxis("2.5.32"), LibAxis("2.6.16", "")) |
| 20 | + |
| 21 | + val (play26, play27, play28) = |
| 22 | + (LibAxis("2.6.25"), LibAxis("2.7.9"), LibAxis("2.8.8", "")) |
| 23 | + |
| 24 | + // TODO remove `for3Use2_13` when this issue is fixed: https://github.com/akka/akka/issues/30243 |
| 25 | + def akka(modules: NameAnConfig*): Initialize[Seq[ModuleID]] = akkaVersion { |
| 26 | + v => |
| 27 | + modules.map { case NameAnConfig(m, c) => |
| 28 | + "com.typesafe.akka" %% s"akka-$m" % v withConfigurations c cross CrossVersion.for3Use2_13 |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + def play(modules: NameAnConfig*): Initialize[Seq[ModuleID]] = playVersion { |
| 33 | + v => |
| 34 | + modules.map { |
| 35 | + case NameAnConfig("" | "play", c) => |
| 36 | + "com.typesafe.play" %% "play" % v withConfigurations c |
| 37 | + case NameAnConfig(m @ "json", _) => |
| 38 | + sys.error(s"play-$m don't have save version as play") |
| 39 | + case NameAnConfig(m, c) => |
| 40 | + "com.typesafe.play" %% s"play-$m" % v withConfigurations c |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + implicit class ProjectMatrixOps(val p: ProjectMatrix) extends AnyVal { |
| 45 | + def libAxis( |
| 46 | + lib: LibAxis, |
| 47 | + scalaVersions: Seq[String], |
| 48 | + process: Project => Project = identity, |
| 49 | + ): ProjectMatrix = p |
| 50 | + .customRow( |
| 51 | + scalaVersions, |
| 52 | + axisValues = Seq(lib, jvm), |
| 53 | + p => |
| 54 | + process( |
| 55 | + p.settings( |
| 56 | + moduleName := moduleName.value + lib.suffix, |
| 57 | + ) |
| 58 | + ) |
| 59 | + ) |
| 60 | + |
| 61 | + def akkaAxis( |
| 62 | + lib: LibAxis, |
| 63 | + scalaVersions: Seq[String], |
| 64 | + process: Project => Project = identity, |
| 65 | + ): ProjectMatrix = p |
| 66 | + .customRow( |
| 67 | + scalaVersions, |
| 68 | + axisValues = Seq(lib, jvm), |
| 69 | + p => |
| 70 | + process( |
| 71 | + p.settings( |
| 72 | + moduleName := moduleName.value + lib.suffix, |
| 73 | + akkaVersion := lib.version, |
| 74 | + ) |
| 75 | + ) |
| 76 | + ) |
| 77 | + |
| 78 | + def playAxis( |
| 79 | + lib: LibAxis, |
| 80 | + scalaVersions: Seq[String], |
| 81 | + process: Project => Project = identity, |
| 82 | + ): ProjectMatrix = p |
| 83 | + .customRow( |
| 84 | + scalaVersions, |
| 85 | + axisValues = Seq(lib, jvm), |
| 86 | + p => |
| 87 | + process( |
| 88 | + p.settings( |
| 89 | + moduleName := moduleName.value + lib.suffix, |
| 90 | + playVersion := lib.version, |
| 91 | + ) |
| 92 | + ) |
| 93 | + ) |
| 94 | + } |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +private case class NameAnConfig(name: String, configuration: Option[String]) |
| 99 | +private object NameAnConfig { |
| 100 | + implicit def from(name: String): NameAnConfig = NameAnConfig(name, None) |
| 101 | + |
| 102 | + implicit def from(nameAndConfig: (String, String)): NameAnConfig = |
| 103 | + NameAnConfig(nameAndConfig._1, Some(nameAndConfig._2)) |
| 104 | + |
| 105 | + implicit def fromConfig( |
| 106 | + nameAndConfig: (String, Configuration) |
| 107 | + ): NameAnConfig = |
| 108 | + NameAnConfig(nameAndConfig._1, Some(nameAndConfig._2.name)) |
| 109 | +} |
0 commit comments