Skip to content

Instantly share code, notes, and snippets.

@Centaur
Created December 5, 2011 15:10
Show Gist options
  • Save Centaur/1433893 to your computer and use it in GitHub Desktop.
Save Centaur/1433893 to your computer and use it in GitHub Desktop.
sbt updator for windows (rename it to .bat)
::#!
@echo off
call scala .\%0 .\%0 %*
goto :eof
::!#
/**
* 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._
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