Skip to content

Instantly share code, notes, and snippets.

@d10xa
Created July 11, 2020 19:55
Show Gist options
  • Save d10xa/2f10f4b0673e56a6617f7ca20d326859 to your computer and use it in GitHub Desktop.
Save d10xa/2f10f4b0673e56a6617f7ca20d326859 to your computer and use it in GitHub Desktop.
import $ivy.`io.get-coursier::coursier:2.0.0-RC6-21`
import coursier._
import java.io.File
import java.io.FileInputStream
import scala.concurrent.ExecutionContext
def extractMainClassName(file: File): Option[String] = {
val jarStream =
new java.util.jar.JarInputStream(new FileInputStream(file))
val mf = jarStream.getManifest
Option(mf.getMainAttributes).flatMap(attrs =>
Option(attrs.getValue("Main-Class"))
)
}
def fetchDependencyMainClassUnsafe(d: Dependency): List[String] =
Fetch()
.addDependencies(d)
.run()
.filter(_.getName == s"${d.module.name.value}-${d.version}.jar")
.toList
.flatMap(extractMainClassName)
def fetchVersionsUnsafe(module: Module): List[String] =
Versions()
.withModule(module)
.result()
.unsafeRun()(ExecutionContext.global)
.results
.collect { case (_, Right(value)) => value }
.flatMap(_.available)
.toList
val module = Module(
Organization("io.swagger.codegen.v3"),
ModuleName("swagger-codegen-cli")
)
fetchVersionsUnsafe(module)
.foreach { version =>
val mainClassOpt =
fetchDependencyMainClassUnsafe(Dependency(module, version))
println(s"$version: $mainClassOpt")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment