Skip to content

Commit

Permalink
Merge pull request #280 from captenmasin/prompts
Browse files Browse the repository at this point in the history
Convert Install and Upgrade commands to Laravel Prompts
  • Loading branch information
Cannonb4ll authored Sep 2, 2024
2 parents afc1409 + f010b97 commit 6adffc5
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 37 deletions.
72 changes: 51 additions & 21 deletions app/Console/Commands/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,20 @@
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Hash;
use App\Console\Commands\Concerns\CanShowAnIntro;
use Filament\Support\Commands\Concerns\CanValidateInput;
use function Laravel\Prompts\confirm;
use function Laravel\Prompts\password;
use function Laravel\Prompts\text;
use function Laravel\Prompts\info;

class Install extends Command
{
use CanValidateInput, CanShowAnIntro;
use CanShowAnIntro;

protected $signature = 'roadmap:install';

protected $description = 'Install Roadmap software.';

public function handle()
public function handle(): void
{
$this->intro();
$this->refreshDatabase();
Expand All @@ -31,59 +34,71 @@ public function handle()
$this->writeSeparationLine();
$this->line(' ');

$this->info('All done! You can now login at ' . url('/admin'));
info('All done! You can now login at ' . url('/admin'));
}

protected function refreshDatabase()
protected function refreshDatabase(): void
{
if ($this->confirm('Do you want to run the migrations to set up everything fresh? (php artisan migrate:fresh)')) {
if (confirm(
label: 'Do you want to run the migrations to set up everything fresh?',
default: false,
hint: '(php artisan migrate:fresh)'
)) {
$this->call('migrate:fresh');
}
}

protected function createUser()
protected function createUser(): User
{
$this->info('Let\'s create a user.');
info('Let\'s create a user.');

$user = User::create($this->getUserData());
$user->role = UserRole::Admin;
$user->email_verified_at = now();
$user->save();

$this->info('User created!');
info('User created!');

return $user;
}

protected function linkStorage()
protected function linkStorage(): void
{
if (!file_exists(public_path('storage')) && $this->confirm('Your storage does not seem to be linked, do you want me to do this?')) {
if (!file_exists(public_path('storage')) && confirm(
label: 'Your storage does not seem to be linked, do you want me to do this?',
hint: '(php artisan storage:link)'
)
) {
$this->call('storage:link');
}
}

protected function runNpm()
protected function runNpm(): void
{
if ($this->confirm('Do you want to run npm ci & npm run production to get the assets ready?')) {
$this->info('Running NPM..');
if (confirm('Do you want to run npm ci & npm run production to get the assets ready?')) {
info('Running NPM...');

shell_exec('npm ci');
shell_exec('npm run production');

$this->info('NPM installation & mixing production done!');
info('NPM installation & mixing production done!');
}
}

protected function publishAssets(): void
{
$this->info('Publishing assets..');
info('Publishing assets...');

$this->call('filament:assets');
}

protected function askForStar()
protected function askForStar(): void
{
if (User::count() === 1 && $this->confirm('Would you like to show some love by starring the repo?', true)) {
if (User::count() === 1 && confirm(
label: 'Would you like to show some love by starring the repo?',
default: true
)
) {
if (PHP_OS_FAMILY === 'Darwin') {
exec('open https://github.com/ploi/roadmap');
}
Expand All @@ -99,9 +114,24 @@ protected function askForStar()
protected function getUserData(): array
{
return [
'name' => $this->validateInput(fn () => $this->ask('Name'), 'name', ['required']),
'email' => $this->validateInput(fn () => $this->ask('Email address'), 'email', ['required', 'email', 'unique:' . User::class]),
'password' => Hash::make($this->validateInput(fn () => $this->secret('Password'), 'password', ['required', 'min:8'])),
'name' => text(
label: 'Name',
required: true,
),
'email' => text(
label: 'Email address',
required: true,
validate: [
'email' => ['required', 'email', 'unique:' . User::class]
]
),
'password' => Hash::make(password(
label: 'Password',
required: true,
validate: [
'password' => ['required', 'min:8']
]
))
];
}
}
15 changes: 8 additions & 7 deletions app/Console/Commands/Upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Services\SystemChecker;
use Illuminate\Console\Command;
use App\Console\Commands\Concerns\CanShowAnIntro;
use function Laravel\Prompts\info;

class Upgrade extends Command
{
Expand All @@ -28,42 +29,42 @@ public function handle(): void
$this->publishAssets();
$this->line(' ');

$this->info('Upgrading done!');
info('Upgrading done!');
}

protected function flushVersionData(): void
{
$this->info('Clearing version data cache..');
info('Clearing version data cache..');

(new SystemChecker)->flushVersionData();

$this->info('Version data cache has been cleared.');
info('Version data cache has been cleared.');
}

protected function migrateMigrations(): void
{
$this->info('Running migrations..');
info('Running migrations..');

$this->call('migrate', ['--force' => true]);
}

protected function cacheRoutes(): void
{
$this->info('Caching routes..');
info('Caching routes..');

$this->call('route:cache');
}

protected function cacheViews(): void
{
$this->info('Caching views..');
info('Caching views..');

$this->call('view:cache');
}

protected function publishAssets(): void
{
$this->info('Publishing assets..');
info('Publishing assets..');

$this->call('filament:assets');
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Feature/Console/InstallCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
test('install command works', function () {
$command = artisan('roadmap:install')
->expectsOutput('Roadmap Installation')
->expectsConfirmation('Do you want to run the migrations to set up everything fresh? (php artisan migrate:fresh)')
->expectsOutput('Let\'s create a user.')
->expectsConfirmation('Do you want to run the migrations to set up everything fresh?')
->expectsOutputToContain('Let\'s create a user.')
->expectsQuestion('Name', 'John Doe')
->expectsQuestion('Email address', '[email protected]')
->expectsQuestion('Password', 'ploiisawesome');
Expand Down
14 changes: 7 additions & 7 deletions tests/Feature/Console/UpgradeCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
test('upgrade command works', function () {
artisan('roadmap:upgrade')
->expectsOutput('Roadmap Upgrade')
->expectsOutput('Clearing version data cache..')
->expectsOutput('Version data cache has been cleared.')
->expectsOutput('Caching routes..')
->expectsOutput('Caching views..')
->expectsOutput('Running migrations..')
->expectsOutput('Upgrading done!')
->expectsOutputToContain('Clearing version data cache..')
->expectsOutputToContain('Version data cache has been cleared.')
->expectsOutputToContain('Caching routes..')
->expectsOutputToContain('Caching views..')
->expectsOutputToContain('Running migrations..')
->expectsOutputToContain('Upgrading done!')
->run();
});
})->only();

0 comments on commit 6adffc5

Please sign in to comment.