Skip to content

Commit ed9e472

Browse files
committed
added more support api elements
1 parent 8196892 commit ed9e472

File tree

3 files changed

+63
-26
lines changed

3 files changed

+63
-26
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,36 @@
11
package kscript
22

3+
import java.io.*
4+
import java.util.zip.GZIPOutputStream
5+
36
/**
47
* Helpers to deal with files in kscript applications
58
*
69
* @author Holger Brandl
710
*/
11+
12+
13+
operator fun File.div(childName: String): File {
14+
return this.resolve(childName)
15+
}
16+
17+
18+
//https://dzone.com/articles/readingwriting-compressed-and
19+
/** Save a list of items into a file. Output can be option ally zipped and a the stringifying operation can be changed from toString to custom operation if needed. */
20+
fun <T> Iterable<T>.saveAs(f: File,
21+
transform: (T) -> String = { it.toString() },
22+
separator: Char = '\n',
23+
overwrite: Boolean = true,
24+
compress: Boolean = f.name.let { it.endsWith(".zip") || it.endsWith(".gz") }) {
25+
26+
// ensure that file is not yet there or overwrite flag is set
27+
require(!f.isFile || overwrite) { "$f is present already. Use overwrite=true to enforce file replacement." }
28+
29+
val p = if (!compress) PrintWriter(f) else BufferedWriter(OutputStreamWriter(GZIPOutputStream(FileOutputStream(f))))
30+
31+
toList().forEach { p.write(transform(it) + separator) }
32+
33+
p.close()
34+
}
35+
36+
Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,37 @@
11
package kscript
22

3+
import java.io.BufferedReader
34
import java.io.File
5+
import java.io.FileReader
46
import kotlin.system.exitProcess
57

68

7-
// todo maybe it should also become part of the kscript code repository once ready
8-
99
// for top-level vs member extensions see https://kotlinlang.org/docs/reference/extensions.html#scope-of-extensions
1010
//object KscriptHelpers {}
1111

1212
val stdin = generateSequence() { readLine() }
1313

14-
fun Sequence<String>.print() = forEach { println(it) }
1514

15+
/** Read lines stdin or a file argument. Example 'argLines(1).map { it+"foo"}'. could be used either with "-" or file argument. */
16+
fun argLines(arg: String, stdinNames: List<String> = listOf("-", "stdin")): Sequence<String> {
17+
if (stdinNames.contains(arg)) return stdin
1618

17-
fun processStdin(trafo: (String) -> String) {
18-
generateSequence() { readLine() }.map {
19-
println(trafo(it))
20-
}
21-
}
19+
val inputFile = File(arg)
2220

23-
@Deprecated("use mapLines instead")
24-
fun File.processLines(trafo: (String) -> String) {
25-
useLines {
26-
it.map { println(trafo(it)) }
27-
}
28-
}
21+
stopIfNot(inputFile.canRead()) { "Can not read from '${arg}'" }
2922

30-
fun <T> File.mapLines(trafo: (String) -> T) {
31-
return useLines {
32-
it.map { trafo(it) }
33-
}
23+
// not we don't close the buffer with this approach
24+
// BufferedReader(FileReader(inputFile )).use { return it }
25+
return BufferedReader(FileReader(inputFile)).lineSequence()
3426
}
3527

36-
fun String.processLines(trafo: (String) -> String) {
37-
split("\n").map { println(trafo(it)) }
38-
}
3928

4029

41-
public inline fun stopIfNot(value: Boolean, lazyMessage: () -> Any): Unit {
42-
if (!value) {
43-
System.err.println("ERROR: " + lazyMessage().toString())
44-
exitProcess(1)
30+
fun Sequence<String>.print() = forEach { println(it) }
31+
32+
33+
fun processStdin(trafo: (String) -> String) {
34+
generateSequence() { readLine() }.map {
35+
println(trafo(it))
4536
}
46-
}
37+
}

src/main/kotlin/kscript/Support.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package kscript
2+
3+
import kotlin.system.exitProcess
4+
5+
/**
6+
* @author Holger Brandl
7+
*/
8+
9+
10+
fun foo() = "1"
11+
12+
public inline fun stopIfNot(value: Boolean, lazyMessage: () -> Any) {
13+
if (!value) {
14+
System.err.println("[ERROR] " + lazyMessage().toString())
15+
exitProcess(1)
16+
}
17+
}

0 commit comments

Comments
 (0)