-
Notifications
You must be signed in to change notification settings - Fork 1
/
commonScriptingUtils.kt
68 lines (61 loc) · 2 KB
/
commonScriptingUtils.kt
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
package com.ivieleague.skate
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import java.net.URL
import java.util.*
import java.util.concurrent.TimeUnit
val home: File get() = File(System.getProperty("user.home"))
fun download(url: String, to: File): File {
var lastPrint = System.currentTimeMillis()
URL(url).openStream().buffered().use { input ->
FileOutputStream(to).use { output ->
var bytesCopied: Long = 0
val buffer = ByteArray(DEFAULT_BUFFER_SIZE)
var bytes = input.read(buffer)
while (bytes >= 0) {
output.write(buffer, 0, bytes)
bytesCopied += bytes
bytes = input.read(buffer)
val now = System.currentTimeMillis()
if (now - lastPrint > 5000) {
val mb = bytesCopied / (1024 * 1024)
println("Download at ${bytesCopied / mb}mb - ${url}")
lastPrint = now
}
}
}
}
return to
}
fun execute(vararg command: String): Int {
val p = ProcessBuilder().command(*command).inheritIO().start()
p.waitFor()
return p.exitValue()
}
fun execute(
command: Array<String>,
directory: File = File(""),
environment: Map<String, String> = mapOf(),
abandonAfterMilliseconds: Long = Long.MAX_VALUE
): Int {
val p = ProcessBuilder()
.command(*command)
.directory(directory)
.apply {
environment().putAll(environment)
}
.inheritIO()
.start()
p.waitFor(abandonAfterMilliseconds, TimeUnit.MILLISECONDS)
return p.exitValue()
}
@Suppress("UNCHECKED_CAST")
val userProperties: Map<String, String> by lazy {
try {
val props = Properties().apply { load(FileInputStream(home.resolve("user.properties"))) }
props as Map<String, String>
} catch (e: Exception) {
mapOf<String, String>()
}
}