|
1 | 1 | import com.github.gradle.node.npm.task.NpmTask |
2 | | -import kotlinx.coroutines.CoroutineScope |
3 | | -import kotlinx.coroutines.Dispatchers |
4 | | -import kotlinx.coroutines.joinAll |
5 | | -import kotlinx.coroutines.launch |
6 | | -import kotlinx.coroutines.runBlocking |
7 | 2 | import java.io.FileNotFoundException |
8 | | -import java.io.Reader |
9 | | -import kotlin.time.Duration |
10 | 3 | import kotlin.time.Duration.Companion.minutes |
11 | 4 |
|
12 | 5 | plugins { |
@@ -117,72 +110,3 @@ tasks.register("generateTestResources") { |
117 | 110 | ) |
118 | 111 | } |
119 | 112 | } |
120 | | - |
121 | | -object ProcessUtil { |
122 | | - data class Result( |
123 | | - val exitCode: Int, |
124 | | - val stdout: String, |
125 | | - val stderr: String, |
126 | | - val isTimeout: Boolean, // true if the process was terminated due to timeout |
127 | | - ) |
128 | | - |
129 | | - fun run( |
130 | | - command: List<String>, |
131 | | - input: Reader = "".reader(), |
132 | | - timeout: Duration? = null, |
133 | | - builder: ProcessBuilder.() -> Unit = {}, |
134 | | - ): Result { |
135 | | - val process = ProcessBuilder(command).apply(builder).start() |
136 | | - return communicate(process, input, timeout) |
137 | | - } |
138 | | - |
139 | | - private fun communicate( |
140 | | - process: Process, |
141 | | - input: Reader, |
142 | | - timeout: Duration? = null, |
143 | | - ): Result { |
144 | | - val stdout = StringBuilder() |
145 | | - val stderr = StringBuilder() |
146 | | - |
147 | | - val scope = CoroutineScope(Dispatchers.IO) |
148 | | - |
149 | | - // Handle process input |
150 | | - val stdinJob = scope.launch { |
151 | | - process.outputStream.bufferedWriter().use { writer -> |
152 | | - input.copyTo(writer) |
153 | | - } |
154 | | - } |
155 | | - |
156 | | - // Launch output capture coroutines |
157 | | - val stdoutJob = scope.launch { |
158 | | - process.inputStream.bufferedReader().useLines { lines -> |
159 | | - lines.forEach { stdout.appendLine(it) } |
160 | | - } |
161 | | - } |
162 | | - val stderrJob = scope.launch { |
163 | | - process.errorStream.bufferedReader().useLines { lines -> |
164 | | - lines.forEach { stderr.appendLine(it) } |
165 | | - } |
166 | | - } |
167 | | - |
168 | | - // Wait for completion |
169 | | - val isTimeout = if (timeout != null) { |
170 | | - !process.waitFor(timeout.inWholeNanoseconds, TimeUnit.NANOSECONDS) |
171 | | - } else { |
172 | | - process.waitFor() |
173 | | - false |
174 | | - } |
175 | | - |
176 | | - // Wait for all coroutines to finish |
177 | | - runBlocking { |
178 | | - joinAll(stdinJob, stdoutJob, stderrJob) |
179 | | - } |
180 | | - |
181 | | - return Result( |
182 | | - exitCode = process.exitValue(), |
183 | | - stdout = stdout.toString(), |
184 | | - stderr = stderr.toString(), |
185 | | - isTimeout = isTimeout, |
186 | | - ) |
187 | | - } |
188 | | -} |
0 commit comments