mirror of
https://github.com/crazywhalecc/static-php-cli.git
synced 2026-07-09 01:45:36 +08:00
Enhance Windows support by updating artifact configuration and improving extraction logic
This commit is contained in:
@@ -42,7 +42,7 @@ class DefaultShell extends Shell
|
||||
$cmd = SPC_CURL_EXEC . " -sfSL {$retry_arg} {$method_arg} {$header_arg} {$url_arg}";
|
||||
|
||||
$this->logCommandInfo($cmd);
|
||||
$result = $this->passthru($cmd, console_output: false, capture_output: true, throw_on_error: false);
|
||||
$result = $this->passthru($cmd, capture_output: true, throw_on_error: false);
|
||||
$ret = $result['code'];
|
||||
$output = $result['output'];
|
||||
if ($ret !== 0) {
|
||||
@@ -96,15 +96,15 @@ class DefaultShell extends Shell
|
||||
$cmd = clean_spaces("{$git} clone --config core.autocrlf=false --branch {$branch_arg} {$shallow_arg} {$submodules_arg} {$url_arg} {$path_arg}");
|
||||
$this->logCommandInfo($cmd);
|
||||
logger()->debug("[GIT CLONE] {$cmd}");
|
||||
$this->passthru($cmd, $this->console_putput, capture_output: false, throw_on_error: true);
|
||||
$this->passthru($cmd, $this->console_putput);
|
||||
if ($submodules !== null) {
|
||||
$depth_flag = $shallow ? '--depth 1' : '';
|
||||
foreach ($submodules as $submodule) {
|
||||
$submodule = escapeshellarg($submodule);
|
||||
$submodule_cmd = clean_spaces("cd {$path_arg} && {$git} submodule update --init {$depth_flag} {$submodule}");
|
||||
$submodule_cmd = clean_spaces("{$git} submodule update --init {$depth_flag} {$submodule}");
|
||||
$this->logCommandInfo($submodule_cmd);
|
||||
logger()->debug("[GIT SUBMODULE] {$submodule_cmd}");
|
||||
$this->passthru($submodule_cmd, $this->console_putput, capture_output: false, throw_on_error: true);
|
||||
$this->passthru($submodule_cmd, $this->console_putput, cwd: $path_arg);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -117,7 +117,7 @@ class DefaultShell extends Shell
|
||||
* @param string $compression Compression type: 'gz', 'bz2', 'xz', or 'none'
|
||||
* @param int $strip Number of leading components to strip (default: 1)
|
||||
*/
|
||||
public function executeTarExtract(string $archive_path, string $target_path, string $compression, int $strip = 1): void
|
||||
public function executeTarExtract(string $archive_path, string $target_path, string $compression, int $strip = 1): bool
|
||||
{
|
||||
$archive_arg = escapeshellarg(FileSystem::convertPath($archive_path));
|
||||
$target_arg = escapeshellarg(FileSystem::convertPath($target_path));
|
||||
@@ -135,7 +135,8 @@ class DefaultShell extends Shell
|
||||
|
||||
$this->logCommandInfo($cmd);
|
||||
logger()->debug("[TAR EXTRACT] {$cmd}");
|
||||
$this->passthru($cmd, $this->console_putput, capture_output: false, throw_on_error: true);
|
||||
$this->passthru($cmd, $this->console_putput);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -154,7 +155,7 @@ class DefaultShell extends Shell
|
||||
|
||||
$this->logCommandInfo($cmd);
|
||||
logger()->debug("[UNZIP] {$cmd}");
|
||||
$this->passthru($cmd, $this->console_putput, capture_output: false, throw_on_error: true);
|
||||
$this->passthru($cmd, $this->console_putput);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -162,9 +163,8 @@ class DefaultShell extends Shell
|
||||
*
|
||||
* @param string $archive_path Path to the archive file
|
||||
* @param string $target_path Path to extract to
|
||||
* @param bool $is_txz Whether this is a .txz/.tar.xz file that needs double extraction
|
||||
*/
|
||||
public function execute7zExtract(string $archive_path, string $target_path, bool $is_txz = false): void
|
||||
public function execute7zExtract(string $archive_path, string $target_path): bool
|
||||
{
|
||||
$sdk_path = getenv('PHP_SDK_PATH');
|
||||
if ($sdk_path === false) {
|
||||
@@ -177,15 +177,19 @@ class DefaultShell extends Shell
|
||||
|
||||
$mute = $this->console_putput ? '' : ' > NUL';
|
||||
|
||||
if ($is_txz) {
|
||||
// txz/tar.xz contains a tar file inside, extract twice
|
||||
$cmd = "{$_7z} x {$archive_arg} -so | {$_7z} x -si -ttar -o{$target_arg} -y{$mute}";
|
||||
} else {
|
||||
$cmd = "{$_7z} x {$archive_arg} -o{$target_arg} -y{$mute}";
|
||||
}
|
||||
$run = function ($cmd) {
|
||||
$this->logCommandInfo($cmd);
|
||||
logger()->debug("[7Z EXTRACT] {$cmd}");
|
||||
$this->passthru($cmd, $this->console_putput);
|
||||
};
|
||||
|
||||
$this->logCommandInfo($cmd);
|
||||
logger()->debug("[7Z EXTRACT] {$cmd}");
|
||||
$this->passthru($cmd, $this->console_putput, capture_output: false, throw_on_error: true);
|
||||
$extname = FileSystem::extname($archive_path);
|
||||
match ($extname) {
|
||||
'tar' => $this->executeTarExtract($archive_path, $target_path, 'none'),
|
||||
'gz', 'tgz', 'xz', 'txz', 'bz2' => $run("{$_7z} x -so {$archive_arg} | tar tar -f - -x -C {$target_arg} --strip-components 1"),
|
||||
default => $run("{$_7z} x {$archive_arg} -o{$target_arg} -y{$mute}"),
|
||||
};
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -148,8 +148,12 @@ abstract class Shell
|
||||
bool $console_output = false,
|
||||
?string $original_command = null,
|
||||
bool $capture_output = false,
|
||||
bool $throw_on_error = true
|
||||
bool $throw_on_error = true,
|
||||
?string $cwd = null
|
||||
): array {
|
||||
if ($cwd !== null) {
|
||||
$cwd = $cwd;
|
||||
}
|
||||
$file_res = null;
|
||||
if ($this->enable_log_file) {
|
||||
// write executed command to the log file using fwrite
|
||||
@@ -160,10 +164,10 @@ abstract class Shell
|
||||
}
|
||||
$descriptors = [
|
||||
0 => ['file', 'php://stdin', 'r'], // stdin
|
||||
1 => ['pipe', 'w'], // stdout
|
||||
2 => ['pipe', 'w'], // stderr
|
||||
1 => PHP_OS_FAMILY === 'Windows' ? ['socket'] : ['pipe', 'w'], // stdout
|
||||
2 => PHP_OS_FAMILY === 'Windows' ? ['socket'] : ['pipe', 'w'], // stderr
|
||||
];
|
||||
$process = proc_open($cmd, $descriptors, $pipes);
|
||||
$process = proc_open($cmd, $descriptors, $pipes, $cwd);
|
||||
|
||||
$output_value = '';
|
||||
try {
|
||||
|
||||
@@ -33,7 +33,7 @@ class UnixShell extends Shell
|
||||
$original_command = $cmd;
|
||||
$this->logCommandInfo($original_command);
|
||||
$this->last_cmd = $cmd = $this->getExecString($cmd);
|
||||
$this->passthru($cmd, $this->console_putput, $original_command, capture_output: false, throw_on_error: true);
|
||||
$this->passthru($cmd, $this->console_putput, $original_command, cwd: $this->cd);
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -71,7 +71,7 @@ class UnixShell extends Shell
|
||||
}
|
||||
$cmd = $this->getExecString($cmd);
|
||||
$this->logCommandInfo($cmd);
|
||||
$result = $this->passthru($cmd, $this->console_putput, $cmd, capture_output: true, throw_on_error: false);
|
||||
$result = $this->passthru($cmd, $this->console_putput, $cmd, capture_output: true, throw_on_error: false, cwd: $this->cd);
|
||||
$out = explode("\n", $result['output']);
|
||||
return [$result['code'], $out];
|
||||
}
|
||||
@@ -83,9 +83,6 @@ class UnixShell extends Shell
|
||||
if (!empty($env_str)) {
|
||||
$cmd = "{$env_str} {$cmd}";
|
||||
}
|
||||
if ($this->cd !== null) {
|
||||
$cmd = 'cd ' . escapeshellarg($this->cd) . ' && ' . $cmd;
|
||||
}
|
||||
return $cmd;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ declare(strict_types=1);
|
||||
|
||||
namespace StaticPHP\Runtime\Shell;
|
||||
|
||||
use StaticPHP\Exception\ExecutionException;
|
||||
use StaticPHP\Exception\SPCInternalException;
|
||||
use ZM\Logger\ConsoleColor;
|
||||
|
||||
@@ -28,7 +27,7 @@ class WindowsCmd extends Shell
|
||||
$this->last_cmd = $cmd = $this->getExecString($cmd);
|
||||
// echo $cmd . PHP_EOL;
|
||||
|
||||
$this->passthru($cmd, $this->console_putput, $original_command, capture_output: false, throw_on_error: true);
|
||||
$this->passthru($cmd, $this->console_putput, $original_command, cwd: $this->cd);
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -46,7 +45,7 @@ class WindowsCmd extends Shell
|
||||
logger()->debug('Running command with result: ' . $cmd);
|
||||
}
|
||||
$cmd = $this->getExecString($cmd);
|
||||
$result = $this->passthru($cmd, $this->console_putput, $cmd, capture_output: true, throw_on_error: false);
|
||||
$result = $this->passthru($cmd, $this->console_putput, $cmd, capture_output: true, throw_on_error: false, cwd: $this->cd);
|
||||
$out = explode("\n", $result['output']);
|
||||
return [$result['code'], $out];
|
||||
}
|
||||
@@ -68,89 +67,8 @@ class WindowsCmd extends Shell
|
||||
return $this->last_cmd;
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes a command with console and log file output.
|
||||
*
|
||||
* @param string $cmd Full command to execute (including cd and env vars)
|
||||
* @param bool $console_output If true, output will be printed to console
|
||||
* @param null|string $original_command Original command string for logging
|
||||
* @param bool $capture_output If true, capture and return output
|
||||
* @param bool $throw_on_error If true, throw exception on non-zero exit code
|
||||
*
|
||||
* @return array{code: int, output: string} Returns exit code and captured output
|
||||
*/
|
||||
protected function passthru(
|
||||
string $cmd,
|
||||
bool $console_output = false,
|
||||
?string $original_command = null,
|
||||
bool $capture_output = false,
|
||||
bool $throw_on_error = true
|
||||
): array {
|
||||
$file_res = null;
|
||||
if ($this->enable_log_file) {
|
||||
$file_res = fopen(SPC_SHELL_LOG, 'a');
|
||||
}
|
||||
|
||||
$output_value = '';
|
||||
try {
|
||||
$process = popen($cmd . ' 2>&1', 'r');
|
||||
if (!$process) {
|
||||
throw new ExecutionException(
|
||||
cmd: $original_command ?? $cmd,
|
||||
message: 'Failed to open process for command, popen() failed.',
|
||||
code: -1,
|
||||
cd: $this->cd,
|
||||
env: $this->env
|
||||
);
|
||||
}
|
||||
|
||||
while (($line = fgets($process)) !== false) {
|
||||
if (static::$passthru_callback !== null) {
|
||||
$callback = static::$passthru_callback;
|
||||
$callback();
|
||||
}
|
||||
if ($console_output) {
|
||||
echo $line;
|
||||
}
|
||||
if ($file_res !== null) {
|
||||
fwrite($file_res, $line);
|
||||
}
|
||||
if ($capture_output) {
|
||||
$output_value .= $line;
|
||||
}
|
||||
}
|
||||
|
||||
$result_code = pclose($process);
|
||||
|
||||
if ($throw_on_error && $result_code !== 0) {
|
||||
if ($file_res !== null) {
|
||||
fwrite($file_res, "Command exited with non-zero code: {$result_code}\n");
|
||||
}
|
||||
throw new ExecutionException(
|
||||
cmd: $original_command ?? $cmd,
|
||||
message: "Command exited with non-zero code: {$result_code}",
|
||||
code: $result_code,
|
||||
cd: $this->cd,
|
||||
env: $this->env,
|
||||
);
|
||||
}
|
||||
|
||||
return [
|
||||
'code' => $result_code,
|
||||
'output' => $output_value,
|
||||
];
|
||||
} finally {
|
||||
if ($file_res !== null) {
|
||||
fclose($file_res);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function getExecString(string $cmd): string
|
||||
{
|
||||
if ($this->cd !== null) {
|
||||
$cmd = 'cd /d ' . escapeshellarg($this->cd) . ' && ' . $cmd;
|
||||
}
|
||||
return $cmd;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user