-
Notifications
You must be signed in to change notification settings - Fork 3
/
usi.scala
83 lines (63 loc) · 2.37 KB
/
usi.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* Script de lancement du serveur USI 2011 en mode developpement.
* - mvn est dans le PATH
* - le repertoire courant est la racine du projet challenge-usi
* Usage: scala usi.scala [port]
*/
import java.io._
import scala.io.Source._
import scala.xml.XML._
import System._
object usi {
val EOL = getProperty("line.separator")
val PATH = getProperty("path.separator")
val DEFAULT_PORT = "8082"
val cpFile = "target/classpath.txt"
def system (cmd: String*): Process = {
new ProcessBuilder (cmd:_*).redirectErrorStream(true).start
}
def system (cmd: Array[String], env : Map[String,String]): Process = {
val pb = new ProcessBuilder (cmd:_*).redirectErrorStream(true)
for((k,v) <- env) pb.environment.put(k,v)
pb.start
}
def assertInRootDirectory : Boolean = {
val cwd = new File(System.getProperty("user.dir"))
try {
(loadFile(new File(cwd,"pom.xml")) \ "artifactId").text == "challenge-usi"
} catch {
case e => false
}
}
def collectOutput(proc : Process, to : PrintStream) : Unit =
new Thread { override def run() = for(c <- fromInputStream(proc.getInputStream)) to.print(c) }.start
def reap(proc : Process) : Unit =
Runtime.getRuntime().addShutdownHook(new Thread { override def run() = proc.destroy })
def mvnExecutable = if(getProperty("os.name").toLowerCase.startsWith("windows")) "mvn.bat" else "mvn"
def main(args: Array[String]) = {
if(!assertInRootDirectory) {
println("Not in toplevel directory for project challenge-usi, giving up")
exit(1)
}
val pb = system(mvnExecutable,"-q","dependency:build-classpath","-Dmdep.outputFile=" + cpFile)
collectOutput(pb,out)
reap(pb)
if(pb.waitFor != 0) {
println("Failed to generate classpath.txt. Is maven in your PATH?")
println("If so, check that you configured your #/.m2/setting.xml to activate 'with-repo' profile")
println("See http://maven.apache.org/settings.html#Active_Profiles")
exit(1)
}
val classpath = "target/classes" + PATH + fromFile(new File(cpFile)).mkString
println("generated classpath ")
val java = system(Array("java","org.psug.usi.Main"), Map("CLASSPATH" -> classpath))
collectOutput(java,out)
reap(java)
println("Type <ENTER> to exit")
while(System.in.read() == -1)
wait(500)
java.destroy
java.waitFor
}
}
usi.main(args)