Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.x] Run commands inside the spin function. #340

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: add runCommand method.
xiCO2k committed Jun 3, 2024
commit f10025e7e253d3e575701cf685ca5d91c0582e2b
44 changes: 32 additions & 12 deletions src/NewCommand.php
Original file line number Diff line number Diff line change
@@ -12,9 +12,9 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Exception\ProcessStartFailedException;
use Symfony\Component\Console\Terminal;
use Symfony\Component\Process\PhpExecutableFinder;
use Symfony\Component\Process\Process;

use function Laravel\Prompts\confirm;
use function Laravel\Prompts\multiselect;
use function Laravel\Prompts\select;
@@ -892,13 +892,39 @@ protected function runCommands($commands, InputInterface $input, OutputInterface
}, $commands);
}

$commands = implode(' && ', $commands);
foreach ($commands as $command) {
$process = $this->runCommand($command, $input, $output, $workingPath, $env);

if ($this->canUseSpinner($input, $output)) {
$commands .= ' > /dev/null 2>&1';
if (! $process->isSuccessful()) {
$output->writeln(' <bg=red;fg=white> ERROR </> '.$process->getErrorOutput().PHP_EOL);

break;
}
}

$process = Process::fromShellCommandline($commands, $workingPath, $env, null, null);
return $process;
}

/**
* Run the given command.
*
* @param string $command
* @param InputInterface $input
* @param OutputInterface $output
* @param string|null $workingPath
* @param array $env
* @return \Symfony\Component\Process\Process
*/
protected function runCommand(string $command, InputInterface $input, OutputInterface $output, string $workingPath = null, array $env = [])
{
$process = Process::fromShellCommandline($command, $workingPath, $env, null, null);

if ($this->canUseSpinner($input, $output)) {
$terminalWidth = (new Terminal)->getWidth();
$description = mb_substr($command, 0, $terminalWidth - 6);

return spin(fn () => tap($process)->run(), "<fg=gray>{$description}...</>");
}

if ('\\' !== DIRECTORY_SEPARATOR && file_exists('/dev/tty') && is_readable('/dev/tty')) {
try {
@@ -908,13 +934,7 @@ protected function runCommands($commands, InputInterface $input, OutputInterface
}
}

if ($this->canUseSpinner($input, $output)) {
return spin(fn () => tap($process)->run(), 'Installing...');
}

return tap($process)->run(function ($type, $line) use ($output) {
$output->write(' '.$line);
});
return tap($process)->run();
}

/**