diff --git a/README.md b/README.md index d4ddbf8b..b1067c38 100644 --- a/README.md +++ b/README.md @@ -6,12 +6,14 @@ $ composer require --dev ucan-lab/laravel-dacapo ``` -## Publish the schema file +## Generate default schema.yml ``` -$ php artisan vendor:publish --provider="UcanLab\LaravelDacapo\Providers\ConsoleServiceProvider" +$ php artisan dacapo:init ``` +`database/schemas/default.yml` + ## Generate migration files from schema.yml ``` diff --git a/src/Console/DacapoClearCommand.php b/src/Console/DacapoClearCommand.php index e58acfc8..61800d2e 100644 --- a/src/Console/DacapoClearCommand.php +++ b/src/Console/DacapoClearCommand.php @@ -40,7 +40,7 @@ public function __construct() /** * @return void */ - public function handle() + public function handle(): void { if (! $this->confirmToProceed()) { return; @@ -54,7 +54,7 @@ public function handle() * * @return void */ - private function clearMigrationDirectory() + private function clearMigrationDirectory(): void { File::deleteDirectory(database_path('migrations')); File::makeDirectory(database_path('migrations')); diff --git a/src/Console/DacapoGenerateCommand.php b/src/Console/DacapoGenerateCommand.php index f2ac1719..8da338c7 100644 --- a/src/Console/DacapoGenerateCommand.php +++ b/src/Console/DacapoGenerateCommand.php @@ -23,6 +23,7 @@ class DacapoGenerateCommand extends Command {--fresh : Drop all tables and re-run all migrations} {--refresh : Reset and re-run all migrations} {--seed : Seed the database with records} + {--m|model : Run make model command} '; /** @@ -35,7 +36,7 @@ class DacapoGenerateCommand extends Command /** * @return void */ - public function handle() + public function handle(): void { if (! $this->confirmToProceed()) { return; @@ -43,7 +44,7 @@ public function handle() $this->call('dacapo:clear', ['--force' => true]); - (new DacapoGenerator())->run(); + (new DacapoGenerator($this->option('model')))->run(); $this->info('Generated migration files.'); if ($this->option('seed')) { diff --git a/src/Console/DacapoInitCommand.php b/src/Console/DacapoInitCommand.php index f706e0b8..645a870a 100644 --- a/src/Console/DacapoInitCommand.php +++ b/src/Console/DacapoInitCommand.php @@ -31,6 +31,18 @@ class DacapoInitCommand extends Command */ public function handle() { + if ($this->existsSchemasDirectory()) { + if ($this->ask('The database/schemas directory already exists. Initialize ? [y/N]')) { + $this->deleteSchemasDirectory(); + $this->makeSchemasDirectory(); + } else { + $this->comment('Command Cancelled!'); + return; + } + } else { + $this->makeSchemasDirectory(); + } + $this->initSchema(); } @@ -39,14 +51,54 @@ public function handle() * * @return void */ - private function initSchema() + private function initSchema(): void { if ($this->option('legacy')) { - File::copy(__DIR__ . '/../Storage/schemas/schema.legacy.yml', database_path('schema.yml')); + File::copy($this->getStoragePath() . '/default.legacy.yml', database_path('schemas/default.yml')); } else { - File::copy(__DIR__ . '/../Storage/schemas/schema.yml', database_path('schema.yml')); + File::copy($this->getStoragePath() . '/default.yml', database_path('schemas/default.yml')); } $this->info('Init dacapo default schema yaml.'); } + + /** + * @return bool + */ + private function existsSchemasDirectory(): bool + { + return File::exists($this->getSchemasPath()); + } + + /** + * @return bool + */ + private function makeSchemasDirectory(): bool + { + return File::makeDirectory($this->getSchemasPath()); + } + + /** + * @return bool + */ + private function deleteSchemasDirectory(): bool + { + return File::deleteDirectory($this->getSchemasPath()); + } + + /** + * @return string + */ + private function getSchemasPath(): string + { + return database_path('schemas'); + } + + /** + * @return string + */ + private function getStoragePath(): string + { + return __DIR__ . '/../Storage/schemas'; + } } diff --git a/src/Console/DacapoUninstallCommand.php b/src/Console/DacapoUninstallCommand.php new file mode 100644 index 00000000..53f33e60 --- /dev/null +++ b/src/Console/DacapoUninstallCommand.php @@ -0,0 +1,63 @@ +uninstallDacapo(); + } + + /** + * Uninstall dacapo + * + * @return void + */ + private function uninstallDacapo(): void + { + $this->deleteSchemasDirectory(); + $this->info('Deleted schemas directory.'); + $this->info('Please delete dacapo composer package.'); + $this->comment('composer remove --dev ucan-lab/laravel-dacapo'); + } + + /** + * @return bool + */ + private function deleteSchemasDirectory(): bool + { + return File::deleteDirectory($this->getSchemasPath()); + } + + /** + * @return string + */ + private function getSchemasPath(): string + { + return database_path('schemas'); + } +} diff --git a/src/Generator/ModelTemplateGenerator.php b/src/Generator/ModelTemplateGenerator.php new file mode 100644 index 00000000..afbe58a8 --- /dev/null +++ b/src/Generator/ModelTemplateGenerator.php @@ -0,0 +1,41 @@ +tables = $tables; + } + + public function run(): void + { + foreach ($this->tables as $table) { + if (! $this->existsModel($table->getModelName())) { + Artisan::call('make:model', [ + 'name' => $table->getModelName(), + ]); + } + } + } + + /** + * @return string + */ + private function getModelPath(): string + { + return app_path(); + } + + private function existsModel(string $modelName): bool + { + return File::exists($this->getModelPath() . '/' . $modelName . '.php'); + } +} diff --git a/src/Migrations/DacapoGenerator.php b/src/Migrations/DacapoGenerator.php index e342c73e..f6f4dca9 100644 --- a/src/Migrations/DacapoGenerator.php +++ b/src/Migrations/DacapoGenerator.php @@ -2,8 +2,24 @@ namespace UcanLab\LaravelDacapo\Migrations; +use UcanLab\LaravelDacapo\Generator\ModelTemplateGenerator; + +/** + * Class DacapoGenerator + */ class DacapoGenerator { + private $enabledMakeModel; + + /** + * DacapoGenerator constructor. + * @param bool $enabledMakeModel + */ + public function __construct(bool $enabledMakeModel) + { + $this->enabledMakeModel = $enabledMakeModel; + } + /** * @return void */ @@ -16,5 +32,9 @@ public function run(): void (new GenerateCreateIndexMigration($table))->run(); (new GenerateConstraintForeignKeyMigration($table))->run(); } + + if ($this->enabledMakeModel) { + (new ModelTemplateGenerator($tables))->run(); + } } } diff --git a/src/Migrations/Schema/Column.php b/src/Migrations/Schema/Column.php index 08b4b1b5..ba210ca6 100644 --- a/src/Migrations/Schema/Column.php +++ b/src/Migrations/Schema/Column.php @@ -177,39 +177,66 @@ protected function getDropIndexType(): string /** * @return string + * @todo refactor later */ protected function getColumnModifier(): string { $str = ''; - if ($this->after) { - // - } elseif ($this->autoIncrement) { - // - } elseif ($this->charset) { - // - } elseif ($this->collation) { - // - } elseif ($this->comment) { +// if ($this->after) { +// $str .= "->after('$this->after')"; +// } + + if ($this->autoIncrement) { + $str .= "->autoIncrement()"; + } + + if ($this->charset) { + $str .= "->charset('$this->charset')"; + } + + if ($this->collation) { + $str .= "->collation('$this->collation')"; + } + + if ($this->comment) { $str .= "->comment('$this->comment')"; - } elseif ($this->default) { + } + + if ($this->default) { $str .= "->comment('$this->default')"; - } elseif ($this->first) { - // - } elseif ($this->nullable !== null) { + } + +// if ($this->first) { +// $str .= "->first()"; +// } + + if ($this->nullable !== null) { $str .= "->nullable($this->nullable)"; - } elseif ($this->storedAs) { - // - } elseif ($this->unsigned) { + } + + if ($this->storedAs) { + $str .= "->storedAs('$this->storedAs')"; + } + + if ($this->unsigned) { $str .= "->unsigned()"; - } elseif ($this->useCurrent) { - // - } elseif ($this->virtualAs) { - // - } elseif ($this->generatedAs) { - // - } elseif ($this->always) { - // + } + + if ($this->useCurrent) { + $str .= "->useCurrent()"; + } + + if ($this->virtualAs) { + $str .= "->virtualAs('$this->virtualAs')"; + } + + if ($this->generatedAs) { + $str .= "->generatedAs('$this->generatedAs')"; + } + + if ($this->always) { + $str .= "->always()"; } return $str; diff --git a/src/Migrations/Schema/Table.php b/src/Migrations/Schema/Table.php index 81203852..9b57e842 100644 --- a/src/Migrations/Schema/Table.php +++ b/src/Migrations/Schema/Table.php @@ -71,6 +71,14 @@ public function getTableName(): string return $this->name; } + /** + * @return string + */ + public function getModelName(): string + { + return Str::studly(Str::singular($this->name)); + } + /** * @return string */ diff --git a/src/Migrations/SchemaLoader.php b/src/Migrations/SchemaLoader.php index 6189abf6..f7047ec6 100644 --- a/src/Migrations/SchemaLoader.php +++ b/src/Migrations/SchemaLoader.php @@ -2,9 +2,10 @@ namespace UcanLab\LaravelDacapo\Migrations; +use Illuminate\Support\Facades\File; use Symfony\Component\Yaml\Yaml; -use UcanLab\LaravelDacapo\Migrations\Schema\Tables; use UcanLab\LaravelDacapo\Migrations\Schema\Table; +use UcanLab\LaravelDacapo\Migrations\Schema\Tables; class SchemaLoader { @@ -20,10 +21,9 @@ public function __construct() */ public function run(): Tables { - $path = $this->getSchemaPath(); - $yaml = $this->getYamlContents($path); + $schemas = $this->getSchemas(); - foreach ($yaml as $tableName => $tableAttributes) { + foreach ($schemas as $tableName => $tableAttributes) { $table = $this->makeTable($tableName, $tableAttributes); $this->tables->add($table); } @@ -31,12 +31,39 @@ public function run(): Tables return $this->tables; } + private function getSchemas(): array + { + $files = $this->getYamlFiles($this->getSchemaPath()); + $schemas = []; + foreach ($files as $file) { + $schemas += $this->getYamlContents($file->getRealPath()); + } + + return $schemas; + } + /** * @return string */ private function getSchemaPath(): string { - return database_path('schema.yml'); + return database_path('schemas'); + } + + /** + * @param string $path + * @return array + */ + private function getYamlFiles(string $path): array + { + $files = []; + foreach (File::files($path) as $file) { + if ($file->getExtension() === 'yml') { + $files[] = $file; + } + } + + return $files; } /** diff --git a/src/Providers/ConsoleServiceProvider.php b/src/Providers/ConsoleServiceProvider.php index 62f9a205..dad66977 100644 --- a/src/Providers/ConsoleServiceProvider.php +++ b/src/Providers/ConsoleServiceProvider.php @@ -3,9 +3,10 @@ namespace UcanLab\LaravelDacapo\Providers; use Illuminate\Support\ServiceProvider; -use UcanLab\LaravelDacapo\Console\DacapoInitCommand; -use UcanLab\LaravelDacapo\Console\DacapoGenerateCommand; use UcanLab\LaravelDacapo\Console\DacapoClearCommand; +use UcanLab\LaravelDacapo\Console\DacapoGenerateCommand; +use UcanLab\LaravelDacapo\Console\DacapoInitCommand; +use UcanLab\LaravelDacapo\Console\DacapoUninstallCommand; /** * Class ConsoleServiceProvider @@ -17,7 +18,6 @@ class ConsoleServiceProvider extends ServiceProvider public function boot() { - $this->bootPublishes(); $this->registerCommands(); } @@ -29,20 +29,6 @@ public function register() // register bindings } - /** - * Bootstrap publishes - * - * @return void - */ - protected function bootPublishes() - { - $schemaPath = __DIR__ . '/../Storage'; - - $this->publishes([ - $schemaPath . '/schema.yml' => database_path('schema.yml'), - ]); - } - /** * @return void */ @@ -60,10 +46,15 @@ protected function registerCommands() return new DacapoClearCommand(); }); + $this->app->singleton('command.ucan.dacapo.uninstall', function () { + return new DacapoUninstallCommand(); + }); + $this->commands([ 'command.ucan.dacapo.init', 'command.ucan.dacapo.generate', 'command.ucan.dacapo.clear', + 'command.ucan.dacapo.uninstall', ]); } @@ -73,8 +64,10 @@ protected function registerCommands() public function provides() { return [ + 'command.ucan.dacapo.init', 'command.ucan.dacapo.generate', 'command.ucan.dacapo.clear', + 'command.ucan.dacapo.uninstall', ]; } } diff --git a/src/Storage/schemas/schema.legacy.yml b/src/Storage/schemas/default.legacy.yml similarity index 100% rename from src/Storage/schemas/schema.legacy.yml rename to src/Storage/schemas/default.legacy.yml diff --git a/src/Storage/schemas/schema.yml b/src/Storage/schemas/default.yml similarity index 100% rename from src/Storage/schemas/schema.yml rename to src/Storage/schemas/default.yml