Skip to content

Commit

Permalink
add package to module converter command
Browse files Browse the repository at this point in the history
  • Loading branch information
3x1io committed Sep 16, 2024
1 parent 2223e75 commit b3d8daa
Show file tree
Hide file tree
Showing 13 changed files with 280 additions and 142 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ it will generate the files for you and you can use it directly, please note that

## List your package on the plugins list

you can list your package on the plugins list by adding this a json file in your package root folder with name `filament-plugin.json` with content like this:
you can list your package on the plugins list by adding this a json file in your package root folder with name `module.json` with content like this:

```json
{
Expand Down Expand Up @@ -122,6 +122,14 @@ make sure you allow packages scan on the `filament-plugins.php` config file
'scan' => true
```

now you can publish your package as module with the very easy way use this command

```php
php artisan filament-plugins:publish
```

and input your package name from the list and it will move it to your module folder and register the provider to you, so you can custom anything you like on the package.

## Use Selected Module in your panel

you can use the selected module in your panel by using this code in your PanelProvider
Expand Down
2 changes: 1 addition & 1 deletion resources/views/pages/table.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<div class="flex justifiy-between gap-1 my-4 px-4 border-t border-gray-100 dark:border-gray-700 pt-4">
<div class="flex justifiy-start w-full gap-2">
@if($item['type'] !== 'lib')
@if((bool)config('filament-plugins.allow_generator'))
@if(((bool)config('filament-plugins.allow_generator') ) && !str(module_path($item['module_name']))->contains('vendor'))
<x-filament::icon-button :tooltip="trans('filament-plugins::messages.plugins.actions.generate')" tag="a" href="{{route('filament.'.filament()->getCurrentPanel()->getId().'.resources.tables.index', ['module'=>$item->module_name])}}">
<x-slot name="icon">
<x-heroicon-s-cog class="w-5 h-5" />
Expand Down
36 changes: 22 additions & 14 deletions src/Console/FilamentPageGenerate.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ public function handle(): int
$moduleName = $this->argument('module') ?? text('In which Module should we create the page?', 'e.g Blog', required: true);
$moduleStudlyName = str($moduleName)->studly()->toString();
$module = Module::find($moduleName);

$module = Module::find($moduleName);
$appPath = 'app';
$moduleDir = File::directories($module->getPath());
if(in_array($module->getPath() .'/src', $moduleDir)){
$appPath = 'src';
}

$page = (string) str(
$this->argument('name') ??
text(
Expand Down Expand Up @@ -197,13 +205,13 @@ public function handle(): int
//Create Directory For Selected Panel in Module If Not Exists
if(count($pageDirectories) < 1 && count($pageNamespaces) < 1){
$modulePath = module_path($moduleName);
if(!File::exists($modulePath . '/app/Filament/'.Str::studly($panel->getId()))){
File::makeDirectory($modulePath . '/app/Filament/'.Str::studly($panel->getId()));
if(!File::exists($modulePath . '/'.$appPath . '/Filament/'.Str::studly($panel->getId()))){
File::makeDirectory($modulePath . '/'.$appPath . '/Filament/'.Str::studly($panel->getId()));
}
if(!File::exists($modulePath . '/app/Filament/'.Str::studly($panel->getId()).'/Pages')){
File::makeDirectory($modulePath . '/app/Filament/'.Str::studly($panel->getId()).'/Pages');
if(!File::exists($modulePath . '/'.$appPath . '/Filament/'.Str::studly($panel->getId()).'/Pages')){
File::makeDirectory($modulePath . '/'.$appPath . '/Filament/'.Str::studly($panel->getId()).'/Pages');
}
$pageDirectories[] = $modulePath . '/app/Filament/'.Str::studly($panel->getId()).'/Pages';
$pageDirectories[] = $modulePath . '/'.$appPath . '/Filament/'.Str::studly($panel->getId()).'/Pages';
$pageNamespaces[] = $module->appNamespace().'\\Filament\\'.Str::studly($panel->getId()).'\\Pages';
}

Expand All @@ -224,20 +232,20 @@ public function handle(): int
//Create Directory For Selected Panel in Module If Not Exists
if(count($resourceDirectories) < 1 && count($resourceNamespaces) < 1){
$modulePath = module_path($moduleName);
if(!File::exists($modulePath . '/app/Filament/'.Str::studly($panel->getId()))){
File::makeDirectory($modulePath . '/app/Filament/'.Str::studly($panel->getId()));
if(!File::exists($modulePath . '/'.$appPath . '/Filament/'.Str::studly($panel->getId()))){
File::makeDirectory($modulePath . '/'.$appPath . '/Filament/'.Str::studly($panel->getId()));
}
if(!File::exists($modulePath . '/app/Filament/'.Str::studly($panel->getId()).'/Resource')){
File::makeDirectory($modulePath . '/app/Filament/'.Str::studly($panel->getId()).'/Resource');
if(!File::exists($modulePath . '/'.$appPath . '/Filament/'.Str::studly($panel->getId()).'/Resource')){
File::makeDirectory($modulePath . '/'.$appPath . '/Filament/'.Str::studly($panel->getId()).'/Resource');
}
if(!File::exists($modulePath . '/app/Filament/'.Str::studly($panel->getId()).'/Resource/'.$resource)){
File::makeDirectory($modulePath . '/app/Filament/'.Str::studly($panel->getId()).'/Resource/'.$resource);
if(!File::exists($modulePath . '/'.$appPath . '/Filament/'.Str::studly($panel->getId()).'/Resource/'.$resource)){
File::makeDirectory($modulePath . '/'.$appPath . '/Filament/'.Str::studly($panel->getId()).'/Resource/'.$resource);
}
if(!File::exists($modulePath . '/app/Filament/'.Str::studly($panel->getId()).'/Resource/'.$resource.'/Pages')){
File::makeDirectory($modulePath . '/app/Filament/'.Str::studly($panel->getId()).'/Resource/'.$resource.'/Pages');
if(!File::exists($modulePath . '/'.$appPath . '/Filament/'.Str::studly($panel->getId()).'/Resource/'.$resource.'/Pages')){
File::makeDirectory($modulePath . '/'.$appPath . '/Filament/'.Str::studly($panel->getId()).'/Resource/'.$resource.'/Pages');
}

$resourceDirectories[] = $modulePath . '/app/Filament/'.Str::studly($panel->getId()).'/Resource/'.$resource.'/Pages';
$resourceDirectories[] = $modulePath . '/'.$appPath . '/Filament/'.Str::studly($panel->getId()).'/Resource/'.$resource.'/Pages';
$resourceNamespaces[] = $module->appNamespace().'\\Filament\\'.Str::studly($panel->getId()).'\\Resource\\'.$resource.'\\Pages';
}

Expand Down
111 changes: 111 additions & 0 deletions src/Console/FilamentPublishModule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

namespace TomatoPHP\FilamentPlugins\Console;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use Nwidart\Modules\Module;
use TomatoPHP\ConsoleHelpers\Traits\RunCommand;
use TomatoPHP\FilamentPlugins\Services\PublishPackage;
use function Laravel\Prompts\search;

class FilamentPublishModule extends Command
{
use RunCommand;

private ?Module $module = null;
private ?string $newPath = null;

/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'filament-plugins:publish {module?}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'publish a module';

public function __construct()
{
parent::__construct();
}


public function handle()
{
$modules = collect(\Nwidart\Modules\Facades\Module::all())->filter(function ($item){
if(str($item->getPath())->contains('vendor')){
return true;
}

return false;
});
$module = $this->argument('module') && $this->argument('module') != "0" ? $this->argument('module') : search(
label: 'Please input your module name you went to publish?',
options: fn (string $value) => strlen($value) > 0
? $modules->filter(function ($item, $key) use ($value){
return str($item->getName())->contains($value) ? (string)$item->getName() : null;
})->toArray()
: [],
placeholder: "ex: FilamentAccounts",
scroll: 10
);

$this->module = \Nwidart\Modules\Facades\Module::find($module);

$this->info("Publishing module: {$module}");

$this->moveFolder();
$this->registerProvider();
$this->updateComposer();


$this->info("Module: {$module} published successfully");
}


public function moveFolder()
{
$basePath = $this->module->getPath();
$newFolderName = $this->module->getStudlyName();

File::move($basePath, base_path("Modules/{$newFolderName}"));

$this->newPath = base_path("Modules/{$newFolderName}");
}

public function updateComposer()
{
$composerJson = json_decode(File::get(base_path('composer.json')), true);
$packageName = str($this->module->getPath())->remove(base_path('/vendor/'));
$composerCollect = collect($composerJson['require'])->filter(function ($item, $key) use ($packageName){
return !str($key)->contains($packageName);
});
$composerJson['require'] = $composerCollect->toArray();

File::put(base_path('composer.json'), json_encode($composerJson, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES ));

system('composer update');
}

public function registerProvider()
{
$info = json_decode(File::get($this->newPath . "/module.json"));
$providers = include base_path('bootstrap/providers.php');
foreach ($info->providers as $provider){
$providers[] = $provider;
}

$array = "";
foreach ($providers as $provider){
$array .= "\t".$provider."::class,\n";
}
File::put(base_path('bootstrap/providers.php'), "<?php\nreturn [\n ".$array." \n];\n");
}

}
15 changes: 10 additions & 5 deletions src/Console/FilamentResourceGenerate.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public function handle(): int
$moduleName = $this->argument('module') ?? text('In which Module should we create the resource?', 'e.g Blog', required: true);
$moduleStudlyName = str($moduleName)->studly()->toString();
$module = Module::find($moduleName);
$appPath = 'app';
$moduleDir = File::directories($module->getPath());
if(in_array($module->getPath() .'/src', $moduleDir)){
$appPath = 'src';
}
$modelNamespace = $this->option('model-namespace') ?? $module->appNamespace('Models');
$modelNamespace = str($modelNamespace)->rtrim('\\')->toString();

Expand Down Expand Up @@ -91,13 +96,13 @@ public function handle(): int
//Create Directory For Selected Panel in Module If Not Exists
if(count($resourceDirectories) < 1 && count($resourceNamespaces) < 1){
$modulePath = module_path($moduleName);
if(!File::exists($modulePath . '/app/Filament/'.Str::studly($panel->getId()))){
File::makeDirectory($modulePath . '/app/Filament/'.Str::studly($panel->getId()));
if(!File::exists($modulePath . '/'.$appPath . '//Filament/'.Str::studly($panel->getId()))){
File::makeDirectory($modulePath . '/'.$appPath . '//Filament/'.Str::studly($panel->getId()));
}
if(!File::exists($modulePath . '/app/Filament/'.Str::studly($panel->getId()).'/Resources')){
File::makeDirectory($modulePath . '/app/Filament/'.Str::studly($panel->getId()).'/Resources');
if(!File::exists($modulePath . '/'.$appPath . '//Filament/'.Str::studly($panel->getId()).'/Resources')){
File::makeDirectory($modulePath . '/'.$appPath . '//Filament/'.Str::studly($panel->getId()).'/Resources');
}
$resourceDirectories[] = $modulePath . '/app/Filament/'.Str::studly($panel->getId()).'/Resources';
$resourceDirectories[] = $modulePath . '/'.$appPath . '//Filament/'.Str::studly($panel->getId()).'/Resources';
$resourceNamespaces[] = $module->appNamespace().'\\Filament\\'.Str::studly($panel->getId()).'\\Resources';
}

Expand Down
33 changes: 19 additions & 14 deletions src/Console/FilamentWidgetGenerate.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public function handle(): int
$moduleName = $this->argument('module') ?? text('In which Module should we create this?', 'e.g Blog', required: true);
$moduleStudlyName = str($moduleName)->studly()->toString();
$module = Module::find($moduleName);
$appPath = 'app';
$moduleDir = File::directories($module->getPath());
if(in_array($module->getPath() .'/src', $moduleDir)){
$appPath = 'src';
}
$widget = (string) str($this->argument('name') ?? text(
label: 'What is the widget name?',
placeholder: 'BlogPostsChart',
Expand Down Expand Up @@ -118,13 +123,13 @@ public function handle(): int
//Create Directory For Selected Panel in Module If Not Exists
if(count($widgetDirectories) < 1 && count($widgetNamespaces) < 1){
$modulePath = module_path($moduleName);
if(!File::exists($modulePath . '/app/Filament/'.Str::studly($panel->getId()))){
File::makeDirectory($modulePath . '/app/Filament/'.Str::studly($panel->getId()));
if(!File::exists($modulePath . '/'.$appPath . '//Filament/'.Str::studly($panel->getId()))){
File::makeDirectory($modulePath . '/'.$appPath . '//Filament/'.Str::studly($panel->getId()));
}
if(!File::exists($modulePath . '/app/Filament/'.Str::studly($panel->getId()).'/Widgets')){
File::makeDirectory($modulePath . '/app/Filament/'.Str::studly($panel->getId()).'/Widgets');
if(!File::exists($modulePath . '/'.$appPath . '//Filament/'.Str::studly($panel->getId()).'/Widgets')){
File::makeDirectory($modulePath . '/'.$appPath . '//Filament/'.Str::studly($panel->getId()).'/Widgets');
}
$widgetDirectories[] = $modulePath . '/app/Filament/'.Str::studly($panel->getId()).'/Widgets';
$widgetDirectories[] = $modulePath . '/'.$appPath . '//Filament/'.Str::studly($panel->getId()).'/Widgets';
$widgetNamespaces[] = $module->appNamespace().'\\Filament\\'.Str::studly($panel->getId()).'\\Widgets';
}

Expand All @@ -144,20 +149,20 @@ public function handle(): int
//Create Directory For Selected Panel in Module If Not Exists
if(count($resourceDirectories) < 1 && count($resourceNamespaces) < 1){
$modulePath = module_path($moduleName);
if(!File::exists($modulePath . '/app/Filament/'.Str::studly($panel->getId()))){
File::makeDirectory($modulePath . '/app/Filament/'.Str::studly($panel->getId()));
if(!File::exists($modulePath . '/'.$appPath . '//Filament/'.Str::studly($panel->getId()))){
File::makeDirectory($modulePath . '/'.$appPath . '//Filament/'.Str::studly($panel->getId()));
}
if(!File::exists($modulePath . '/app/Filament/'.Str::studly($panel->getId()).'/Resource')){
File::makeDirectory($modulePath . '/app/Filament/'.Str::studly($panel->getId()).'/Resource');
if(!File::exists($modulePath . '/'.$appPath . '//Filament/'.Str::studly($panel->getId()).'/Resource')){
File::makeDirectory($modulePath . '/'.$appPath . '//Filament/'.Str::studly($panel->getId()).'/Resource');
}
if(!File::exists($modulePath . '/app/Filament/'.Str::studly($panel->getId()).'/Resource/'.$resource)){
File::makeDirectory($modulePath . '/app/Filament/'.Str::studly($panel->getId()).'/Resource/'.$resource);
if(!File::exists($modulePath . '/'.$appPath . '//Filament/'.Str::studly($panel->getId()).'/Resource/'.$resource)){
File::makeDirectory($modulePath . '/'.$appPath . '//Filament/'.Str::studly($panel->getId()).'/Resource/'.$resource);
}
if(!File::exists($modulePath . '/app/Filament/'.Str::studly($panel->getId()).'/Resource/'.$resource.'/Widgets')){
File::makeDirectory($modulePath . '/app/Filament/'.Str::studly($panel->getId()).'/Resource/'.$resource.'/Widgets');
if(!File::exists($modulePath . '/'.$appPath . '//Filament/'.Str::studly($panel->getId()).'/Resource/'.$resource.'/Widgets')){
File::makeDirectory($modulePath . '/'.$appPath . '//Filament/'.Str::studly($panel->getId()).'/Resource/'.$resource.'/Widgets');
}

$resourceDirectories[] = $modulePath . '/app/Filament/'.Str::studly($panel->getId()).'/Resource/'.$resource.'/Widgets';
$resourceDirectories[] = $modulePath . '/'.$appPath . '//Filament/'.Str::studly($panel->getId()).'/Resource/'.$resource.'/Widgets';
$resourceNamespaces[] = $module->appNamespace().'\\Filament\\'.Str::studly($panel->getId()).'\\Resource\\'.$resource.'\\Widgets';
}

Expand Down
4 changes: 2 additions & 2 deletions src/FilamentPluginsPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public function register(Panel $panel): void
foreach ($plugins as $plugin){
if($plugin->type === 'plugin' && in_array($plugin->module_name, $this->modules)){
$module = Module::find($plugin->module_name);
if($module->isEnabled()){
$dir = File::directories($module->getPath());
if($module->isEnabled() && !in_array($module->getPath() . DIRECTORY_SEPARATOR . 'src', $dir)){
$checkIfThereIsDirectoryForThisPanel = File::exists($module->appPath('Filament' . DIRECTORY_SEPARATOR . Str::studly($panel->getId())));
if($checkIfThereIsDirectoryForThisPanel && $this->discoverCurrentPanelOnly){
$panel->discoverPages(
Expand Down Expand Up @@ -92,7 +93,6 @@ public function register(Panel $panel): void
);
}
}

}
}
}
Expand Down
Loading

0 comments on commit b3d8daa

Please sign in to comment.