Skip to content
This repository was archived by the owner on Aug 18, 2020. It is now read-only.

Commit 4b33aad

Browse files
committed
Added library fetching functionality to the bootstrap launcher.
1 parent 882f29e commit 4b33aad

File tree

3 files changed

+88
-5
lines changed

3 files changed

+88
-5
lines changed

.gitignore

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,7 @@ project/plugins/project/
2626
/plugins.sbt
2727
/config/
2828

29-
# resources of the bootstrap project (everything is generated with sbt bs)
30-
/bootstrap/src/main/resources/
29+
# resources of the bootstrap project
30+
/bootstrap/src/main/resources/
31+
/bin/
32+
/lib/

bootstrap/build.sbt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
name := "chatoverflow-bootstrap"
22
version := "0.1"
3-
assemblyJarName in assembly := "ChatOverflow.jar"
3+
assemblyJarName in assembly := "ChatOverflow.jar"
4+
5+
libraryDependencies += "org.scala-lang.modules" %% "scala-xml" % "1.1.1"
6+
fork := true

bootstrap/src/main/scala/Bootstrap.scala

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,91 @@
1+
import java.io.File
2+
import java.net.{HttpURLConnection, URL}
3+
import java.nio.file.Paths
4+
5+
import scala.sys.process._
6+
17
object Bootstrap {
28

9+
val currentFolderPath: String = Paths.get("").toAbsolutePath.toString
10+
val javaHomePath: String = System.getProperty("java.home")
11+
312
def main(args: Array[String]): Unit = {
4-
println("Hello! I'm the bootstrap launcher...")
13+
14+
if (testValidity()) {
15+
if (checkLibraries(args)) {
16+
// TODO: Create java path and classpath, just print for now (testing purposes)
17+
}
18+
}
19+
}
20+
21+
def checkLibraries(args: Array[String]): Boolean = {
22+
23+
val libFolder = new File(s"$currentFolderPath/lib")
24+
// Args contains --reload or lib folder is non existent?
25+
if ((args.length > 0 && args.head == "--reload") || !libFolder.exists()) {
26+
27+
// Create or clean directory
28+
if (libFolder.exists()) {
29+
for (libFile <- libFolder.listFiles()) {
30+
libFile.delete()
31+
}
32+
} else {
33+
libFolder.mkdir()
34+
}
35+
36+
// Download all libraries
37+
downloadLibraries()
38+
39+
} else {
40+
true
41+
}
42+
}
43+
44+
def downloadLibraries(): Boolean = {
45+
46+
// Get dependency xml and read dependencies with their download URL
47+
val dependencyStream = getClass.getResourceAsStream("/dependencies.xml")
48+
val dependencyXML = xml.XML.load(dependencyStream)
49+
val dependencies = for (dependency <- dependencyXML \\ "dependency")
50+
yield ((dependency \ "name").text.trim, (dependency \ "url").text.trim)
51+
52+
for ((name, url) <- dependencies) {
53+
downloadLibrary(name, url)
54+
}
55+
56+
true
57+
}
58+
59+
private def downloadLibrary(libraryName: String, libraryURL: String): Unit = {
60+
val url = new URL(libraryURL)
61+
62+
val connection = url.openConnection().asInstanceOf[HttpURLConnection]
63+
connection.setConnectTimeout(5000)
64+
connection.setReadTimeout(5000)
65+
connection.connect()
66+
67+
if (connection.getResponseCode >= 400) {
68+
69+
}
70+
// TODO: Retry 3 times
71+
// TODO: Feedback on download progress
72+
else {
73+
74+
// TODO: try
75+
url #> new File(s"$currentFolderPath/lib/${libraryURL.substring(libraryURL.lastIndexOf("/"))}") !!
76+
}
77+
}
78+
79+
private def testValidity(): Boolean = {
80+
// The only validity check for now is the existence of a bin folder
81+
new File(currentFolderPath + "/bin").exists()
582
}
683

784
/*
85+
TODO: Code deploy task (copying files, used after sbt clean and assembly)
886
TODO: Code bootstrap launcher
987
1. Bootstrap launcher checks integrity (bin folder existing)
10-
2. Bootstrap launcher checks libraries (no lib folder or flag -> Download everything)
88+
2. Bootstrap launcher checks libraries (no lib folder or flag -> Download everything
1189
3. Bootstrap launcher checks java path launched with and starts java -cp "..." chat overflow main class
1290
*/
1391

0 commit comments

Comments
 (0)