|
5 | 5 | use Psr\Log\LoggerInterface;
|
6 | 6 | use SPC\builder\BuilderBase;
|
7 | 7 | use SPC\builder\BuilderProvider;
|
| 8 | +use SPC\exception\ExecutionException; |
8 | 9 | use SPC\exception\InterruptException;
|
9 |
| -use SPC\exception\RuntimeException; |
10 | 10 | use SPC\exception\WrongUsageException;
|
11 | 11 | use SPC\util\shell\UnixShell;
|
12 | 12 | use SPC\util\shell\WindowsCmd;
|
@@ -165,7 +165,7 @@ function f_passthru(string $cmd): ?bool
|
165 | 165 | }
|
166 | 166 | $ret = passthru($cmd, $code);
|
167 | 167 | if ($code !== 0) {
|
168 |
| - throw new RuntimeException('Command run failed with code[' . $code . ']: ' . $cmd, $code); |
| 168 | + throw new ExecutionException($cmd, "Direct command run failed with code: {$code}", $code); |
169 | 169 | }
|
170 | 170 | return $ret;
|
171 | 171 | }
|
@@ -203,7 +203,7 @@ function f_putenv(string $env): bool
|
203 | 203 | function get_cmake_version(): ?string
|
204 | 204 | {
|
205 | 205 | try {
|
206 |
| - [,$output] = shell()->execWithResult('cmake --version', false); |
| 206 | + [,$output] = shell(false)->execWithResult('cmake --version', false); |
207 | 207 | if (preg_match('/cmake version ([\d.]+)/i', $output[0], $matches)) {
|
208 | 208 | return $matches[1];
|
209 | 209 | }
|
@@ -244,3 +244,42 @@ function clean_spaces(string $string): string
|
244 | 244 | {
|
245 | 245 | return trim(preg_replace('/\s+/', ' ', $string));
|
246 | 246 | }
|
| 247 | + |
| 248 | +/** |
| 249 | + * Register a callback function to handle keyboard interrupts (Ctrl+C). |
| 250 | + * |
| 251 | + * @param callable $callback callback function to handle keyboard interrupts |
| 252 | + */ |
| 253 | +function keyboard_interrupt_register(callable $callback): void |
| 254 | +{ |
| 255 | + if (PHP_OS_FAMILY === 'Windows') { |
| 256 | + sapi_windows_set_ctrl_handler($callback); |
| 257 | + } elseif (extension_loaded('pcntl')) { |
| 258 | + pcntl_signal(SIGINT, $callback); |
| 259 | + } |
| 260 | +} |
| 261 | + |
| 262 | +/** |
| 263 | + * Unregister the keyboard interrupt handler. |
| 264 | + * |
| 265 | + * This function is used to remove the previously registered keyboard interrupt handler. |
| 266 | + * It should be called when you no longer need to handle keyboard interrupts. |
| 267 | + */ |
| 268 | +function keyboard_interrupt_unregister(): void |
| 269 | +{ |
| 270 | + if (PHP_OS_FAMILY === 'Windows') { |
| 271 | + sapi_windows_set_ctrl_handler(null); |
| 272 | + } elseif (extension_loaded('pcntl')) { |
| 273 | + pcntl_signal(SIGINT, SIG_IGN); |
| 274 | + } |
| 275 | +} |
| 276 | + |
| 277 | +/** |
| 278 | + * Strip ANSI color codes from a string. |
| 279 | + */ |
| 280 | +function strip_ansi_colors(string $text): string |
| 281 | +{ |
| 282 | + // Regular expression to match ANSI escape sequences |
| 283 | + // Including color codes, cursor control, clear screen and other control sequences |
| 284 | + return preg_replace('/\e\[[0-9;]*[a-zA-Z]/', '', $text); |
| 285 | +} |
0 commit comments