webman迁移
This commit is contained in:
@@ -84,7 +84,9 @@ class Terminal
|
||||
'command' => $command,
|
||||
];
|
||||
} else {
|
||||
$command['cwd'] = root_path() . ($command['cwd'] ?? '');
|
||||
$cwd = $command['cwd'] ?? '';
|
||||
$root = rtrim(root_path(), DIRECTORY_SEPARATOR . '/');
|
||||
$command['cwd'] = $root . DIRECTORY_SEPARATOR . ltrim(str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $cwd), DIRECTORY_SEPARATOR . '/');
|
||||
}
|
||||
|
||||
$request = request();
|
||||
@@ -98,6 +100,10 @@ class Terminal
|
||||
}
|
||||
|
||||
$command['cwd'] = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $command['cwd']);
|
||||
if (DIRECTORY_SEPARATOR === '\\') {
|
||||
$command['command'] = 'cmd /c "cd /d ' . str_replace('"', '""', $command['cwd']) . ' && ' . $command['command'] . '"';
|
||||
$command['cwd'] = root_path();
|
||||
}
|
||||
return $command;
|
||||
}
|
||||
|
||||
@@ -140,21 +146,34 @@ class Terminal
|
||||
}
|
||||
$this->output('> ' . $command['command'], false);
|
||||
|
||||
$this->process = proc_open($command['command'], $this->descriptorsPec, $this->pipes, $command['cwd']);
|
||||
$procEnv = $this->getProcEnv();
|
||||
$this->process = proc_open($command['command'], $this->descriptorsPec, $this->pipes, $command['cwd'], $procEnv);
|
||||
if (!is_resource($this->process)) {
|
||||
$this->execError('Failed to execute', true);
|
||||
}
|
||||
if (isset($this->pipes[0])) {
|
||||
fclose($this->pipes[0]);
|
||||
unset($this->pipes[0]);
|
||||
}
|
||||
$lastHeartbeat = time();
|
||||
$heartbeatInterval = 15;
|
||||
while ($this->getProcStatus()) {
|
||||
$contents = file_get_contents($this->outputFile);
|
||||
if (strlen($contents) && $this->outputContent != $contents) {
|
||||
$newOutput = str_replace($this->outputContent, '', $contents);
|
||||
$this->checkOutput($contents, $newOutput);
|
||||
if (preg_match('/\r\n|\r|\n/', $newOutput)) {
|
||||
if (strlen(trim($newOutput)) > 0) {
|
||||
$this->output($newOutput);
|
||||
$this->outputContent = $contents;
|
||||
$lastHeartbeat = time();
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->procStatusMark === 1 && (time() - $lastHeartbeat) >= $heartbeatInterval) {
|
||||
$this->sendHeartbeat();
|
||||
$lastHeartbeat = time();
|
||||
}
|
||||
|
||||
if ($this->procStatusMark === 2) {
|
||||
$this->output('exitCode: ' . $this->procStatusData['exitcode']);
|
||||
if ($this->procStatusData['exitcode'] === 0) {
|
||||
@@ -178,6 +197,17 @@ class Terminal
|
||||
$this->outputFlag('exec-completed');
|
||||
}
|
||||
|
||||
protected function getProcEnv(): ?array
|
||||
{
|
||||
$env = getenv();
|
||||
if (!is_array($env)) {
|
||||
return null;
|
||||
}
|
||||
$env['CI'] = '1';
|
||||
$env['FORCE_COLOR'] = '0';
|
||||
return $env;
|
||||
}
|
||||
|
||||
public function getProcStatus(): bool
|
||||
{
|
||||
$this->procStatusData = proc_get_status($this->process);
|
||||
@@ -221,6 +251,16 @@ class Terminal
|
||||
$this->output($this->flag[$flag], false);
|
||||
}
|
||||
|
||||
protected function sendHeartbeat(): void
|
||||
{
|
||||
$request = request();
|
||||
$connection = $request && isset($request->connection) ? $request->connection : null;
|
||||
if ($connection) {
|
||||
$connection->send(new ServerSentEvents(['' => 'keepalive']));
|
||||
}
|
||||
@ob_flush();
|
||||
}
|
||||
|
||||
public function outputCallback($data): void
|
||||
{
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user