Created
August 24, 2023 13:23
-
-
Save datYori/4ddc05d8d5b42829af1111da12f79ebc to your computer and use it in GitHub Desktop.
find unused Dependencies.scala val in build.sbt
This file contains 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 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