From ccd0a636da0e2645e0d93506088bb7d2e4cad6af Mon Sep 17 00:00:00 2001 From: Iosif Miclaus Date: Fri, 3 Jan 2025 21:11:05 +0100 Subject: [PATCH] feat: add target option - remove version restriction tests - add dev and target options validation for interactive mode (otherwise the exception will be thrown only after the interactive part, if there's a conflict) --- src/NewCommand.php | 39 +++++++++++++++++++++++---------------- tests/NewCommandTest.php | 13 ------------- 2 files changed, 23 insertions(+), 29 deletions(-) diff --git a/src/NewCommand.php b/src/NewCommand.php index e0744ec..954a500 100644 --- a/src/NewCommand.php +++ b/src/NewCommand.php @@ -42,7 +42,8 @@ protected function configure() ->setName('new') ->setDescription('Create a new Laravel application') ->addArgument('name', InputArgument::REQUIRED) - ->addOption('dev', null, InputOption::VALUE_NONE, 'Installs the latest "development" release') + ->addOption('dev', null, InputOption::VALUE_NONE, 'Installs the latest "development" release. Cannot be used with --target') + ->addOption('target', null, InputOption::VALUE_OPTIONAL, 'Installs the specified version of Laravel. Cannot be used with --dev') ->addOption('git', null, InputOption::VALUE_NONE, 'Initialize a Git repository') ->addOption('branch', null, InputOption::VALUE_REQUIRED, 'The branch that should be created for a new repository', $this->defaultBranch()) ->addOption('github', null, InputOption::VALUE_OPTIONAL, 'Create a new repository on GitHub', false) @@ -86,6 +87,7 @@ protected function interact(InputInterface $input, OutputInterface $output) |______\__,_|_| \__,_| \_/ \___|_|'.PHP_EOL.PHP_EOL); $this->ensureExtensionsAreAvailable($input, $output); + $this->validateDevAndTargetOptions($input); if (! $input->getArgument('name')) { $input->setArgument('name', text( @@ -190,6 +192,7 @@ protected function ensureExtensionsAreAvailable(InputInterface $input, OutputInt */ protected function execute(InputInterface $input, OutputInterface $output): int { + $this->validateDevAndTargetOptions($input); $this->validateDatabaseOption($input); $this->validateStackOption($input); @@ -382,21 +385,6 @@ protected function configureDefaultDatabaseConnection(string $directory, string ); } - /** - * Determine if the application is using Laravel 11 or newer. - * - * @param string $directory - * @return bool - */ - public function usingLaravelVersionOrNewer(int $usingVersion, string $directory): bool - { - $version = json_decode(file_get_contents($directory.'/composer.json'), true)['require']['laravel/framework']; - $version = str_replace('^', '', $version); - $version = explode('.', $version)[0]; - - return $version >= $usingVersion; - } - /** * Comment the irrelevant database configuration entries for SQLite applications. * @@ -648,6 +636,21 @@ protected function promptForJetstreamOptions(InputInterface $input) ))->each(fn ($option) => $input->setOption($option, true)); } + /** + * Validate the usage of --dev and --target options. + * + * @param \Symfony\Component\Console\Input\InputInterface $input + * @return void + * + * @throws \InvalidArgumentException + */ + protected function validateDevAndTargetOptions(InputInterface $input) + { + if ($input->getOption('dev') && $input->getOption('target')) { + throw new \InvalidArgumentException('The --dev and --target options cannot be used together. Please specify only one.'); + } + } + /** * Validate the database driver input. * @@ -884,6 +887,10 @@ protected function getVersion(InputInterface $input) return 'dev-master'; } + if ($input->getOption('target')) { + return $input->getOption('target'); + } + return ''; } diff --git a/tests/NewCommandTest.php b/tests/NewCommandTest.php index ca73557..180099a 100644 --- a/tests/NewCommandTest.php +++ b/tests/NewCommandTest.php @@ -72,17 +72,4 @@ public function test_it_can_chops_trailing_slash_from_name() ); } } - - public function test_on_at_least_laravel_11() - { - $command = new NewCommand; - - $onLaravel10 = $command->usingLaravelVersionOrNewer(11, __DIR__.'/fixtures/laravel10'); - $onLaravel11 = $command->usingLaravelVersionOrNewer(11, __DIR__.'/fixtures/laravel11'); - $onLaravel12 = $command->usingLaravelVersionOrNewer(11, __DIR__.'/fixtures/laravel12'); - - $this->assertFalse($onLaravel10); - $this->assertTrue($onLaravel11); - $this->assertTrue($onLaravel12); - } }