|
| 1 | +import java.io.File |
| 2 | +import java.net.{HttpURLConnection, URL} |
| 3 | +import java.nio.file.Paths |
| 4 | + |
| 5 | +import scala.sys.process._ |
| 6 | + |
1 | 7 | object Bootstrap { |
2 | 8 |
|
| 9 | + val currentFolderPath: String = Paths.get("").toAbsolutePath.toString |
| 10 | + val javaHomePath: String = System.getProperty("java.home") |
| 11 | + |
3 | 12 | 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() |
5 | 82 | } |
6 | 83 |
|
7 | 84 | /* |
| 85 | + TODO: Code deploy task (copying files, used after sbt clean and assembly) |
8 | 86 | TODO: Code bootstrap launcher |
9 | 87 | 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 |
11 | 89 | 3. Bootstrap launcher checks java path launched with and starts java -cp "..." chat overflow main class |
12 | 90 | */ |
13 | 91 |
|
|
0 commit comments