Skip to content
Merged
Show file tree
Hide file tree
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
21 changes: 16 additions & 5 deletions src/Commands/NewCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,21 @@ private function runArtisanInstall(string $phpPrefix): self
return $this->runArtisan($phpPrefix, 'pollora:install', 'Running pollora:install...');
}

/**
* The shell command that runs an artisan command in the new project.
*
* `pollora new --no-interaction` passes the flag on: the artisan command
* gets a terminal from here, so without it pollora:install would prompt
* anyway — or, with no terminal at all, fall back to its defaults.
*/
private function artisanCommandLine(string $phpPrefix, string $artisanCommand): string
{
$php = str_starts_with($phpPrefix, 'ddev') ? 'ddev exec php' : $phpPrefix;
$flags = $this->input->isInteractive() ? '' : ' --no-interaction';

return $php.' artisan '.$artisanCommand.$flags;
}

/**
* Run an artisan command with a terminal attached, so that its prompts work.
*/
Expand All @@ -351,11 +366,7 @@ private function runArtisan(string $phpPrefix, string $artisanCommand, string $m
$this->output->writeln(' <info>'.$message.'</info>');
$this->output->writeln('');

$isDdev = str_starts_with($phpPrefix, 'ddev');

$command = ($isDdev ? 'ddev exec php' : $phpPrefix).' artisan '.$artisanCommand;

$process = Process::fromShellCommandline($command, $this->absolutePath);
$process = Process::fromShellCommandline($this->artisanCommandLine($phpPrefix, $artisanCommand), $this->absolutePath);
$process->setTimeout(null);

// Set TERM=dumb on the HOST side to prevent the terminal emulator
Expand Down
16 changes: 16 additions & 0 deletions tests/Unit/Commands/NewCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Pollora\Cli\Application;
use Pollora\Cli\Commands\NewCommand;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Tester\CommandTester;

Expand Down Expand Up @@ -113,6 +114,7 @@
$command = newCommandWith('');
(new ReflectionProperty($command, 'absolutePath'))->setValue($command, $dir);
(new ReflectionProperty($command, 'output'))->setValue($command, new BufferedOutput);
(new ReflectionProperty($command, 'input'))->setValue($command, new ArrayInput([]));

// `false artisan pollora:install` exits 1, standing in for a failed install.
$run = fn (): mixed => (new ReflectionMethod($command, 'runArtisan'))
Expand Down Expand Up @@ -219,3 +221,17 @@ function prepareEnvironmentFileIn(string $directory): ?string

return $path;
}

it('passes --no-interaction on to artisan when it was given one', function (bool $interactive, string $expected): void {
$command = newCommandWith('');
$input = new ArrayInput([]);
$input->setInteractive($interactive);
(new ReflectionProperty($command, 'input'))->setValue($command, $input);

$line = (new ReflectionMethod($command, 'artisanCommandLine'))->invoke($command, 'ddev exec php', 'pollora:install');

expect($line)->toBe($expected);
})->with([
'interactive' => [true, 'ddev exec php artisan pollora:install'],
'no interaction' => [false, 'ddev exec php artisan pollora:install --no-interaction'],
]);
Loading