Skip to content

Instantly share code, notes, and snippets.

@datYori
Created August 24, 2023 13:23
Show Gist options
  • Save datYori/4ddc05d8d5b42829af1111da12f79ebc to your computer and use it in GitHub Desktop.
Save datYori/4ddc05d8d5b42829af1111da12f79ebc to your computer and use it in GitHub Desktop.
find unused Dependencies.scala val in build.sbt
import scala.io.Source
import scala.util.matching.Regex
val findUnusedDeps = taskKey[Unit]("Finds unused dependencies from Dependencies.scala")
findUnusedDeps := {
// 1. Parse Dependencies.scala
val depsFile = (baseDirectory.value / "project" / "Dependencies.scala").getAbsolutePath
val depsContent = Source.fromFile(depsFile).getLines.mkString("\n")
val valuesRegex: Regex = """val (\w+) =""".r
val definedValues = valuesRegex.findAllMatchIn(depsContent).map(_.group(1)).toList
// 2. Convert the content of build.sbt into a string format
val sbtContent = IO.read(baseDirectory.value / "build.sbt")
// 3. Find values that are used only once in Dependencies.scala (i.e., at their declaration)
val internallyUsedValues = definedValues.filter(value => depsContent.split(value).length > 2)
// 4. Find unused values
val unusedValues = definedValues.filterNot(value => sbtContent.contains(value) || internallyUsedValues.contains(value))
if (unusedValues.isEmpty) {
println("All dependencies from Dependencies.scala are used in build.sbt.")
} else {
println("Values from Dependencies.scala not used in build.sbt:")
unusedValues.foreach(println)
}
}
// Rest of your build.sbt configurations...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment