Skip to content

Commit b6aebbb

Browse files
authored
Working version running JShell through Exec task (#6)
1 parent 09e2392 commit b6aebbb

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

src/main/groovy/com/github/mrsarm/jshell/plugin/JShellPlugin.groovy

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@
1515
*/
1616
package com.github.mrsarm.jshell.plugin
1717

18-
import jdk.jshell.tool.JavaShellToolBuilder
1918
import org.gradle.api.Plugin
2019
import org.gradle.api.Project
2120
import org.gradle.api.Task
21+
import org.gradle.api.tasks.Exec
2222
import org.gradle.api.tasks.JavaExec
2323

2424
class JShellPlugin implements Plugin<Project> {
2525

26-
private String[] shellArgs
26+
private shellArgs = []
2727

2828
// This task could be merged with the main task in just one,
2929
// the problem is that the main task is a JavaExec one,
@@ -49,9 +49,9 @@ class JShellPlugin implements Plugin<Project> {
4949
pathSet.addAll(classpath.findAll { it.exists() })
5050
}
5151
}
52-
def path = pathSet.join(System.properties['os.name'].toLowerCase().contains('windows') ? ';' : ':')
52+
def path = pathSet.join(System.getProperty("path.separator"))
5353
jshellTask.logger.info(":jshell executing with --class-path \"{}\"", path)
54-
shellArgs = [
54+
shellArgs += [
5555
"--class-path", path,
5656
"--startup", "DEFAULT",
5757
"--startup", "PRINTING"
@@ -63,15 +63,15 @@ class JShellPlugin implements Plugin<Project> {
6363
def jshellStartup = project.findProperty("jshell.startup")
6464
jshellTask.logger.info(":jshell executing with --startup DEFAULT --startup PRINTING " +
6565
"--startup \"{}\"", jshellStartup)
66-
shellArgs = shellArgs + (String[]) ["--startup", jshellStartup]
66+
shellArgs += ["--startup", jshellStartup]
6767
}
6868
else {
6969
def startupJsh = new File("${project.projectDir}/startup.jsh")
7070
if (startupJsh.exists()) {
7171
def startupJshPath = startupJsh.absolutePath
7272
jshellTask.logger.info(":jshell executing with --startup DEFAULT --startup PRINTING" +
7373
" --startup \"{}\"", startupJshPath)
74-
shellArgs = shellArgs + (String[]) ["--startup", startupJshPath]
74+
shellArgs += ["--startup", startupJshPath]
7575
} else {
7676
jshellTask.logger.info(":jshell did not find a startup.jsh script at the project dir " +
7777
"nor a `jshell.startup` configuration")
@@ -84,13 +84,15 @@ class JShellPlugin implements Plugin<Project> {
8484
@Override
8585
void apply(Project project) {
8686
Task jshellSetupTask = createJshellSetupTask(project)
87-
Task jshellTask = project.tasks.create('jshell')
88-
jshellTask.group = 'application'
89-
jshellTask.description = 'Runs a JShell session with all the code and dependencies.'
90-
jshellTask.dependsOn jshellSetupTask
91-
jshellTask.doLast {
92-
Thread.currentThread().setContextClassLoader(ClassLoader.getSystemClassLoader()) // promote class loader
93-
JavaShellToolBuilder.builder().run(shellArgs)
87+
project.tasks.register("jshell", Exec) {
88+
group = 'application'
89+
description = 'Runs a JShell session with all the code and dependencies.'
90+
dependsOn jshellSetupTask
91+
doFirst {
92+
standardInput = System.in
93+
executable = "jshell"
94+
args(shellArgs)
95+
}
9496
}
9597
}
9698
}

0 commit comments

Comments
 (0)