1
1
package cc.unitmesh.devti.provider
2
2
3
- import com.intellij.build.process.BuildProcessHandler
4
- import com.intellij.execution.*
3
+ import com.intellij.execution.ExecutionManager
4
+ import com.intellij.execution.Executor
5
+ import com.intellij.execution.RunManager
5
6
import com.intellij.execution.configurations.RunConfiguration
6
7
import com.intellij.execution.configurations.RunProfile
7
8
import com.intellij.execution.executors.DefaultRunExecutor
8
- import com.intellij.execution.filters.TextConsoleBuilderFactory
9
- import com.intellij.execution.process.ProcessTerminatedListener
10
- import com.intellij.execution.ui.ConsoleView
9
+ import com.intellij.execution.runners.ExecutionEnvironmentBuilder
10
+ import com.intellij.openapi.actionSystem.DataContext
11
11
import com.intellij.openapi.diagnostic.Logger
12
12
import com.intellij.openapi.diagnostic.logger
13
13
import com.intellij.openapi.project.Project
14
- import com.intellij.openapi.util.Key
15
- import com.intellij.openapi.util.UserDataHolderBase
16
- import com.intellij.openapi.util.io.StreamUtil
17
14
import com.intellij.openapi.vfs.VirtualFile
18
- import java.io.*
19
- import java.nio.channels.Channels
20
- import java.nio.channels.Pipe
21
15
22
16
interface RunService {
23
17
private val logger: Logger get() = logger<RunService >()
@@ -34,27 +28,27 @@ interface RunService {
34
28
35
29
fun createConfiguration (project : Project , path : String ): RunConfiguration ? = null
36
30
37
- fun runFile (project : Project , virtualFile : VirtualFile ): DefaultExecutionResult ? {
31
+ fun runFile (project : Project , virtualFile : VirtualFile ): String ? {
38
32
val runManager = RunManager .getInstance(project)
39
- var runConfig = runManager.allConfigurationsList.firstOrNull {
33
+ var testConfig = runManager.allConfigurationsList.firstOrNull {
40
34
val runConfigureClass = runConfigurationClass(project)
41
35
it.name == virtualFile.nameWithoutExtension && (it.javaClass == runConfigureClass)
42
36
}
43
37
44
38
var isTemporary = false
45
39
46
40
// try to create config if not founds
47
- if (runConfig == null ) {
41
+ if (testConfig == null ) {
48
42
isTemporary = true
49
- runConfig = createConfiguration(project, virtualFile)
43
+ testConfig = createConfiguration(project, virtualFile)
50
44
}
51
45
52
- if (runConfig == null ) {
46
+ if (testConfig == null ) {
53
47
logger.warn(" Failed to find test configuration for: ${virtualFile.nameWithoutExtension} " )
54
48
return null
55
49
}
56
50
57
- val settings = runManager.findConfigurationByTypeAndName(runConfig .type, runConfig .name)
51
+ val settings = runManager.findConfigurationByTypeAndName(testConfig .type, testConfig .name)
58
52
if (settings == null ) {
59
53
logger.warn(" Failed to find test configuration for: ${virtualFile.nameWithoutExtension} " )
60
54
return null
@@ -64,85 +58,14 @@ interface RunService {
64
58
settings.isTemporary = true
65
59
}
66
60
67
- logger.info(" configurationSettings: $settings " )
68
61
runManager.selectedConfiguration = settings
69
62
70
- val executor: Executor = DefaultRunExecutor ()
71
- val executionManager = ExecutionManager .getInstance(project)
72
- val dataHolderBase = UserDataHolderBase ()
73
- val processHandler = RunServiceHandler (settings.name, dataHolderBase)
63
+ val executor: Executor = DefaultRunExecutor .getRunExecutorInstance()
64
+ val builder = ExecutionEnvironmentBuilder .createOrNull(executor, settings) ? : return null
74
65
75
- executionManager
76
- .restartRunProfile(
77
- project,
78
- executor,
79
- DefaultExecutionTarget .INSTANCE ,
80
- settings,
81
- processHandler
82
- )
66
+ val environment = builder.activeTarget().dataContext(DataContext .EMPTY_CONTEXT ).build()
67
+ ExecutionManager .getInstance(project).restartRunProfile(environment)
83
68
84
- val consoleView: ConsoleView = TextConsoleBuilderFactory .getInstance().createBuilder(project).console
85
- ProcessTerminatedListener .attach(processHandler)
86
- consoleView.attachToProcess(processHandler)
87
-
88
- val defaultExecutionResult = DefaultExecutionResult (consoleView, processHandler)
89
-
90
- return defaultExecutionResult
91
- }
92
- }
93
-
94
- class RunServiceHandler (val myExecutionName : String , private val myDataHolder : UserDataHolderBase ) :
95
- BuildProcessHandler () {
96
- private var myProcessInputWriter: OutputStream ? = null
97
- private var myProcessInputReader: InputStream ? = null
98
-
99
- init {
100
- try {
101
- val pipe = Pipe .open()
102
- myProcessInputReader = BufferedInputStream (Channels .newInputStream(pipe.source()))
103
- myProcessInputWriter = BufferedOutputStream (Channels .newOutputStream(pipe.sink()))
104
- } catch (e: IOException ) {
105
- logger<RunService >().warn(" Unable to setup process input" , e)
106
- }
107
-
108
- myDataHolder.putUserData(RUN_INPUT_KEY , myProcessInputReader)
109
- }
110
-
111
- override fun notifyTextAvailable (text : String , outputType : Key <* >) {
112
- super .notifyTextAvailable(text, outputType)
113
- logger<RunService >().warn(" notifyTextAvailable: $text " )
114
- }
115
-
116
- override fun detachIsDefault (): Boolean = false
117
- override fun destroyProcessImpl () {
118
- notifyProcessDetached()
119
- closeInput()
120
- }
121
-
122
- override fun detachProcessImpl () {
123
- closeInput()
124
- notifyProcessTerminated(0 )
125
- }
126
-
127
- override fun getProcessInput (): OutputStream {
128
- return myProcessInputWriter!!
129
- }
130
-
131
- protected fun closeInput () {
132
- val processInputWriter = myProcessInputWriter!!
133
- val processInputReader = myProcessInputReader!!
134
- myProcessInputWriter = null
135
- myProcessInputReader = null
136
-
137
- myDataHolder.putUserData(RUN_INPUT_KEY , null )
138
-
139
- StreamUtil .closeStream(processInputWriter)
140
- StreamUtil .closeStream(processInputReader)
141
- }
142
-
143
- override fun getExecutionName (): String = myExecutionName
144
-
145
- companion object {
146
- val RUN_INPUT_KEY : Key <InputStream > = Key .create(" RUN_INPUT_KEY" )
69
+ return null
147
70
}
148
71
}
0 commit comments