Last active
August 31, 2025 07:43
-
-
Save xuwei-k/a24bcded6fa4484ba6004b839787bf20 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sbt.* | |
import sbt.Keys.* | |
import sjsonnew.BasicJsonProtocol | |
import sjsonnew.BasicJsonProtocol.* | |
import sjsonnew.Builder | |
import sjsonnew.JsonFormat | |
import sjsonnew.JsonWriter | |
import sjsonnew.support.scalajson.unsafe.CompactPrinter | |
object ExternalDependencyModuleIds extends AutoPlugin { | |
override def trigger = allRequirements | |
private final case class Lib(groupId: String, artifactId: String, version: String) | |
private final object Lib { | |
implicit val instance: JsonFormat[Lib] = | |
BasicJsonProtocol.caseClass3(apply, unapply)("groupId", "artifactId", "version") | |
implicit val ordering: Ordering[Lib] = Ordering.by(unapply) | |
} | |
override lazy val projectSettings: Seq[Def.Setting[?]] = | |
Def.settings( | |
TaskKey[Seq[ModuleID]]("externalDependencyModuleIds") := { | |
val result = (Compile / externalDependencyClasspath).value.flatMap(_.metadata.get(moduleID.key)) | |
val libs = result.map { x => | |
val suffix = x.crossVersion match { | |
case _: CrossVersion.Binary => | |
s"_${scalaBinaryVersion.value}" | |
case _ => | |
// TODO | |
"" | |
} | |
Lib( | |
groupId = x.organization, | |
artifactId = s"${x.name}$suffix", | |
version = x.revision | |
) | |
}.sorted.map(x => s" ${x.toJsonString}") | |
IO.write( | |
file(s"${name.value}.json"), | |
libs.mkString("{\n", ",\n", "\n}\n") | |
) | |
result | |
} | |
) | |
private implicit class JsonClassOps[A](private val self: A) extends AnyVal { | |
def toJsonString(implicit format: JsonWriter[A]): String = { | |
val builder = new Builder(sjsonnew.support.scalajson.unsafe.Converter.facade) | |
format.write(self, builder) | |
CompactPrinter( | |
builder.result.getOrElse(sys.error("invalid json")) | |
) | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sbt.* | |
import sbt.Keys.* | |
object ExternalDependencyModuleIds extends AutoPlugin { | |
override def trigger = allRequirements | |
private final case class Lib(groupId: String, artifactId: String, version: String) { | |
def asString: String = s"$groupId:$artifactId:$version" | |
} | |
private final object Lib { | |
implicit val ordering: Ordering[Lib] = Ordering.by(unapply) | |
} | |
override lazy val projectSettings: Seq[Def.Setting[?]] = | |
Def.settings( | |
TaskKey[Seq[ModuleID]]("externalDependencyModuleIds") := { | |
val result = (Compile / externalDependencyClasspath).value.flatMap(_.metadata.get(moduleID.key)) | |
val libs = result.map { x => | |
val suffix = x.crossVersion match { | |
case _: CrossVersion.Binary => | |
s"_${scalaBinaryVersion.value}" | |
case c if c == CrossVersion.disabled => | |
"" | |
case _ => | |
sys.error(s"not supported cross version $x") | |
} | |
Lib( | |
groupId = x.organization, | |
artifactId = s"${x.name}$suffix", | |
version = x.revision | |
) | |
}.sorted.map(_.asString) | |
IO.write( | |
file(s"${name.value}.txt"), | |
libs.mkString("", "\n", "\n") | |
) | |
result | |
} | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment