Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bin/kshell.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

MAVEN_REPO_BASE=$HOME/.m2/repository/org/jetbrains/kotlin
KOTLIN_VERSION=1.2.30
SPARKLIN_VERSION=0.2
SPARKLIN_VERSION=0.2-SNAPSHOT

# Kotlin libraries
KOTLIN_STDLIB=${MAVEN_REPO_BASE}/kotlin-stdlib/${KOTLIN_VERSION}/kotlin-stdlib-${KOTLIN_VERSION}.jar
Expand Down
2 changes: 1 addition & 1 deletion bin/sparklin-spark1x.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

MAVEN_REPO_BASE=$HOME/.m2/repository/org/jetbrains/kotlin
KOTLIN_VERSION=1.2.30
SPARKLIN_VERSION=0.2
SPARKLIN_VERSION=0.2-SNAPSHOT

# Kotlin libraries
KOTLIN_STDLIB=${MAVEN_REPO_BASE}/kotlin-stdlib/${KOTLIN_VERSION}/kotlin-stdlib-${KOTLIN_VERSION}.jar
Expand Down
2 changes: 1 addition & 1 deletion bin/sparklin-spark2x.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

MAVEN_REPO_BASE=$HOME/.m2/repository/org/jetbrains/kotlin
KOTLIN_VERSION=1.2.30
SPARKLIN_VERSION=0.2
SPARKLIN_VERSION=0.2-SNAPSHOT

# Kotlin libraries
KOTLIN_STDLIB=${MAVEN_REPO_BASE}/kotlin-stdlib/${KOTLIN_VERSION}/kotlin-stdlib-${KOTLIN_VERSION}.jar
Expand Down
2 changes: 1 addition & 1 deletion conf/spark1x.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ plugins=sparklin.kshell.plugins.LoadFilePlugin, \
sparklin.spark1x.Spark1xPlugin, \
sparklin.hdfsbrowser.HdfsBrowserPlugin, \
sparklin.kshell.plugins.SyntaxPlugin, \
sparklin.kshell.plugins.PromptPlugin
sparklin.kshell.plugins.PromptPlugin
4 changes: 2 additions & 2 deletions hdfs-browser/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
<parent>
<groupId>sparklin</groupId>
<artifactId>sparklin</artifactId>
<version>0.2</version>
<version>0.2-SNAPSHOT</version>
</parent>


<artifactId>hdfs-browser</artifactId>
<version>0.2</version>
<version>0.2-SNAPSHOT</version>

<dependencies>
<dependency>
Expand Down
4 changes: 2 additions & 2 deletions jline3-shaded/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
<parent>
<groupId>sparklin</groupId>
<artifactId>sparklin</artifactId>
<version>0.2</version>
<version>0.2-SNAPSHOT</version>
</parent>


<artifactId>jline3-shaded</artifactId>
<version>0.2</version>
<version>0.2-SNAPSHOT</version>

<dependencies>
<dependency>
Expand Down
4 changes: 2 additions & 2 deletions kshell-repl-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
<parent>
<groupId>sparklin</groupId>
<artifactId>sparklin</artifactId>
<version>0.2</version>
<version>0.2-SNAPSHOT</version>
</parent>


<artifactId>kshell-repl-api</artifactId>
<version>0.2</version>
<version>0.2-SNAPSHOT</version>

<properties>
<kotlin.version>1.2.31</kotlin.version>
Expand Down
6 changes: 3 additions & 3 deletions kshell/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
<parent>
<groupId>sparklin</groupId>
<artifactId>sparklin</artifactId>
<version>0.2</version>
<version>0.2-SNAPSHOT</version>
</parent>

<artifactId>kshell</artifactId>
<version>0.2</version>
<version>0.2-SNAPSHOT</version>

