Skip to content

Instantly share code, notes, and snippets.

@tbje
Last active December 27, 2015 11:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tbje/7322501 to your computer and use it in GitHub Desktop.
Save tbje/7322501 to your computer and use it in GitHub Desktop.
Allows you to look at sources of dependencies in ensime. Feel free to transform this into a sbt plugin.
// After having run eclipse with-sources=true from sbt (with sbteclipse) .classpath should contain sourcepaths:
val eclippseCp = scala.xml.XML.load(".classpath")
val entries = eclippseCp.child filter { _.label == "classpathentry" }
val sources = entries flatMap { _.attributes filter { _.key=="sourcepath" }} map { _.value.text }
val destDir = "~/ensime-sources" // feel free to modify this
def fileName(file: String) = new java.io.File(file).getName.replace("-sources.jar", "")
import java.io.File
def extractJar(file: File, destDir: String): Unit = {
val jar = new java.util.jar.JarFile(file)
import scala.collection.JavaConverters._
val entries = jar.entries().asScala.toList.sortBy(_.getName.length)
def withResource[A <: {def close(): Unit},B](res: A)(func: A=>B) = try func(res) finally res.close()
for {
entry <- entries
destinationFile = new java.io.File(destDir + java.io.File.separator + entry.getName())
} {
if (entry.isDirectory()) {
println(s"dir: ${destinationFile}")
destinationFile.mkdir()
} else {
println(s"file: ${destinationFile}")
val parent = destinationFile.getParentFile
if (!parent.exists) parent.mkdirs()
destinationFile.createNewFile()
withResource(jar.getInputStream(entry)) { is =>
withResource(new java.io.FileOutputStream(destinationFile)) { fos =>
while (is.available() > 0) {
fos.write(is.read())
}
}
}
}
}
}
// this will unpack the sources in the destDir
sources foreach { file =>
extractJar(new File(file), destDir + fileName(file))
}
// this goes in .ensime file
println(sources map { file =>
s""""$destDir/${fileName(file)}""""
} mkString(":reference-source-roots (\n", "\n", "\n)"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment