Skip to content

Commit

Permalink
Update InstallCommand in Page
Browse files Browse the repository at this point in the history
  • Loading branch information
Kim-the-Diamond committed Mar 7, 2024
1 parent 6c82b5e commit 529778d
Showing 1 changed file with 137 additions and 6 deletions.
143 changes: 137 additions & 6 deletions packages/page/src/Commands/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@
namespace Moox\Page\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Schema;

use function Laravel\Prompts\alert;
use function Laravel\Prompts\confirm;
use function Laravel\Prompts\info;
use function Laravel\Prompts\multiselect;
use function Laravel\Prompts\note;
use function Laravel\Prompts\warning;

class InstallCommand extends Command
{
Expand All @@ -25,12 +34,134 @@ class InstallCommand extends Command
*/
public function handle()
{
$this->comment('Publishing Page Configuration...');
$this->callSilent('vendor:publish', ['--tag' => 'page-config']);
$this->art();
$this->welcome();
$this->publish_configuration();
$this->publish_migrations();
$this->run_migrations();
$this->register_plugins();
$this->finish();
}

public function art(): void
{
info('
▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓
▓▓▒░░▒▓▓▒▒░░░░░░▒▒▓▓▓▒░░░░░░░▒▓▓ ▓▓▓▓▒░░░░░░░▒▓▓▓▓ ▓▓▓▓▓▒░░░░░░░▒▒▓▓▓▓▓▒▒▒▒▓▓ ▓▓▓▒▒▒▒▓▓
▓▒░░░░░░░░░░░░░░░░░░░░░░░░░░░░░▓▓▓▓▓▒░░░░░░░░░░░░░▒▓▓▓ ▓▓▓▓▒░░░░░░░░░░░░░▒▓▓▓░░░░░▒▓▓ ▓▓▒░░░░░▓▓
▓▒░░░░░░▒▓▓▓▓▒░░░░░░░▒▓▓▓▓░░░░░▒▓▓▓░░░░░▒▓▓▓▓▒░░░░░░░▓▓▓▓░░░░░░▒▓▓▓▓▓░░░░░░▒▓▓░░░░░▒▓▓▓▓▓░░░░░▒▓▓
▓▒░░░░▓▓▓▓ ▓▓░░░░░▓▓▓ ▓▓▓░░░░▒▓▓░░░░▒▓▓▓ ▓▓▓▓░░░░░▓░░░░░░▓▓▓▓ ▓▓▓▒░░░░▓▓▓▒░░░░░▓▓▓░░░░░▓▓▓
▓▒░░░░▒▓ ▓▓░░░░░▓▓ ▓▓░░░░▒▓░░░░▒▓▓ ▓▓▓░░▒░░░░░▓▓▓ ▓▓░░░░▒▓▓▓▓░░░░░░░░░░░▓▓
▓▒░░░░▒▓ ▓▓░░░░░▓▓ ▓▓░░░░▒▓░░░░▒▓ ▓▓▓░░░░░▒▓▓ ▓▓▒░░░░▓ ▓▓▓░░░░░░░░░▓▓
▓▒░░░░▒▓ ▓▓░░░░░▓▓ ▓▓░░░░▒▓░░░░▒▓▓ ▓▓▒░░░░░▒░░▒▓▓ ▓▓░░░░▒▓▓▓▒░░░░░▒░░░░░▒▓
▓▒░░░░▒▓ ▓▓░░░░░▓▓ ▓▓░░░░▒▓▓░░░░▒▓▓▓ ▓▓▓▒░░░░░▒▒░░░░░▒▓▓▓ ▓▓▓░░░░░▓▓▓░░░░░▒▓▓▓░░░░░▒▓▓
▓▒░░░░▒▓ ▓▓░░░░░▓▓ ▓▓░░░░▒▓▓▓░░░░░░▒▒▓▓▒░░░░░░▒▓▓▓▓░░░░░░░▒▒▓▓▒░░░░░░▓▓▓░░░░░▒▓▓▓▓▓▒░░░░░▓▓
▓▒░░░░▒▓ ▓▓░░░░░▓▓ ▓▓░░░░▒▓▓▓▓▒░░░░░░░░░░░░░▒▓▓▓ ▓▓▓▓▒░░░░░░░░░░░░░▒▓▓▒░░░░░▓▓▓ ▓▓▒░░░░░▒▓
▓▓░░░▒▓▓ ▓▓▒░░░▒▓▓ ▓▓░░░░▓▓ ▓▓▓▓▒░░░░░░▒▒▓▓▓▓ ▓▓▓▓▓▒▒░░░░░▒▒▓▓▓▓▓░░░░▒▓▓ ▓▓▓░░░░▒▓
▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓
');
}

public function welcome(): void
{
info('Welcome to Moox Page Installer');
}

public function publish_configuration(): void
{
if (confirm('Do you wish to publish the configuration?', true)) {
if (! File::exists('config/page.php')) {
info('Publishing Page Configuration...');
$this->call('vendor:publish', ['--tag' => 'page-config']);
} else {
warning('The Page config already exist. The config will not be published.');
}
}
}

public function publish_migrations(): void
{
if (confirm('Do you wish to publish the migrations?', true)) {
if (Schema::hasTable('pages')) {
warning('The pages table already exists. The migrations will not be published.');
} else {
info('Publishing Page Migrations...');
$this->callSilent('vendor:publish', ['--tag' => 'page-migrations']);
}
}
}

public function run_migrations(): void
{
if (confirm('Do you wish to run the migrations?', true)) {
info('Running Page Migrations...');
$this->call('migrate');
}
}

public function register_plugins(): void
{
$providerPath = app_path('Providers/Filament/AdminPanelProvider.php');

if (File::exists($providerPath)) {

$content = File::get($providerPath);

$intend = ' ';

$namespace = "\Moox\Page";

$pluginsToAdd = multiselect(
label: 'Which plugins do you want to register?',
options: ['PagePlugin'],
default: ['PagePlugin'],
);

$function = '::make(),';

$pattern = '/->plugins\(\[([\s\S]*?)\]\);/';
$newPlugins = '';

foreach ($pluginsToAdd as $plugin) {
$searchPlugin = '/'.$plugin.'/';
if (preg_match($searchPlugin, $content)) {
warning("$plugin already registered.");
} else {
$newPlugins .= $intend.$namespace.'\\'.$plugin.$function."\n";
}
}

if ($newPlugins) {

$this->comment('Publishing Page Migrations...');
$this->callSilent('vendor:publish', ['--tag' => 'page-migrations']);
$this->call('migrate');
$this->info('Moox Page was installed successfully');
if (preg_match($pattern, $content)) {
info('Plugins section found. Adding new plugins...');

$replacement = "->plugins([$1\n$newPlugins\n ]);";
$newContent = preg_replace($pattern, $replacement, $content);

} else {
info('Plugins section created. Adding new plugins...');

$pluginsSection = " ->plugins([\n$newPlugins\n ]);";
$placeholderPattern = '/(\->authMiddleware\(\[.*?\]\))\s*\;/s';
$replacement = "$1\n".$pluginsSection;
$newContent = preg_replace($placeholderPattern, $replacement, $content, 1);
}

File::put($providerPath, $newContent);
}

} else {

alert('AdminPanelProvider not found. You need to add the plugins manually.');
}

}

public function finish(): void
{
note('Moox Page installed successfully. Enjoy!');
}
}

0 comments on commit 529778d

Please sign in to comment.