Skip to content

Commit

Permalink
feat: add target option
Browse files Browse the repository at this point in the history
- 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)
  • Loading branch information
miclaus committed Jan 3, 2025
1 parent 1690a92 commit ccd0a63
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 29 deletions.
39 changes: 23 additions & 16 deletions src/NewCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -884,6 +887,10 @@ protected function getVersion(InputInterface $input)
return 'dev-master';
}

if ($input->getOption('target')) {
return $input->getOption('target');
}

return '';
}

Expand Down
13 changes: 0 additions & 13 deletions tests/NewCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit ccd0a63

Please # to comment.