scaladependenciesversionsbtsbt-android-plugin

How to download user-provided Scala version using SBT?


I am trying to write a new class while extending Jan Berkel's Android Plugin using which I would like to provide a functionality of pushing entire scala library to rooted phones.

So far, I have the code that uses local versions of Scala you have already downloaded to your computer, however, I'd like to be able to download a version of Scala if it's not there.

Here is the relevant part of code:

def makePermission(version: String) {  
    val home = JSystem.getProperty("user.home")
    val scalaFolder = new JFile(home + "/.sbt/boot/scala-" + version)

    if (!scalaFolder.exists) {
        // TODO: Try to download the Scala version in "version"
    }

    val permission = new JFile(scalaFolder, "scala-library.xml")

    if (scalaFolder.exists && !permission.exists) {
        val writer = new PrintWriter(permission)
        writer.println("<?xml version=\"1.0\" encoding=\"utf-8\"?>")
        writer.println("")
        writer.println("<permissions>")
        writer.println("    <library")
        writer.println("        name=\"scala-library-" + version + "\"")
        writer.println("        file=\"/system/framework/scala-library-" + version + "\"")
        writer.println("    />")
        writer.println("</permissions>")
        writer.close
    }
}

For example, calling my function like this:
makePermission("2.9.1") will see that I have Scala 2.9.1 and make scala-library.xml in scala-2.9.1 folder.

Doing this:
makePermission("2.8.1") will see that I don't have Scala 2.8.1, download it and make scala-library.xml in scala-2.8.1 folder.

However, doing this:
makePermission("3.0.0") will see that I don't have Scala 3.0.0, try to download it but see it doesn't exist nad skip the rest.

I'm puzzled how to do the TODO part, so any help is appreciated. :D


Solution

  • I am trying to write a new class while extending Jan Berkel's Android Plugin

    That sounds like sbt 0.7, but let's assume you're on sbt 0.12. You can rely on sbt itself to grab any published version of Scala by saying

    > ++2.10.2
    

    Then you can get hold of scala-library.jar by looking at fullClasspath in Runtime. From the shell, it's:

    > show runtime:full-classpath
    [info] List(Attributed(~/foo/app/target/scala-2.9.2/classes), Attributed(~/foo/library/target/scala-2.9.2/classes), Attributed(/~/foo/.sbt/0.12.0/boot/scala-2.9.2/lib/scala-library.jar))
    

    Make a task that depends on fullClasspath in Runtime, and extract the full path to scala-library.jar in there.