Skip to content

Commit 91d9728

Browse files
committed
feat: support remote interpreters
1 parent d3d4868 commit 91d9728

File tree

1 file changed

+52
-14
lines changed

1 file changed

+52
-14
lines changed

src/main/kotlin/com/github/xepozz/php_dump/command/PhpCommandExecutor.kt

Lines changed: 52 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
package com.github.xepozz.php_dump.command
22

3+
import com.intellij.execution.ExecutionException
34
import com.intellij.execution.configurations.GeneralCommandLine
45
import com.intellij.execution.process.KillableColoredProcessHandler
56
import com.intellij.execution.process.ProcessAdapter
67
import com.intellij.execution.process.ProcessEvent
8+
import com.intellij.execution.process.ProcessHandler
79
import com.intellij.openapi.project.Project
10+
import com.intellij.openapi.util.text.StringUtil
811
import com.jetbrains.php.config.PhpProjectConfigurationFacade
12+
import com.jetbrains.php.config.commandLine.PhpCommandSettingsBuilder
913
import com.jetbrains.php.config.interpreters.PhpInterpretersManagerImpl
14+
import com.jetbrains.php.run.remote.PhpRemoteInterpreterManager
1015
import kotlin.coroutines.suspendCoroutine
1116

1217
object PhpCommandExecutor {
@@ -17,32 +22,65 @@ object PhpCommandExecutor {
1722
processListener: ProcessAdapter,
1823
processArguments: List<String> = emptyList()
1924
) {
20-
val interpretersManager = PhpInterpretersManagerImpl.getInstance(project)
21-
val interpreter = PhpProjectConfigurationFacade.getInstance(project).interpreter
22-
?: interpretersManager.interpreters.firstOrNull() ?: return
25+
println("run php command $file, $phpSnippet")
2326

24-
val interpreterPath = interpreter.pathToPhpExecutable ?: return
25-
val commandArgs = buildList {
26-
add(interpreterPath)
27+
val arguments = buildList {
2728
addAll(processArguments)
2829
add("-r")
2930
add(phpSnippet)
3031

3132
add(file)
3233
}
3334

34-
executeCommand(commandArgs, processListener)
35+
executeCommand(project, arguments, processListener)
3536
}
3637

37-
private suspend fun executeCommand(commandArgs: List<String>, processListener: ProcessAdapter) =
38+
private suspend fun executeCommand(project: Project, arguments: List<String>, processListener: ProcessAdapter) =
3839
suspendCoroutine<Int> { continuation ->
39-
val command = GeneralCommandLine(commandArgs)
40-
command.withRedirectErrorStream(false)
40+
val interpretersManager = PhpInterpretersManagerImpl.getInstance(project)
41+
val interpreter = PhpProjectConfigurationFacade.getInstance(project).interpreter
42+
?: interpretersManager.interpreters.firstOrNull() ?: return@suspendCoroutine
43+
44+
val executable = interpreter.pathToPhpExecutable!!
45+
46+
val command = GeneralCommandLine()
47+
.withExePath(executable)
48+
.apply { addParameters(arguments) }
49+
50+
val processHandler: ProcessHandler
51+
if (interpreter.isRemote) {
52+
val manager = PhpRemoteInterpreterManager.getInstance() ?: throw ExecutionException(
53+
PhpRemoteInterpreterManager.getRemoteInterpreterPluginIsDisabledErrorMessage()
54+
)
55+
56+
val data = interpreter.phpSdkAdditionalData
57+
val validate = data.validate(project, null)
58+
if (StringUtil.isNotEmpty(validate)) {
59+
throw ExecutionException(validate)
60+
}
61+
62+
val pathMapper = manager.createPathMapper(project, data)
63+
val phpCommandSettings = PhpCommandSettingsBuilder.create(executable, pathMapper, data)
64+
65+
val command = phpCommandSettings.createGeneralCommandLine(false)
66+
.withRedirectErrorStream(false)
67+
.apply { addParameters(arguments) }
68+
69+
println("cmd: ${command.commandLineString}")
70+
processHandler = manager.getRemoteProcessHandler(
71+
project,
72+
data,
73+
command,
74+
false,
75+
*phpCommandSettings.additionalMappings
76+
)
77+
} else {
78+
println("cmd: ${command.commandLineString}")
79+
processHandler = KillableColoredProcessHandler.Silent(command)
80+
processHandler.setShouldKillProcessSoftly(false)
81+
processHandler.setShouldDestroyProcessRecursively(true)
82+
}
4183

42-
// println("running command ${command.commandLineString}")
43-
val processHandler = KillableColoredProcessHandler.Silent(command)
44-
processHandler.setShouldKillProcessSoftly(false)
45-
processHandler.setShouldDestroyProcessRecursively(true)
4684
processHandler.addProcessListener(processListener)
4785
processHandler.addProcessListener(object : ProcessAdapter() {
4886
override fun processTerminated(event: ProcessEvent) {

0 commit comments

Comments
 (0)