diff --git a/composer.json b/composer.json index 87921fc..9606ba1 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ "php": "^8.2", "illuminate/filesystem": "^10.20|^11.0", "illuminate/support": "^10.20|^11.0", - "laravel/prompts": "^0.1", + "laravel/prompts": "^0.1.23", "symfony/console": "^6.2|^7.0", "symfony/process": "^6.2|^7.0" }, diff --git a/src/FormBuilder.php b/src/FormBuilder.php new file mode 100644 index 0000000..f1e7993 --- /dev/null +++ b/src/FormBuilder.php @@ -0,0 +1,20 @@ +steps[] = new \Laravel\Prompts\FormStep($step, $condition, $name, $ignoreWhenReverting); + + return $this; + } + + public function getStepsCount(): int + { + return count($this->steps); + } +} diff --git a/src/NewCommand.php b/src/NewCommand.php index dd0814a..89023dd 100644 --- a/src/NewCommand.php +++ b/src/NewCommand.php @@ -76,16 +76,14 @@ protected function interact(InputInterface $input, OutputInterface $output) parent::interact($input, $output); $this->configurePrompts($input, $output); - $output->write(PHP_EOL.' _ _ | | | | | | __ _ _ __ __ ___ _____| | | | / _` | \'__/ _` \ \ / / _ \ | | |___| (_| | | | (_| |\ V / __/ | |______\__,_|_| \__,_| \_/ \___|_|'.PHP_EOL.PHP_EOL); - - if (! $input->getArgument('name')) { - $input->setArgument('name', text( + (new FormBuilder())->addWithCondition( + fn () => $input->setArgument('name', text( label: 'What is the name of your project?', placeholder: 'E.g. example-app', required: 'The project name is required.', @@ -102,48 +100,65 @@ protected function interact(InputInterface $input, OutputInterface $output) } } }, - )); - } - - if ($input->getOption('force') !== true) { - $this->verifyApplicationDoesntExist( + )), + condition: ! $input->getArgument('name') + )->addWithCondition( + fn () => $this->verifyApplicationDoesntExist( $this->getInstallationDirectory($input->getArgument('name')) - ); - } - - if (! $input->getOption('breeze') && ! $input->getOption('jet')) { - match (select( - label: 'Would you like to install a starter kit?', - options: [ - 'none' => 'No starter kit', - 'breeze' => 'Laravel Breeze', - 'jetstream' => 'Laravel Jetstream', - ], - default: 'none', - )) { - 'breeze' => $input->setOption('breeze', true), - 'jetstream' => $input->setOption('jet', true), - default => null, - }; - } - - if ($input->getOption('breeze')) { - $this->promptForBreezeOptions($input); - } elseif ($input->getOption('jet')) { - $this->promptForJetstreamOptions($input); - } - - if (! $input->getOption('phpunit') && ! $input->getOption('pest')) { - $input->setOption('pest', select( - label: 'Which testing framework do you prefer?', - options: ['Pest', 'PHPUnit'], - default: 'Pest', - ) === 'Pest'); - } - - if (! $input->getOption('git') && $input->getOption('github') === false && Process::fromShellCommandline('git --version')->run() === 0) { - $input->setOption('git', confirm(label: 'Would you like to initialize a Git repository?', default: false)); - } + ), + ignoreWhenReverting: true, + condition: $input->getOption('force') !== true, + )->addWithCondition( + function ($reponses, $previousResponse) use ($input) { + if ($previousResponse) { + $input->setOption('breeze', false); + $input->setOption('jet', false); + $input->setOption('stack', false); + } + match (select( + label: 'Would you like to install a starter kit?', + options: [ + 'none' => 'No starter kit', + 'breeze' => 'Laravel Breeze', + 'jetstream' => 'Laravel Jetstream', + ], + default: 'none', + )) { + 'breeze' => $input->setOption('breeze', true), + 'jetstream' => $input->setOption('jet', true), + default => null, + }; + + return true; + }, + condition: ! $input->getOption('breeze') && ! $input->getOption('jet') + )->addWithCondition( + function () use ($input) { + $this->promptForBreezeOptions($input); + }, + condition: fn () => $input->getOption('breeze') + )->addWithCondition( + function () use ($input) { + $this->promptForJetstreamOptions($input); + }, + condition: fn () => $input->getOption('jet') + )->addWithCondition( + function () use ($input) { + $input->setOption('pest', select( + label: 'Which testing framework do you prefer?', + options: ['Pest', 'PHPUnit'], + default: 'Pest', + ) === 'Pest'); + }, + condition: ! $input->getOption('phpunit') && ! $input->getOption('pest') + )->addWithCondition( + function () use ($input) { + $input->setOption('git', confirm(label: 'Would you like to initialize a Git repository?', default: false)); + }, + condition: ! $input->getOption('git') + && $input->getOption('github') === false + && Process::fromShellCommandline('git --version')->run() === 0 + )->submit(); } /** diff --git a/tests/FormBuilderTest.php b/tests/FormBuilderTest.php new file mode 100644 index 0000000..6801645 --- /dev/null +++ b/tests/FormBuilderTest.php @@ -0,0 +1,18 @@ +addWithCondition(function () { + return 'test'; + }); + $this->assertEquals(1, $formBuilder->getStepsCount()); + } +}