|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace TomatoPHP\LaravelPackageGenerator\Console; |
| 4 | + |
| 5 | +use Illuminate\Console\Command; |
| 6 | +use Illuminate\Support\Facades\File; |
| 7 | +use Illuminate\Support\Str; |
| 8 | +use TomatoPHP\ConsoleHelpers\Traits\HandleFiles; |
| 9 | +use TomatoPHP\ConsoleHelpers\Traits\HandleStub; |
| 10 | +use TomatoPHP\ConsoleHelpers\Traits\RunCommand; |
| 11 | + |
| 12 | +class FilamentTestGenerator extends Command |
| 13 | +{ |
| 14 | + use RunCommand; |
| 15 | + use HandleFiles; |
| 16 | + use HandleStub; |
| 17 | + |
| 18 | + private string $stubPath; |
| 19 | + |
| 20 | + /** |
| 21 | + * The name and signature of the console command. |
| 22 | + * |
| 23 | + * @var string |
| 24 | + */ |
| 25 | + protected $name = 'package:tests'; |
| 26 | + |
| 27 | + /** |
| 28 | + * The console command description. |
| 29 | + * |
| 30 | + * @var string |
| 31 | + */ |
| 32 | + protected $description = 'use this command to generate package boilerplate'; |
| 33 | + |
| 34 | + public function __construct() |
| 35 | + { |
| 36 | + parent::__construct(); |
| 37 | + $this->publish = __DIR__ .'/../../publish/'; |
| 38 | + $this->stubPath = config('laravel-package-generator.stub-path'); |
| 39 | + } |
| 40 | + |
| 41 | + |
| 42 | + /** |
| 43 | + * Execute the console command. |
| 44 | + * |
| 45 | + * @return mixed |
| 46 | + */ |
| 47 | + public function handle() |
| 48 | + { |
| 49 | + $packageName = null; |
| 50 | + while(empty($packageName)){ |
| 51 | + $packageName = $this->ask("Enter your package name"); |
| 52 | + if(is_numeric($packageName[0])){ |
| 53 | + $this->error("Package name can't start with a number"); |
| 54 | + $packageName = null; |
| 55 | + } |
| 56 | + } |
| 57 | + $packageString = Str::of($packageName); |
| 58 | + |
| 59 | + $packageVendor = null; |
| 60 | + while(empty($packageVendor)){ |
| 61 | + $packageVendor = $this->ask("Enter your package vendor name"); |
| 62 | + if(is_numeric($packageVendor[0])){ |
| 63 | + $this->error("Vendor name can't start with a number"); |
| 64 | + $packageVendor = null; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + $packageVendorString = Str::of($packageVendor); |
| 69 | + |
| 70 | + //Package Meta |
| 71 | + $packageDescription = $this->ask("Enter your package description", "your package description will go here"); |
| 72 | + $packageAuthor = $this->ask("Enter your package author", "Queen Tech"); |
| 73 | + $packageAuthorEmail = $this->ask("Enter your package author email", "git@queentechsoltions.net"); |
| 74 | + |
| 75 | + //Check Options |
| 76 | + $packageConfig = $this->ask("Has Config file? (yes/no)", "yes")?: "yes"; |
| 77 | + $packageRoute = $this->ask("Has Routes? (yes/no)", "yes")?: "yes"; |
| 78 | + $packageView = $this->ask("Has Views? (yes/no)", "yes")?: "yes"; |
| 79 | + $packageMigration = $this->ask("Has Migrations? (yes/no)", "yes")?: "yes"; |
| 80 | + |
| 81 | + $this->info('Generating package boilerplate...'); |
| 82 | + |
| 83 | + //create package directory |
| 84 | + if(!File::exists(base_path(config('laravel-package-generator.packages-folder')))){ |
| 85 | + File::makeDirectory(base_path(config('laravel-package-generator.packages-folder'))); |
| 86 | + } |
| 87 | + |
| 88 | + $packageVendorPath = $packageVendorString |
| 89 | + ->replace(' ', '-') |
| 90 | + ->replace('_', '-') |
| 91 | + ->lower() |
| 92 | + ->toString(); |
| 93 | + |
| 94 | + //Create vendor directory |
| 95 | + if(!File::exists(base_path(config('laravel-package-generator.packages-folder')) . "/" .$packageVendorPath)){ |
| 96 | + File::makeDirectory(base_path(config('laravel-package-generator.packages-folder')) . "/" .$packageVendorPath); |
| 97 | + } |
| 98 | + |
| 99 | + $packageNamePath = $packageString |
| 100 | + ->replace(' ', '-') |
| 101 | + ->replace('_', '-') |
| 102 | + ->lower() |
| 103 | + ->toString(); |
| 104 | + |
| 105 | + //Create package directory |
| 106 | + if(!File::exists(base_path(config('laravel-package-generator.packages-folder')) . "/" .$packageVendor . "/" . $packageNamePath)){ |
| 107 | + File::makeDirectory(base_path(config('laravel-package-generator.packages-folder')) . "/" .$packageVendor . "/" . $packageNamePath); |
| 108 | + } |
| 109 | + |
| 110 | + $packagePath = base_path(config('laravel-package-generator.packages-folder')) . "/" .$packageVendorPath . "/" . $packageNamePath; |
| 111 | + |
| 112 | + //Build a package inside vendor directory |
| 113 | + $packageConfig !== 'yes' ? null : $this->handelFile('config', $packagePath. "/config", 'folder'); |
| 114 | + $packageMigration !== 'yes' ? null : $this->handelFile('database', $packagePath. "/database", 'folder'); |
| 115 | + $packageView !== 'yes' ? null : $this->handelFile('resources', $packagePath. "/resources", 'folder'); |
| 116 | + $packageRoute !== 'yes' ? null : $this->handelFile('routes', $packagePath. "/routes", 'folder'); |
| 117 | + $this->handelFile('src', $packagePath. "/src", 'folder'); |
| 118 | + $this->handelFile('tests', $packagePath. "/tests", 'folder'); |
| 119 | + $this->handelFile('LICENSE.md', $packagePath. "/LICENSE.md"); |
| 120 | + $this->handelFile('CHANGELOG.md', $packagePath. "/CHANGELOG.md"); |
| 121 | + $this->handelFile('SECURITY.md', $packagePath. "/SECURITY.md"); |
| 122 | + |
| 123 | + |
| 124 | + $vendorNamespace = Str::of($packageVendorPath)->camel()->ucfirst()->toString(); |
| 125 | + $packageNamespace = Str::of($packageNamePath)->camel()->ucfirst()->toString(); |
| 126 | + $packageProviderName = Str::of($packageNamePath)->camel()->ucfirst()->toString() . "ServiceProvider"; |
| 127 | + $fullNameSpace = $vendorNamespace . "\\". $packageNamespace; |
| 128 | + |
| 129 | + //Build Stubs Files |
| 130 | + $commandClassName = $packageString->studly()->append('Install')->toString(); |
| 131 | + $this->generateStubs( |
| 132 | + $this->stubPath . 'command.stub', |
| 133 | + $packagePath . '/src/Console/'. $commandClassName . ".php", |
| 134 | + [ |
| 135 | + "namespace" => $fullNameSpace, |
| 136 | + "name" => $commandClassName, |
| 137 | + "command" => $packageNamePath, |
| 138 | + "packageName" => Str::of($packageNamePath)->camel()->toString(), |
| 139 | + "provider" => $packageProviderName |
| 140 | + ], |
| 141 | + [ |
| 142 | + $packagePath . '/src/Console/' |
| 143 | + ] |
| 144 | + ); |
| 145 | + |
| 146 | + //Build Provider Register Methods |
| 147 | + $register = collect([]); |
| 148 | + $register->push('//Register generate command'); |
| 149 | + $register->push(' $this->commands(['); |
| 150 | + $register->push(' '."\\".$fullNameSpace."\\Console\\".$commandClassName.'::class,'); |
| 151 | + $register->push(' ]);'); |
| 152 | + $register->push(" "); |
| 153 | + if($packageConfig === 'yes'){ |
| 154 | + $register->push(' //Register Config file'); |
| 155 | + $register->push(' $this->mergeConfigFrom(__DIR__.\'/../config/'.$packageNamePath.'.php\', \''.$packageNamePath.'\');'); |
| 156 | + $register->push(" "); |
| 157 | + $register->push(' //Publish Config'); |
| 158 | + $register->push(' $this->publishes(['); |
| 159 | + $register->push(' __DIR__.\'/../config/'.$packageNamePath.'.php\' => config_path(\''.$packageNamePath.'.php\'),'); |
| 160 | + $register->push(' ], \''.$packageNamePath.'-config\');'); |
| 161 | + $register->push(" "); |
| 162 | + |
| 163 | + //Generate Config file |
| 164 | + $this->generateStubs( |
| 165 | + $this->stubPath . 'config.stub', |
| 166 | + $packagePath . '/config/'.$packageNamePath.'.php', |
| 167 | + [ |
| 168 | + ], |
| 169 | + ); |
| 170 | + } |
| 171 | + if($packageMigration === 'yes'){ |
| 172 | + $register->push(' //Register Migrations'); |
| 173 | + $register->push(' $this->loadMigrationsFrom(__DIR__.\'/../database/migrations\');'); |
| 174 | + $register->push(" "); |
| 175 | + $register->push(' //Publish Migrations'); |
| 176 | + $register->push(' $this->publishes(['); |
| 177 | + $register->push(' __DIR__.\'/../database/migrations\' => database_path(\'migrations\'),'); |
| 178 | + $register->push(' ], \''.$packageNamePath.'-migrations\');'); |
| 179 | + } |
| 180 | + if($packageView === 'yes'){ |
| 181 | + $register->push(' //Register views'); |
| 182 | + $register->push(' $this->loadViewsFrom(__DIR__.\'/../resources/views\', \''.$packageNamePath.'\');'); |
| 183 | + $register->push(" "); |
| 184 | + $register->push(' //Publish Views'); |
| 185 | + $register->push(' $this->publishes(['); |
| 186 | + $register->push(' __DIR__.\'/../resources/views\' => resource_path(\'views/vendor/'.$packageNamePath.'\'),'); |
| 187 | + $register->push(' ], \''.$packageNamePath.'-views\');'); |
| 188 | + $register->push(" "); |
| 189 | + $register->push(' //Register Langs'); |
| 190 | + $register->push(' $this->loadTranslationsFrom(__DIR__.\'/../resources/lang\', \''.$packageNamePath.'\');'); |
| 191 | + $register->push(" "); |
| 192 | + $register->push(' //Publish Lang'); |
| 193 | + $register->push(' $this->publishes(['); |
| 194 | + $register->push(' __DIR__.\'/../resources/lang\' => base_path(\'lang/vendor/'.$packageNamePath.'\'),'); |
| 195 | + $register->push(' ], \''.$packageNamePath.'-lang\');'); |
| 196 | + $register->push(" "); |
| 197 | + } |
| 198 | + if($packageRoute === 'yes') { |
| 199 | + $register->push(' //Register Routes'); |
| 200 | + $register->push(' $this->loadRoutesFrom(__DIR__.\'/../routes/web.php\');'); |
| 201 | + $register->push(" "); |
| 202 | + } |
| 203 | + |
| 204 | + //Generate Provider File |
| 205 | + $this->generateStubs( |
| 206 | + $this->stubPath . 'provider.stub', |
| 207 | + $packagePath . '/src/'. $packageProviderName . ".php", |
| 208 | + [ |
| 209 | + "namespace" => $fullNameSpace, |
| 210 | + "name" => $packageProviderName, |
| 211 | + "register" => $register->implode("\n") |
| 212 | + ], |
| 213 | + [ |
| 214 | + $packagePath . '/src' |
| 215 | + ] |
| 216 | + ); |
| 217 | + |
| 218 | + //Generate Composer.json file |
| 219 | + $this->generateStubs( |
| 220 | + $this->stubPath . 'composer.stub', |
| 221 | + $packagePath . '/composer.json', |
| 222 | + [ |
| 223 | + "vendor" => $packageVendorPath, |
| 224 | + "package" => $packageNamePath, |
| 225 | + "description" => $packageDescription, |
| 226 | + "namespace" => $vendorNamespace ."\\\\". $packageNamespace, |
| 227 | + "provider" => $packageProviderName, |
| 228 | + "author" => $packageAuthor, |
| 229 | + "email" => $packageAuthorEmail, |
| 230 | + ], |
| 231 | + [ |
| 232 | + $packagePath |
| 233 | + ] |
| 234 | + ); |
| 235 | + |
| 236 | + |
| 237 | + |
| 238 | + //Generate Readme.md file |
| 239 | + $this->generateStubs( |
| 240 | + $this->stubPath . 'readme.stub', |
| 241 | + $packagePath . '/README.md', |
| 242 | + [ |
| 243 | + "name" => Str::of($packageNamePath)->replace('-', ' ')->ucfirst()->toString(), |
| 244 | + "vendor" => $packageVendorPath, |
| 245 | + "command" => $packageNamePath, |
| 246 | + "package" => $packageNamePath, |
| 247 | + "description" => $packageDescription, |
| 248 | + "author" => $packageAuthor, |
| 249 | + "email" => $packageAuthorEmail, |
| 250 | + "vendorName" => $vendorNamespace, |
| 251 | + ], |
| 252 | + [ |
| 253 | + $packagePath |
| 254 | + ] |
| 255 | + ); |
| 256 | + |
| 257 | + $this->info('Package boilerplate generated successfully'); |
| 258 | + } |
| 259 | +} |
0 commit comments