|
| 1 | +package cc.unitmesh.devti.language.compiler.exec |
| 2 | + |
| 3 | +import cc.unitmesh.devti.language.utils.lookupFile |
| 4 | +import com.intellij.execution.DefaultExecutionResult |
| 5 | +import com.intellij.execution.ExecutionException |
| 6 | +import com.intellij.execution.configurations.GeneralCommandLine |
| 7 | +import com.intellij.execution.configurations.PtyCommandLine |
| 8 | +import com.intellij.execution.process.KillableProcessHandler |
| 9 | +import com.intellij.execution.process.ProcessHandler |
| 10 | +import com.intellij.execution.process.ProcessTerminatedListener |
| 11 | +import com.intellij.execution.ui.ConsoleView |
| 12 | +import com.intellij.openapi.application.ApplicationManager |
| 13 | +import com.intellij.openapi.project.Project |
| 14 | +import com.intellij.sh.run.ShConfigurationType |
| 15 | +import com.intellij.sh.run.ShRunner |
| 16 | +import com.intellij.terminal.TerminalExecutionConsole |
| 17 | +import com.intellij.util.io.BaseOutputReader |
| 18 | + |
| 19 | +class ShellInsCommand(val myProject: Project, val prop: String) : InsCommand { |
| 20 | + override fun execute(): String? { |
| 21 | + val virtualFile = myProject.lookupFile(prop.trim()) ?: return "<DevInsError>: File not found: $prop" |
| 22 | + |
| 23 | + val workingDirectory = virtualFile.parent.path |
| 24 | + val shRunner = ApplicationManager.getApplication().getService( |
| 25 | + ShRunner::class.java |
| 26 | + ) |
| 27 | + if (shRunner != null && shRunner.isAvailable(myProject)) { |
| 28 | + shRunner.run(myProject, virtualFile.path, workingDirectory, "RunDevInsShell", true) |
| 29 | + } |
| 30 | + |
| 31 | + // TODO: after run done |
| 32 | +// runInTerminal(virtualFile.path, workingDirectory, myProject) |
| 33 | + |
| 34 | + return "" |
| 35 | + } |
| 36 | + |
| 37 | + @Throws(ExecutionException::class) |
| 38 | + private fun runInTerminal(command: String, workingDirectory: String, project: Project): DefaultExecutionResult { |
| 39 | + val commandLine = createCommandLineForScript(project, workingDirectory, command) |
| 40 | + val processHandler = createProcessHandler(commandLine) |
| 41 | + ProcessTerminatedListener.attach(processHandler) |
| 42 | + val console: ConsoleView = TerminalExecutionConsole(project, processHandler) |
| 43 | + console.attachToProcess(processHandler) |
| 44 | + return DefaultExecutionResult(console, processHandler) |
| 45 | + } |
| 46 | + |
| 47 | + private fun createCommandLineForScript( |
| 48 | + project: Project, |
| 49 | + workingDirectory: String, |
| 50 | + command: String |
| 51 | + ): GeneralCommandLine { |
| 52 | + val commandLine = PtyCommandLine() |
| 53 | + commandLine.withConsoleMode(false) |
| 54 | + commandLine.withInitialColumns(120) |
| 55 | + commandLine.withParentEnvironmentType(GeneralCommandLine.ParentEnvironmentType.CONSOLE) |
| 56 | + commandLine.setWorkDirectory(workingDirectory) |
| 57 | + commandLine.withExePath(ShConfigurationType.getDefaultShell(project)) |
| 58 | + commandLine.withParameters("-c") |
| 59 | + commandLine.withParameters(command) |
| 60 | + return commandLine |
| 61 | + } |
| 62 | + |
| 63 | + @Throws(ExecutionException::class) |
| 64 | + private fun createProcessHandler(commandLine: GeneralCommandLine): ProcessHandler { |
| 65 | + return object : KillableProcessHandler(commandLine) { |
| 66 | + override fun readerOptions(): BaseOutputReader.Options { |
| 67 | + return BaseOutputReader.Options.forTerminalPtyProcess() |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | +} |
0 commit comments