<dependencies>
<dependency>
Expand Down Expand Up @@ -46,7 +46,7 @@
<dependency>
<groupId>sparklin</groupId>
<artifactId>kshell-repl-api</artifactId>
<version>0.2</version>
<version>0.2-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>sparklin</groupId>
Expand Down
69 changes: 62 additions & 7 deletions kshell/src/main/kotlin/sparklin/kshell/KShell.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ import org.jetbrains.kotlin.cli.jvm.config.jvmClasspathRoots
import org.jetbrains.kotlin.config.CommonConfigurationKeys
import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.utils.PathUtil
import sparklin.kshell.configuration.BooleanConverter
import sparklin.kshell.configuration.Configuration
import sparklin.kshell.configuration.IntConverter
import sparklin.kshell.org.jline.reader.EndOfFileException
import sparklin.kshell.org.jline.reader.LineReader
import sparklin.kshell.org.jline.reader.LineReaderBuilder
import sparklin.kshell.org.jline.reader.UserInterruptException
import sparklin.kshell.org.jline.terminal.Terminal
import sparklin.kshell.org.jline.terminal.TerminalBuilder
import sparklin.kshell.repl.*
import sparklin.kshell.wrappers.ResultWrapper
Expand Down Expand Up @@ -46,10 +50,8 @@ open class KShell(val disposable: Disposable,

val incompleteLines = arrayListOf<String>()

val term = TerminalBuilder.builder().build()
lateinit var readerBuilder: LineReaderBuilder
lateinit var reader: LineReader
private var maxResultLength: Int = 500

val highlighter = ContextHighlighter({ s -> !isCommandMode(s)}, { s -> commands.firstOrNull { it.weakMatch(s) } })

Expand All @@ -67,6 +69,24 @@ open class KShell(val disposable: Disposable,

var evaluationTimeMillis: Long = 0

class EvalThread: Thread() {
lateinit var evalBlock: () -> ResultWrapper
var result: ResultWrapper = ResultWrapper(Result.Error(EvalError.RuntimeError("Interrupted")))

override fun run() {
result = evalBlock()
}
}

var evalThread = EvalThread()

class Settings(conf: Configuration) {
val overrideSignals: Boolean by conf.get(BooleanConverter, default = true)
val maxResultLength: Int by conf.get(IntConverter, default = 500)
}

lateinit var settings: Settings

private class FakeQuit: sparklin.kshell.BaseCommand() {
override val name: String = "quit"
override val short: String = "q"
Expand All @@ -79,13 +99,25 @@ open class KShell(val disposable: Disposable,
fun addClasspathRoots(files: List<File>) = compilerConfiguration.addJvmClasspathRoots(files)

fun initEngine() {
configuration.load()

settings = Settings(configuration)

val term = if (settings.overrideSignals) {
TerminalBuilder.builder().nativeSignals(true).signalHandler({
if (it == Terminal.Signal.INT) {
interrupt()
}
}).build()
} else {
TerminalBuilder.builder().build()
}

readerBuilder = LineReaderBuilder.builder().terminal(term).highlighter(highlighter)
reader = readerBuilder.build()

configuration.load()
configuration.plugins().forEach { it.init(this, configuration) }

maxResultLength = configuration.get("maxResultLength", IntConverter, 500)
reader.setVariable(LineReader.HISTORY_FILE, configuration.get(LineReader.HISTORY_FILE,
System.getProperty("user.home") + File.separator + ".kshell_history"))
reader.setVariable(LineReader.SECONDARY_PROMPT_PATTERN, "")
Expand All @@ -94,6 +126,11 @@ open class KShell(val disposable: Disposable,
evaluator = ReplEvaluator(classpath, baseClassloader)
}

private fun interrupt() {
evalThread.interrupt()
evalThread = EvalThread()
}

private fun isCommandMode(buffer: String): Boolean = incompleteLines.isEmpty()
&& buffer.startsWith(":")
&& !buffer.startsWith("::")
Expand All @@ -102,6 +139,7 @@ open class KShell(val disposable: Disposable,
initEngine()

do {
try {
val line = reader.readLine(prompt())

if (line == null || isQuitAction(line)) break
Expand Down Expand Up @@ -140,7 +178,9 @@ open class KShell(val disposable: Disposable,
}
}
}
}
} }
catch (e: UserInterruptException) { if (settings.overrideSignals) state.lineIndex.getAndIncrement() else break }
catch (ee: EndOfFileException) { break }
} while (true)

cleanUp()
Expand All @@ -160,7 +200,22 @@ open class KShell(val disposable: Disposable,

fun compile(code: SourceCode) = compiler.compile(state, code)

fun eval(source: String): ResultWrapper =
fun eval(source: String): ResultWrapper {
return if (settings.overrideSignals) {
evalThread.apply {
evalBlock = { compileAndEval(source) }
start()
join()
}
val result = evalThread.result
evalThread = EvalThread()
result
} else {
compileAndEval(source)
}
}

fun compileAndEval(source: String): ResultWrapper =
state.lock.write {
val compileResult = compile(source)
ResultWrapper(when (compileResult) {
Expand All @@ -183,7 +238,7 @@ open class KShell(val disposable: Disposable,

fun handleSuccess(result: EvalResult) {
if (result is EvalResult.ValueResult)
println(result.toString().bound(maxResultLength))
println(result.toString().bound(settings.maxResultLength))
}

private fun commandError(e: Exception) {
Expand Down
1 change: 1 addition & 0 deletions kshell/src/main/kotlin/sparklin/kshell/KotlinShell.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import org.jetbrains.kotlin.utils.PathUtil
import sparklin.kshell.configuration.CachedInstance
import sparklin.kshell.configuration.Configuration
import sparklin.kshell.configuration.ConfigurationImpl
import sparklin.kshell.org.jline.reader.UserInterruptException
import sparklin.kshell.repl.stdlibPathForJar

object KotlinShell {
Expand Down
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@
<properties>
<kotlin.version>1.2.30</kotlin.version>
<jvm.version>1.8</jvm.version>
<sparklin.version>0.2</sparklin.version>
<sparklin.version>0.2-SNAPSHOT</sparklin.version>
</properties>

<groupId>sparklin</groupId>
<artifactId>sparklin</artifactId>
<version>0.2</version>
<version>0.2-SNAPSHOT</version>

<packaging>pom</packaging>

<modules>
<module>kshell-repl-api</module>
<module>jline3-shaded</module>
<!--<module>jline3-shaded</module>-->
<module>kshell</module>
<module>sparklin-spark-1.x</module>
<module>sparklin-spark-2.x</module>
Expand Down
4 changes: 2 additions & 2 deletions sparklin-spark-1.x/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
<parent>
<groupId>sparklin</groupId>
<artifactId>sparklin</artifactId>
<version>0.2</version>
<version>0.2-SNAPSHOT</version>
</parent>


<artifactId>sparklin-spark-1.x</artifactId>
<version>0.2</version>
<version>0.2-SNAPSHOT</version>

<dependencies>
<dependency>
Expand Down
4 changes: 2 additions & 2 deletions sparklin-spark-2.x/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
<parent>
<groupId>sparklin</groupId>
<artifactId>sparklin</artifactId>
<version>0.2</version>
<version>0.2-SNAPSHOT</version>
</parent>


<artifactId>sparklin-spark-2.x</artifactId>
<version>0.2</version>
<version>0.2-SNAPSHOT</version>

<dependencies>
<dependency>
Expand Down
4 changes: 2 additions & 2 deletions zeppelin-interpreter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
<parent>
<groupId>sparklin</groupId>
<artifactId>sparklin</artifactId>
<version>0.2</version>
<version>0.2-SNAPSHOT</version>
</parent>


<artifactId>zeppelin-interpreter</artifactId>
<version>0.2</version>
<version>0.2-SNAPSHOT</version>

<dependencies>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public InterpreterResult interpret(String line, InterpreterContext interpreterCo
.put("CURRENT_THREAD", Thread.currentThread()); //to be able to terminate thread

out.setInterpreterOutput(interpreterContext.out);
ResultWrapper result = repl.eval(line);
ResultWrapper result = repl.compileAndEval(line);
ResultWrapper.Status status = result.getStatus();
if (status == ERROR || status == INCOMPLETE) {
Exception e = result.getErrorCause();
Expand Down