2023-03-26 22:27:51 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace SPC\util;
|
|
|
|
|
|
2025-06-09 10:24:06 +08:00
|
|
|
use SPC\builder\freebsd\library\BSDLibraryBase;
|
|
|
|
|
use SPC\builder\linux\library\LinuxLibraryBase;
|
|
|
|
|
use SPC\builder\macos\library\MacOSLibraryBase;
|
2023-03-26 22:27:51 +08:00
|
|
|
use SPC\exception\RuntimeException;
|
|
|
|
|
use ZM\Logger\ConsoleColor;
|
|
|
|
|
|
|
|
|
|
class UnixShell
|
|
|
|
|
{
|
|
|
|
|
private ?string $cd = null;
|
|
|
|
|
|
|
|
|
|
private bool $debug;
|
|
|
|
|
|
2023-04-29 18:59:47 +08:00
|
|
|
private array $env = [];
|
|
|
|
|
|
2024-05-16 10:51:31 +08:00
|
|
|
/**
|
|
|
|
|
* @throws RuntimeException
|
|
|
|
|
*/
|
2023-04-22 21:23:12 +08:00
|
|
|
public function __construct(?bool $debug = null)
|
2023-03-26 22:27:51 +08:00
|
|
|
{
|
2023-12-24 20:17:06 +08:00
|
|
|
if (PHP_OS_FAMILY === 'Windows') {
|
|
|
|
|
throw new RuntimeException('Windows cannot use UnixShell');
|
|
|
|
|
}
|
2023-04-22 21:23:12 +08:00
|
|
|
$this->debug = $debug ?? defined('DEBUG_MODE');
|
2023-03-26 22:27:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function cd(string $dir): UnixShell
|
|
|
|
|
{
|
|
|
|
|
logger()->info('Entering dir: ' . $dir);
|
|
|
|
|
$c = clone $this;
|
|
|
|
|
$c->cd = $dir;
|
|
|
|
|
return $c;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @throws RuntimeException
|
|
|
|
|
*/
|
|
|
|
|
public function exec(string $cmd): UnixShell
|
|
|
|
|
{
|
|
|
|
|
/* @phpstan-ignore-next-line */
|
|
|
|
|
logger()->info(ConsoleColor::yellow('[EXEC] ') . ConsoleColor::green($cmd));
|
2024-07-09 00:44:03 +08:00
|
|
|
logger()->debug('Executed at: ' . debug_backtrace()[0]['file'] . ':' . debug_backtrace()[0]['line']);
|
2023-03-26 22:27:51 +08:00
|
|
|
if ($this->cd !== null) {
|
|
|
|
|
$cmd = 'cd ' . escapeshellarg($this->cd) . ' && ' . $cmd;
|
|
|
|
|
}
|
|
|
|
|
if (!$this->debug) {
|
|
|
|
|
$cmd .= ' 1>/dev/null 2>&1';
|
|
|
|
|
}
|
2025-06-09 10:24:06 +08:00
|
|
|
$env_str = $this->getEnvString();
|
|
|
|
|
if (!empty($env_str)) {
|
|
|
|
|
$env_str = "{$env_str} ";
|
|
|
|
|
}
|
|
|
|
|
f_passthru("{$env_str}{$cmd}");
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Init the environment variable that common build will be used.
|
|
|
|
|
*
|
|
|
|
|
* @param BSDLibraryBase|LinuxLibraryBase|MacOSLibraryBase $library Library class
|
|
|
|
|
*/
|
2025-06-09 11:12:34 +08:00
|
|
|
public function initializeEnv(BSDLibraryBase|LinuxLibraryBase|MacOSLibraryBase $library): UnixShell
|
2025-06-09 10:24:06 +08:00
|
|
|
{
|
|
|
|
|
$this->setEnv([
|
|
|
|
|
'CFLAGS' => $library->getLibExtraCFlags(),
|
|
|
|
|
'LDFLAGS' => $library->getLibExtraLdFlags(),
|
|
|
|
|
'LIBS' => $library->getLibExtraLibs(),
|
|
|
|
|
]);
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function appendEnv(array $env): UnixShell
|
|
|
|
|
{
|
|
|
|
|
foreach ($env as $k => $v) {
|
|
|
|
|
if ($v === '') {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (!isset($this->env[$k])) {
|
|
|
|
|
$this->env[$k] = $v;
|
|
|
|
|
} else {
|
|
|
|
|
$this->env[$k] = "{$v} {$this->env[$k]}";
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-26 22:27:51 +08:00
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function execWithResult(string $cmd, bool $with_log = true): array
|
|
|
|
|
{
|
|
|
|
|
if ($with_log) {
|
|
|
|
|
/* @phpstan-ignore-next-line */
|
|
|
|
|
logger()->info(ConsoleColor::blue('[EXEC] ') . ConsoleColor::green($cmd));
|
|
|
|
|
} else {
|
2024-07-09 00:44:03 +08:00
|
|
|
/* @phpstan-ignore-next-line */
|
|
|
|
|
logger()->debug(ConsoleColor::blue('[EXEC] ') . ConsoleColor::gray($cmd));
|
2023-03-26 22:27:51 +08:00
|
|
|
}
|
2024-07-09 00:44:03 +08:00
|
|
|
logger()->debug('Executed at: ' . debug_backtrace()[0]['file'] . ':' . debug_backtrace()[0]['line']);
|
2024-12-10 23:08:01 +08:00
|
|
|
if ($this->cd !== null) {
|
|
|
|
|
$cmd = 'cd ' . escapeshellarg($this->cd) . ' && ' . $cmd;
|
|
|
|
|
}
|
2023-03-26 22:27:51 +08:00
|
|
|
exec($cmd, $out, $code);
|
|
|
|
|
return [$code, $out];
|
|
|
|
|
}
|
2023-04-29 18:59:47 +08:00
|
|
|
|
|
|
|
|
public function setEnv(array $env): UnixShell
|
|
|
|
|
{
|
2024-04-07 15:52:24 +08:00
|
|
|
foreach ($env as $k => $v) {
|
|
|
|
|
if ($v === '') {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$this->env[$k] = $v;
|
|
|
|
|
}
|
2023-04-29 18:59:47 +08:00
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function getEnvString(): string
|
|
|
|
|
{
|
|
|
|
|
$str = '';
|
|
|
|
|
foreach ($this->env as $k => $v) {
|
|
|
|
|
$str .= ' ' . $k . '="' . $v . '"';
|
|
|
|
|
}
|
|
|
|
|
return trim($str);
|
|
|
|
|
}
|
2023-03-26 22:27:51 +08:00
|
|
|
}
|