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 5 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
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
50 changes: 48 additions & 2 deletions src/NewCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Terminal;
use Symfony\Component\Process\Exception\ProcessStartFailedException;
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;
use function Laravel\Prompts\spin;
use function Laravel\Prompts\text;

class NewCommand extends Command
Expand Down Expand Up @@ -877,7 +879,7 @@ protected function runCommands($commands, InputInterface $input, OutputInterface
}, $commands);
}

if ($input->getOption('quiet')) {
if (! $output->isVerbose() && $this->canUseSpinner($input, $output)) {
$commands = array_map(function ($value) {
if (str_starts_with($value, 'chmod')) {
return $value;
Expand All @@ -891,7 +893,39 @@ protected function runCommands($commands, InputInterface $input, OutputInterface
}, $commands);
}

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

if (! $process->isSuccessful()) {
$output->writeln(' <bg=red;fg=white> ERROR </> '.$process->getErrorOutput().PHP_EOL);

break;
}
}

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 {
Expand Down Expand Up @@ -956,4 +990,16 @@ protected function pregReplaceInFile(string $pattern, string $replace, string $f
preg_replace($pattern, $replace, file_get_contents($file))
);
}

/**
* Checks if its possible to use the spinner.
*
* @return bool
*/
protected function canUseSpinner(InputInterface $input, OutputInterface $output)
{
return function_exists('pcntl_fork') &&
! $output->isVerbose() &&
! $input->getOption('quiet');
}
}