1
1
package com.intellij.plugins.phpstorm_dd
2
2
3
3
import com.intellij.openapi.Disposable
4
- import com.intellij.openapi.components.ServiceManager
5
- import com.intellij.openapi.externalSystem.service.ui.completion.collector.TextCompletionCollector.Companion.async
6
4
import com.intellij.openapi.project.Project
7
- import com.intellij.platform.ijent.IjentProcessWatcher.Companion.launch
5
+ import com.intellij.ui.jcef.JBCefBrowser
8
6
import java.io.File
9
7
import com.jetbrains.php.config.interpreters.PhpInterpreter
10
- import com.jetbrains.rd.generator.nova.PredefinedType
11
8
import kotlinx.coroutines.*
9
+ import kotlinx.coroutines.channels.Channel
12
10
import java.io.InputStream
13
- import java.util.concurrent.TimeUnit
14
11
15
12
class TrapServerService (
16
13
private var project : Project ?
17
14
) : Disposable {
18
15
19
- var trapDaemon: Job ? = null
16
+ var webview: JBCefBrowser ? = null
17
+ var trapDaemon: Process ? = null
20
18
21
19
override fun dispose () {
22
20
if (trapDaemon != null ) {
23
- trapDaemon!! .cancel ()
21
+ trapDaemon!! .destroy ()
24
22
trapDaemon = null
25
23
}
26
24
}
27
-
28
- fun startTrapServer (interpreter : PhpInterpreter ) {
25
+
26
+ // private var processChannel: Channel<Process?>? = null
27
+ private var processChannel = Channel <Process ?>()
28
+
29
+ suspend fun startTrapServer (interpreter : PhpInterpreter ) {
29
30
30
- if (trapDaemon?.isActive == true ) {
31
+ if (trapDaemon?.isAlive == true ) {
31
32
return
32
33
}
33
34
34
35
val cmdString = " %s %s/vendor/bin/trap --ui" .format(interpreter.pathToPhpExecutable, project?.basePath)
35
36
val cmdMap = cmdString.split(" " )
36
37
val workingDir = File (project?.basePath!! )
37
38
38
- // trapDaemon = ProcessBuilder(cmdMap)
39
- // .directory(workingDir)
40
- // .redirectOutput(ProcessBuilder.Redirect.INHERIT)
41
- // .redirectError(ProcessBuilder.Redirect.INHERIT)
42
- // .start()
43
-
44
-
45
- trapDaemon = CoroutineScope (Dispatchers .IO ).launch {
39
+ CoroutineScope (Dispatchers .IO ).launch {
46
40
executeCommand(cmdMap, workingDir)
47
41
}
42
+
43
+ trapDaemon = processChannel.receive()
48
44
49
45
println (" here" )
50
46
}
51
47
52
- suspend fun stopTrapServer () {
48
+ fun stopTrapServer () {
53
49
54
- if (trapDaemon == null || ! trapDaemon!! .isActive ) {
50
+ if (trapDaemon == null || ! trapDaemon!! .isAlive ) {
55
51
return
56
52
}
57
-
58
- trapDaemon!! .cancel()
59
- trapDaemon!! .join()
53
+
54
+ trapDaemon?.destroy()
55
+ }
56
+
57
+ fun statusChanged () {
58
+ if (trapDaemon == null || ! trapDaemon!! .isAlive) {
59
+ webview?.loadHTML(" Not Running" );
60
+ } else {
61
+ webview?.loadURL(" http://127.0.0.1:8000" )
62
+ }
60
63
}
61
64
62
65
private suspend fun executeCommand (commandArgs : List <String >, workingDir : File ): CoroutineScope = withContext(
63
66
Dispatchers .IO
64
67
) {
68
+ var process: Process ? = null
65
69
runCatching {
66
- val process = ProcessBuilder (commandArgs)
70
+ process = ProcessBuilder (commandArgs)
67
71
.directory(workingDir)
72
+ .redirectOutput(ProcessBuilder .Redirect .INHERIT )
73
+ .redirectError(ProcessBuilder .Redirect .INHERIT )
68
74
.start()
75
+
76
+ processChannel.send(process)
77
+
69
78
val outputStream = async {
70
- println (" Context for output stream -> $coroutineContext -> Thread -> ${Thread .currentThread()} " )
71
- readStream(process.inputStream) }
79
+ readStream(process!! .inputStream) }
72
80
val errorStream = async {
73
- println (" Context for error stream -> $coroutineContext -> Thread -> ${Thread .currentThread()} " )
74
- readStream(process.errorStream)
81
+ readStream(process!! .errorStream)
75
82
}
76
- println ( " Context for exit code -> $coroutineContext -> Thread -> ${ Thread .currentThread()} " )
77
- val exitCode = process.waitFor()
83
+ val exitCode = process?.waitFor( )
84
+
78
85
ProcessResult (
79
- exitCode = exitCode,
86
+ exitCode = exitCode!! ,
80
87
message = outputStream.await(),
81
88
errorMessage = errorStream.await()
82
89
)
@@ -92,14 +99,8 @@ class TrapServerService(
92
99
return @withContext this
93
100
}
94
101
95
- private fun readStream (inputStream : InputStream ) =
96
- inputStream.bufferedReader().use { reader -> reader.readText() }
97
-
98
102
data class ProcessResult (val exitCode : Int , val message : String , val errorMessage : String )
99
103
100
- companion object {
101
- fun getInstance (project : Project ): TrapServerService {
102
- return ServiceManager .getService(project, TrapServerService ::class .java)
103
- }
104
- }
104
+ private fun readStream (inputStream : InputStream ) =
105
+ inputStream.bufferedReader().use { reader -> reader.readText() }
105
106
}
0 commit comments