From 56fb4d93060d8d4a1d1715c398f10cc11a774011 Mon Sep 17 00:00:00 2001 From: Jesse Rushlow Date: Tue, 27 Feb 2024 20:29:01 -0500 Subject: [PATCH] [make:twig-component] default to "no" when asking if is live component fixes #1395 --- src/Maker/MakeTwigComponent.php | 43 ++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/src/Maker/MakeTwigComponent.php b/src/Maker/MakeTwigComponent.php index 64390c4757..8c8bd17374 100644 --- a/src/Maker/MakeTwigComponent.php +++ b/src/Maker/MakeTwigComponent.php @@ -13,6 +13,7 @@ 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; @@ -20,6 +21,7 @@ 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; @@ -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 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', @@ -87,11 +115,4 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen $io->writeln(" To render the component, use ."); $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))); - } - } }