Skip to content

Instantly share code, notes, and snippets.

@xuwei-k
Created November 18, 2024 01:37
Show Gist options
  • Save xuwei-k/7cb3f732054ea914c50cdde44d3baae2 to your computer and use it in GitHub Desktop.
Save xuwei-k/7cb3f732054ea914c50cdde44d3baae2 to your computer and use it in GitHub Desktop.
import sbt.*
import sbt.Keys.*
object CountImplicitKeywordPlugin extends AutoPlugin {
override def trigger: PluginTrigger = allRequirements
object autoImport {
val implicitKeyword = taskKey[Int]("")
val allImplicitKeyword = taskKey[Seq[(String, Int)]]("")
}
import autoImport.*
override def globalSettings: Seq[Def.Setting[?]] = Def.settings(
allImplicitKeyword := Def.taskDyn {
val s = state.value
val extracted = Project.extract(s)
val currentBuildUri = extracted.currentRef.build
val projects = extracted.structure.units
.apply(currentBuildUri)
.defined
.values
.toList
Def.task {
val x1: Seq[(String, Int)] =
projects.map(_.id).zip(projects.map(p => LocalProject(p.id) / Compile / implicitKeyword).join.value)
val x2: Seq[(String, Int)] =
projects.map(_.id).zip(projects.map(p => LocalProject(p.id) / Test / implicitKeyword).join.value)
val result = x1.map(x => x.copy(_1 = x._1 + "-main")) ++ x2.map(x => x.copy(_1 = x._1 + "-test"))
val res = result.sortBy(_._2).filter(_._2 > 0)
res.foreach(println)
println("all = " + res.map(_._2).sum)
res
}
}.value
)
override def projectSettings: Seq[Def.Setting[?]] = Def.settings(
Seq(Compile, Test).map { x =>
(x / implicitKeyword) := {
import scala.meta.*
(x / unmanagedSources).value.map { file =>
val input = Input.File(file)
val tree = implicitly[parsers.Parse[Source]].apply(input, dialects.Scala3).get
tree.tokens
.collect {
case c: Token.KwImplicit => (c.pos.start, c.pos.end)
}.distinct.size
}.sum
}
}
)
}
libraryDependencies += "org.scalameta" %% "scalameta" % "4.11.1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment