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

Feature/reversable prompt to the laravel install proecess #350

Closed
Closed
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
20 changes: 20 additions & 0 deletions src/FormBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Laravel\Installer\Console;

use Closure;

class FormBuilder extends \Laravel\Prompts\FormBuilder
{
public function addWithCondition(Closure $step, ?string $name = null, bool $ignoreWhenReverting = false, bool|Closure $condition = true): self
{
$this->steps[] = new \Laravel\Prompts\FormStep($step, $condition, $name, $ignoreWhenReverting);

return $this;
}

public function getStepsCount(): int
{
return count($this->steps);
}
}
105 changes: 60 additions & 45 deletions src/NewCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,14 @@ protected function interact(InputInterface $input, OutputInterface $output)
parent::interact($input, $output);

$this->configurePrompts($input, $output);

$output->write(PHP_EOL.' <fg=red> _ _
| | | |
| | __ _ _ __ __ ___ _____| |
| | / _` | \'__/ _` \ \ / / _ \ |
| |___| (_| | | | (_| |\ 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.',
Expand All @@ -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();
}

/**
Expand Down
18 changes: 18 additions & 0 deletions tests/FormBuilderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Laravel\Installer\Console\Tests;

use Laravel\Installer\Console\FormBuilder;
use PHPUnit\Framework\TestCase;

class FormBuilderTest extends TestCase
{
public function test_add_with_condition(): void
{
$formBuilder = new FormBuilder;
$formBuilder->addWithCondition(function () {
return 'test';
});
$this->assertEquals(1, $formBuilder->getStepsCount());
}
}