Skip to content

Commit

Permalink
Validate Laravel can be installed on selected directory (#344)
Browse files Browse the repository at this point in the history
* Validate installer can be installed on selected directory

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

* formatting

* formatting

---------

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
crynobone and taylorotwell committed Jun 6, 2024
1 parent 5d45e78 commit d48c6b6
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions src/NewCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,28 @@ protected function interact(InputInterface $input, OutputInterface $output)
label: 'What is the name of your project?',
placeholder: 'E.g. example-app',
required: 'The project name is required.',
validate: fn ($value) => preg_match('/[^\pL\pN\-_.]/', $value) !== 0
? 'The name may only contain letters, numbers, dashes, underscores, and periods.'
: null,
validate: function ($value) use ($input) {
if (preg_match('/[^\pL\pN\-_.]/', $value) !== 0) {
return 'The name may only contain letters, numbers, dashes, underscores, and periods.';
}

if ($input->getOption('force') !== true) {
try {
$this->verifyApplicationDoesntExist($this->getInstallationDirectory($value));
} catch (RuntimeException $e) {
return 'Application already exists.';
}
}
},
));
}

if ($input->getOption('force') !== true) {
$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?',
Expand Down Expand Up @@ -144,7 +160,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$name = $input->getArgument('name');

$directory = $name !== '.' ? getcwd().'/'.$name : '.';
$directory = $this->getInstallationDirectory($name);

$this->composer = new Composer(new Filesystem(), $directory);

Expand Down Expand Up @@ -788,6 +804,17 @@ protected function isParked(string $directory)
return $output !== false ? in_array(dirname($directory), json_decode($output)) : false;
}

/**
* Get the installation directory.
*
* @param string $name
* @return string
*/
protected function getInstallationDirectory(string $name)
{
return $name !== '.' ? getcwd().'/'.$name : '.';
}

/**
* Get the version that should be downloaded.
*
Expand Down

0 comments on commit d48c6b6

Please sign in to comment.