Skip to content

Instantly share code, notes, and snippets.

@Centaur
Created November 10, 2011 11:17
Show Gist options
  • Save Centaur/1354634 to your computer and use it in GitHub Desktop.
Save Centaur/1354634 to your computer and use it in GitHub Desktop.
a scala script to update sbt-launch.jar to the latest version
#!/bin/sh
exec scala $0 $0 $@
!#
/**
* This script finds the newest sbt-launch.jar from its official repository,
* update the one in th same directory of this script if necessary.
* it manages the current sbt version in a companion file 'current_sbt_version'
* and will create it if not already exists.
*/
import java.net.URL
import xml.XML
import io.Source
import java.io._
import sys.process._
if(argv.size != 2) {
println("Usage: updatesbt VERSION")
System.exit(0)
}
val indexURL = "http://typesafe.artifactoryonline.com/typesafe/ivy-releases/org.scala-sbt/sbt-launch/"
//val regex = """.*<a\s+href="(.*)/".*""".r
//val content = Source.fromURL(indexURL).getLines.collect {
// case regex(version) if(version != "..")=> version
//}
def asComparable(str:String) = str.split("""\.""").map(_.replace("-", ".").toFloat).toList
import Ordering.Implicits._
val newest = argv(1) // content.map(asComparable).max.mkString(".")
val currentSbtVersion = new File(new File(argv(0)).getParent()+"/current_sbt_version")
def write1Line(f:File, content:String) {
("echo " + content) #> f !
}
if(!currentSbtVersion.exists()) write1Line(currentSbtVersion, "0")
val current = Source.fromFile(currentSbtVersion).mkString.trim
println("newest version:%s, current version:%s".format(newest, current))
if(asComparable(newest) > asComparable(current)){
println("Downloading...")
} else {
println("Update canceled.")
System.exit(0)
}
val targetURL = new URL(indexURL + newest + "/sbt-launch.jar")
val targetFile = new File(new File(argv(0)).getParent()+"/sbt-launch.jar")
targetURL #> targetFile !
println("Ok.")
println("Updating current_sbt_version...")
write1Line(currentSbtVersion, newest)
println("Done.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment