Skip to content

Commit

Permalink
[make:twig-component] default to "no" when asking if is live component
Browse files Browse the repository at this point in the history
  • Loading branch information
jrushlow committed Feb 28, 2024
1 parent cdcd168 commit 56fb4d9
Showing 1 changed file with 32 additions and 11 deletions.
43 changes: 32 additions & 11 deletions src/Maker/MakeTwigComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@

use Symfony\Bundle\MakerBundle\ConsoleStyle;
use Symfony\Bundle\MakerBundle\DependencyBuilder;
use Symfony\Bundle\MakerBundle\Exception\RuntimeCommandException;
use Symfony\Bundle\MakerBundle\Generator;
use Symfony\Bundle\MakerBundle\InputConfiguration;
use Symfony\Bundle\MakerBundle\Str;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Process\Process;
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;

Expand Down Expand Up @@ -52,15 +54,41 @@ public function configureDependencies(DependencyBuilder $dependencies): void
$dependencies->addClassDependency(AsTwigComponent::class, 'symfony/ux-twig-component');
}

public function interact(InputInterface $input, ConsoleStyle $io, Command $command): void
{
if (!$input->getOption('live')) {
$input->setOption('live', $io->confirm('Make this a live component?', false));
}

$isLive = $input->getOption('live');

if (!$isLive || ($isLive && class_exists(AsLiveComponent::class))) {
return;
}

$io->caution('The Symfony UX Live Component is needed to make this a live component.');

if (!$io->confirm('Do you want us to run <fg=yellow>composer require symfony/ux-live-component</> for you?', false)) {
return;
}

$process = Process::fromShellCommandline('composer require symfony/ux-live-component');
if (Command::SUCCESS === $process->run()) {
$io->info('symfony/ux-live-component successfully installed!');

return;
}

$io->block($process->getErrorOutput());

throw new RuntimeCommandException('Oops! There was a problem installing symfony/ux-live-component.');
}

public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void
{
$name = $input->getArgument('name');
$live = $input->getOption('live');

if ($live && !class_exists(AsLiveComponent::class)) {
throw new \RuntimeException('You must install symfony/ux-live-component to create a live component (composer require symfony/ux-live-component)');
}

$factory = $generator->createClassNameDetails(
$name,
'Twig\\Components',
Expand All @@ -87,11 +115,4 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
$io->writeln(" To render the component, use <fg=yellow><twig:{$shortName} /></>.");
$io->newLine();
}

public function interact(InputInterface $input, ConsoleStyle $io, Command $command): void
{
if (!$input->getOption('live')) {
$input->setOption('live', $io->confirm('Make this a live component?', class_exists(AsLiveComponent::class)));
}
}
}

0 comments on commit 56fb4d9

Please sign in to comment.