|
| 1 | +/* |
| 2 | + * Copyright (C) 2020 The Android Open Source Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package cc.unitmesh.devti.language.compiler.service |
| 17 | + |
| 18 | +import com.intellij.execution.configurations.PtyCommandLine |
| 19 | +import com.intellij.openapi.project.ProjectManager |
| 20 | +import com.intellij.openapi.util.text.Strings |
| 21 | +import kotlinx.coroutines.CoroutineDispatcher |
| 22 | +import kotlinx.coroutines.async |
| 23 | +import kotlinx.coroutines.ensureActive |
| 24 | +import kotlinx.coroutines.withContext |
| 25 | +import kotlinx.coroutines.yield |
| 26 | +import java.io.BufferedReader |
| 27 | +import java.io.InputStream |
| 28 | +import java.io.InputStreamReader |
| 29 | +import java.io.OutputStream |
| 30 | +import java.io.Writer |
| 31 | +import kotlin.text.Charsets.UTF_8 |
| 32 | + |
| 33 | +object ProcessExecutor { |
| 34 | + suspend fun exec(shellScript: String, |
| 35 | + stdWriter: Writer, |
| 36 | + errWriter: Writer, |
| 37 | + dispatcher: CoroutineDispatcher): Int = withContext(dispatcher) { |
| 38 | + val process = createProcess(shellScript) |
| 39 | + |
| 40 | + |
| 41 | + val errOutput = async { consumeProcessOutput(process.errorStream, errWriter, process, dispatcher) } |
| 42 | + val stdOutput = async { consumeProcessOutput(process.inputStream, stdWriter, process, dispatcher) } |
| 43 | + |
| 44 | + val exitCode = async { process.waitFor() } |
| 45 | + stdOutput.await() |
| 46 | + errOutput.await() |
| 47 | + exitCode.await() |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * for share process |
| 52 | + */ |
| 53 | + fun createInteractiveProcess(): Process { |
| 54 | + val commandLine = PtyCommandLine() |
| 55 | + commandLine.withConsoleMode(false) |
| 56 | + commandLine.withUnixOpenTtyToPreserveOutputAfterTermination(true) |
| 57 | + commandLine.withInitialColumns(240) |
| 58 | + commandLine.withInitialRows(80) |
| 59 | + commandLine.withEnvironment("TERM", "dumb") |
| 60 | + commandLine.withEnvironment("BASH_SILENCE_DEPRECATION_WARNING", "1") |
| 61 | + commandLine.withEnvironment("GIT_PAGER", "cat") |
| 62 | + val commands: List<String> = listOf("bash", "--noprofile", "--norc", "-i") |
| 63 | + return commandLine.startProcessWithPty(commands) |
| 64 | + } |
| 65 | + |
| 66 | + private fun createProcess(shellScript: String): Process { |
| 67 | + val basedir = ProjectManager.getInstance().openProjects.firstOrNull()?.basePath |
| 68 | + val commandLine = PtyCommandLine() |
| 69 | + commandLine.withConsoleMode(false) |
| 70 | +// commandLine.withExePath("/bin/bash") |
| 71 | +// .withParameters("-c", formatCommand(shellScript)) |
| 72 | +// .withCharset(UTF_8) |
| 73 | +// .withRedirectErrorStream(true) |
| 74 | + |
| 75 | + commandLine.withUnixOpenTtyToPreserveOutputAfterTermination(true) |
| 76 | + commandLine.withInitialColumns(240) |
| 77 | + commandLine.withInitialRows(80) |
| 78 | + commandLine.withEnvironment("TERM", "dumb") |
| 79 | + commandLine.withEnvironment("BASH_SILENCE_DEPRECATION_WARNING", "1") |
| 80 | + commandLine.withEnvironment("GIT_PAGER", "cat") |
| 81 | + val commands: List<String> = listOf("bash", "--noprofile", "--norc", "-c", formatCommand(shellScript)) |
| 82 | + |
| 83 | + |
| 84 | + if (basedir != null) { |
| 85 | + commandLine.withWorkDirectory(basedir) |
| 86 | + } |
| 87 | + |
| 88 | + return commandLine.startProcessWithPty(commands) |
| 89 | + } |
| 90 | + |
| 91 | + |
| 92 | + private fun formatCommand(command: String): String { |
| 93 | + return "{ $command; EXIT_CODE=$?; } 2>&1 && echo \"EXIT_CODE: ${'$'}EXIT_CODE\"" |
| 94 | + } |
| 95 | + |
| 96 | + // Feeds input lines to the process' outputStream |
| 97 | + private fun feedProcessInput(outputStream: OutputStream, inputLines: List<String>) { |
| 98 | + outputStream.writer(UTF_8).use { writer -> |
| 99 | + inputLines.forEach { line -> |
| 100 | + writer.write(line + System.lineSeparator()) |
| 101 | + writer.flush() |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + // Consumes output stream as the process is being executed - otherwise on Windows the process would block when the output buffer is full. |
| 107 | + private suspend fun consumeProcessOutput(source: InputStream?, |
| 108 | + outputWriter: Writer, |
| 109 | + process: Process, |
| 110 | + dispatcher: CoroutineDispatcher) = withContext(dispatcher) { |
| 111 | + if (source == null) return@withContext |
| 112 | + |
| 113 | + var isFirstLine = true |
| 114 | + BufferedReader(InputStreamReader(source, UTF_8.name())).use { reader -> |
| 115 | + do { |
| 116 | + val line = reader.readLine() |
| 117 | + if (Strings.isNotEmpty(line)) { |
| 118 | + if (!isFirstLine) outputWriter.append(System.lineSeparator()) |
| 119 | + isFirstLine = false |
| 120 | + outputWriter.append(line) |
| 121 | + } |
| 122 | + else { |
| 123 | + yield() |
| 124 | + } |
| 125 | + ensureActive() |
| 126 | + } |
| 127 | + while (process.isAlive || line != null) |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments