-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScriptUtils.kt
53 lines (44 loc) · 1.46 KB
/
ScriptUtils.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
import java.io.File
import java.io.FileWriter
import java.io.IOException
import com.github.doyaaaaaken.kotlincsv.client.CsvFileReader
import com.github.doyaaaaaken.kotlincsv.dsl.csvReader
class Logger(private val verbose: Boolean = false) {
fun verbose(message: String) {
if (verbose) println(message)
}
fun info(message: String) {
println(message)
}
}
class Shell(private val logger: Logger) {
fun execute(command: String, dir: File? = null): Int {
logger.verbose("Running in shell: $command")
return ProcessBuilder("/bin/sh", "-c", command)
.redirectErrorStream(true)
.inheritIO()
.directory(dir)
.start()
.waitFor()
}
}
class FileWriter(private val logger: Logger) {
companion object {
val GENERATED_FILE_PATH = "StarWarsCharacters.kt"
}
@Throws(IOException::class)
fun write(fileContent: String) {
logger.verbose("Starting to write file to path: $GENERATED_FILE_PATH")
FileWriter(GENERATED_FILE_PATH).use {
it.write(fileContent)
}
logger.verbose("Finished writing file to: $GENERATED_FILE_PATH")
}
}
class CsvReader(private val logger: Logger) {
fun <T> open(filePath: String, csvFileReader: CsvFileReader.() -> T) {
logger.verbose("Opening csv file at: $filePath")
csvReader().open(filePath, csvFileReader)
logger.verbose("Finished processing csv file.")
}
}