15
15
*/
16
16
package com.github.mrsarm.jshell.plugin
17
17
18
- import jdk.jshell.tool.JavaShellToolBuilder
19
18
import org.gradle.api.Plugin
20
19
import org.gradle.api.Project
21
20
import org.gradle.api.Task
21
+ import org.gradle.api.tasks.Exec
22
22
import org.gradle.api.tasks.JavaExec
23
23
24
24
class JShellPlugin implements Plugin<Project > {
25
25
26
- private String [] shellArgs
26
+ private shellArgs = []
27
27
28
28
// This task could be merged with the main task in just one,
29
29
// the problem is that the main task is a JavaExec one,
@@ -49,9 +49,9 @@ class JShellPlugin implements Plugin<Project> {
49
49
pathSet. addAll(classpath. findAll { it. exists() })
50
50
}
51
51
}
52
- def path = pathSet. join(System . properties[ ' os.name ' ] . toLowerCase() . contains( ' windows ' ) ? ' ; ' : ' : ' )
52
+ def path = pathSet. join(System . getProperty( " path.separator " ) )
53
53
jshellTask. logger. info(" :jshell executing with --class-path \" {}\" " , path)
54
- shellArgs = [
54
+ shellArgs + = [
55
55
" --class-path" , path,
56
56
" --startup" , " DEFAULT" ,
57
57
" --startup" , " PRINTING"
@@ -63,15 +63,15 @@ class JShellPlugin implements Plugin<Project> {
63
63
def jshellStartup = project. findProperty(" jshell.startup" )
64
64
jshellTask. logger. info(" :jshell executing with --startup DEFAULT --startup PRINTING " +
65
65
" --startup \" {}\" " , jshellStartup)
66
- shellArgs = shellArgs + ( String []) [" --startup" , jshellStartup]
66
+ shellArgs + = [" --startup" , jshellStartup]
67
67
}
68
68
else {
69
69
def startupJsh = new File (" ${ project.projectDir} /startup.jsh" )
70
70
if (startupJsh. exists()) {
71
71
def startupJshPath = startupJsh. absolutePath
72
72
jshellTask. logger. info(" :jshell executing with --startup DEFAULT --startup PRINTING" +
73
73
" --startup \" {}\" " , startupJshPath)
74
- shellArgs = shellArgs + ( String []) [" --startup" , startupJshPath]
74
+ shellArgs + = [" --startup" , startupJshPath]
75
75
} else {
76
76
jshellTask. logger. info(" :jshell did not find a startup.jsh script at the project dir " +
77
77
" nor a `jshell.startup` configuration" )
@@ -84,13 +84,15 @@ class JShellPlugin implements Plugin<Project> {
84
84
@Override
85
85
void apply (Project project ) {
86
86
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
+ }
94
96
}
95
97
}
96
98
}
0 commit comments