Skip to content

Commit dbd74f8

Browse files
committed
Initial commit
0 parents  commit dbd74f8

File tree

5 files changed

+145
-0
lines changed

5 files changed

+145
-0
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2017 Holger Brandl
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# kscript-support-api
2+
A support API written in Kotlin to simplify scripting with kscript

build.gradle

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
buildscript {
2+
ext.kotlin_version = '1.1.2'
3+
repositories {
4+
mavenCentral()
5+
}
6+
dependencies {
7+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
8+
}
9+
}
10+
11+
apply plugin: 'kotlin'
12+
apply plugin: 'application'
13+
apply plugin: 'maven'
14+
15+
//mainClassName = 'demo.HelloWorldKt'
16+
mainClassName = "de.mpicbg.scicomp.kutils.BashKt" // not needed but does not work without
17+
18+
defaultTasks 'run'
19+
20+
repositories {
21+
mavenCentral()
22+
mavenLocal()
23+
}
24+
25+
// https://docs.gradle.org/current/userguide/java_plugin.html#sec:java_plugin_and_dependency_management
26+
dependencies {
27+
// compileOnly "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
28+
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
29+
30+
testRuntime 'junit:junit:4.11'
31+
testRuntime "io.kotlintest:kotlintest:1.3.2"
32+
}
33+
34+
35+
// http://stackoverflow.com/questions/11474729/how-to-build-sources-jar-with-gradle
36+
task sourcesJar(type: Jar, dependsOn: classes) {
37+
classifier = 'sources'
38+
from sourceSets.main.allSource
39+
}
40+
41+
artifacts {
42+
archives sourcesJar
43+
}
44+
45+
// https://docs.gradle.org/current/userguide/maven_plugin.html
46+
uploadArchives {
47+
repositories {
48+
mavenDeployer {
49+
// pom.groupId = 'de.mpicbg.scicomp'
50+
// pom.version = '0.1-SNAPSHOT'
51+
pom.artifactId = 'kscript'
52+
53+
repository(url: "file://localhost/tmp/myRepo/")
54+
}
55+
}
56+
}
57+
58+
//http://stackoverflow.com/questions/34377367/why-is-gradle-install-replacing-my-version-with-unspecified
59+
group = 'kscript'
60+
version = '1.0-SNAPSHOT'
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package kscript
2+
3+
import java.io.File
4+
import kotlin.system.exitProcess
5+
6+
7+
// todo maybe it should also become part of the kscript code repository once ready
8+
9+
// for top-level vs member extensions see https://kotlinlang.org/docs/reference/extensions.html#scope-of-extensions
10+
//object KscriptHelpers {}
11+
12+
val stdin = generateSequence() { readLine() }
13+
14+
fun Sequence<String>.print() = forEach { println(it) }
15+
16+
17+
fun processStdin(trafo: (String) -> String) {
18+
generateSequence() { readLine() }.map {
19+
println(trafo(it))
20+
}
21+
}
22+
23+
@Deprecated("use mapLines instead")
24+
fun File.processLines(trafo: (String) -> String) {
25+
useLines {
26+
it.map { println(trafo(it)) }
27+
}
28+
}
29+
30+
fun <T> File.mapLines(trafo: (String) -> T) {
31+
return useLines {
32+
it.map { trafo(it) }
33+
}
34+
}
35+
36+
fun String.processLines(trafo: (String) -> String) {
37+
split("\n").map { println(trafo(it)) }
38+
}
39+
40+
41+
public inline fun stopIfNot(value: Boolean, lazyMessage: () -> Any): Unit {
42+
if (!value) {
43+
System.err.println("ERROR: " + lazyMessage().toString())
44+
exitProcess(1)
45+
}
46+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package kscript.test
2+
3+
import io.kotlintest.specs.FlatSpec
4+
import kscript.print
5+
import kscript.stdin
6+
7+
/**
8+
* @author Holger Brandl
9+
*/
10+
class SupportApiTest : FlatSpec() { init {
11+
12+
"it" should "allow to use stdin for filtering and mapping" {
13+
val longDf = stdin.filter { "^de0[-0]*".toRegex().matches(it) }.map { it + "foo:" }.print()
14+
}
15+
}
16+
}

0 commit comments

Comments
 (0)