Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package third_party.dependency_analyzer.src.main.io.bazel.rulesscala.dependencyanalyzer

import java.util.jar.JarFile

import scala.reflect.io.AbstractFile
import scala.tools.nsc.plugins.{Plugin, PluginComponent}
import scala.tools.nsc.{Global, Phase}
import scala.util.Try

class DependencyAnalyzer(val global: Global) extends Plugin {

Expand Down Expand Up @@ -59,7 +62,7 @@ class DependencyAnalyzer(val global: Global) extends Plugin {
private def warnOnIndirectTargetsFoundIn(usedJars: Set[AbstractFile]) = {
for (usedJar <- usedJars;
usedJarPath = usedJar.path;
target <- indirect.get(usedJarPath) if !direct.contains(usedJarPath)) {
target <- getJarLabel(usedJarPath) if !direct.contains(usedJarPath)) {
val errorMessage =
s"""Target '$target' is used but isn't explicitly declared, please add it to the deps.
|You can use the following buildozer command:
Expand All @@ -77,6 +80,19 @@ class DependencyAnalyzer(val global: Global) extends Plugin {

}

private def getJarLabel(jarPath: String): Option[String] = {
// First check the jar's manifest for a stamped label
Try(new JarFile(jarPath)).toOption.flatMap { jf ⇒
try {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we change to:

try {
  val jf = new JarFile(jarPath)
  ...

rather than Try().toOption since we have a try following anyway? Seems a bit clearer

Option(jf.getManifest.getMainAttributes.getValue("Target-Label"))
} catch {
case e: Exception ⇒ None
} finally {
jf.close()
}
}.orElse(indirect.get(jarPath))
}

import global._

private def findUsedJars: Set[AbstractFile] = {
Expand Down