Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --database option #320

Merged
merged 2 commits into from
Mar 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions src/NewCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ protected function configure()
->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)
->addOption('organization', null, InputOption::VALUE_REQUIRED, 'The GitHub organization to create the new repository for')
->addOption('database', null, InputOption::VALUE_REQUIRED, 'The database driver your application will use')
->addOption('stack', null, InputOption::VALUE_OPTIONAL, 'The Breeze / Jetstream stack that should be installed')
->addOption('breeze', null, InputOption::VALUE_NONE, 'Installs the Laravel Breeze scaffolding')
->addOption('jet', null, InputOption::VALUE_NONE, 'Installs the Laravel Jetstream scaffolding')
Expand Down Expand Up @@ -137,6 +138,7 @@ protected function interact(InputInterface $input, OutputInterface $output)
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->validateDatabaseOption($input);
$this->validateStackOption($input);

$name = $input->getArgument('name');
Expand Down Expand Up @@ -449,8 +451,8 @@ protected function promptForDatabaseOptions(string $directory, InputInterface $i
{
$defaultDatabase = 'sqlite';

if ($input->isInteractive()) {
$database = select(
if (! $input->getOption('database') && $input->isInteractive()) {
$input->setOption('database', select(
label: 'Which database will your application use?',
options: [
'mysql' => 'MySQL',
Expand All @@ -460,20 +462,19 @@ protected function promptForDatabaseOptions(string $directory, InputInterface $i
'sqlsrv' => 'SQL Server',
],
default: $defaultDatabase
);
));

if ($database !== $defaultDatabase) {
if ($input->getOption('database') !== $defaultDatabase) {
$migrate = confirm(label: 'Default database updated. Would you like to run the default database migrations?', default: true);
}
}

return [$database ?? $defaultDatabase, $migrate ?? false];
return [$input->getOption('database') ?? $defaultDatabase, $migrate ?? false];
}

/**
* Determine the stack for Breeze.
*
* @param \Symfony\Component\Console\Input\InputInterface $input
* @return void
*/
protected function promptForBreezeOptions(InputInterface $input)
Expand Down Expand Up @@ -555,6 +556,18 @@ protected function promptForJetstreamOptions(InputInterface $input)
))->each(fn ($option) => $input->setOption($option, true));
}

/**
* Validate the database driver input.
*
* @param \Symfony\Components\Console\Input\InputInterface
*/
protected function validateDatabaseOption(InputInterface $input)
{
if ($input->getOption('database') && ! in_array($input->getOption('database'), $drivers = ['mysql', 'mariadb', 'pgsql', 'sqlite', 'sqlsrv'])) {
throw new \InvalidArgumentException("Invalid database driver [{$input->getOption('database')}]. Valid options are: ".implode(', ', $drivers).'.');
}
}

/**
* Validate the starter kit stack input.
*
Expand Down