From 7768b843f6858a2b43da82113de86ecc403d413c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=86=E3=81=86=E3=81=8D=E3=82=83=E3=82=93?= Date: Fri, 16 Aug 2019 14:15:57 +0900 Subject: [PATCH 01/11] feat dacapo:init create schemas directory --- src/Console/DacapoInitCommand.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Console/DacapoInitCommand.php b/src/Console/DacapoInitCommand.php index f706e0b8..7b27de24 100644 --- a/src/Console/DacapoInitCommand.php +++ b/src/Console/DacapoInitCommand.php @@ -41,10 +41,12 @@ public function handle() */ private function initSchema() { + File::makeDirectory(database_path('schemas')); + if ($this->option('legacy')) { - File::copy(__DIR__ . '/../Storage/schemas/schema.legacy.yml', database_path('schema.yml')); + File::copy(__DIR__ . '/../Storage/schemas/schema.legacy.yml', database_path('schemas/schema.yml')); } else { - File::copy(__DIR__ . '/../Storage/schemas/schema.yml', database_path('schema.yml')); + File::copy(__DIR__ . '/../Storage/schemas/schema.yml', database_path('schemas/schema.yml')); } $this->info('Init dacapo default schema yaml.'); From 83d17c63107cd716365d2ad9e394346737d0c199 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=86=E3=81=86=E3=81=8D=E3=82=83=E3=82=93?= Date: Fri, 16 Aug 2019 14:16:17 +0900 Subject: [PATCH 02/11] feat dacapo:generate create schemas directory --- src/Migrations/SchemaLoader.php | 37 ++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) 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; } /** From 32f0997f79c4f2ba008d4d814a92917d2d70705b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=86=E3=81=86=E3=81=8D=E3=82=83=E3=82=93?= Date: Fri, 16 Aug 2019 14:32:21 +0900 Subject: [PATCH 03/11] feat dacapo:init rename default.yml --- src/Console/DacapoInitCommand.php | 4 ++-- src/Storage/schemas/{schema.legacy.yml => default.legacy.yml} | 0 src/Storage/schemas/{schema.yml => default.yml} | 0 3 files changed, 2 insertions(+), 2 deletions(-) rename src/Storage/schemas/{schema.legacy.yml => default.legacy.yml} (100%) rename src/Storage/schemas/{schema.yml => default.yml} (100%) diff --git a/src/Console/DacapoInitCommand.php b/src/Console/DacapoInitCommand.php index 7b27de24..810d7377 100644 --- a/src/Console/DacapoInitCommand.php +++ b/src/Console/DacapoInitCommand.php @@ -44,9 +44,9 @@ private function initSchema() File::makeDirectory(database_path('schemas')); if ($this->option('legacy')) { - File::copy(__DIR__ . '/../Storage/schemas/schema.legacy.yml', database_path('schemas/schema.yml')); + File::copy(__DIR__ . '/../Storage/schemas/default.legacy.yml', database_path('schemas/default.yml')); } else { - File::copy(__DIR__ . '/../Storage/schemas/schema.yml', database_path('schemas/schema.yml')); + File::copy(__DIR__ . '/../Storage/schemas/default.yml', database_path('schemas/default.yml')); } $this->info('Init dacapo default schema yaml.'); 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 From 3582eb1b3e86ddd922231456941c64de3560bbf9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=86=E3=81=86=E3=81=8D=E3=82=83=E3=82=93?= Date: Fri, 16 Aug 2019 14:36:34 +0900 Subject: [PATCH 04/11] refactor maintenance --- src/Console/DacapoInitCommand.php | 58 ++++++++++++++++++++++++++++--- 1 file changed, 54 insertions(+), 4 deletions(-) diff --git a/src/Console/DacapoInitCommand.php b/src/Console/DacapoInitCommand.php index 810d7377..eed90f07 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(); } @@ -41,14 +53,52 @@ public function handle() */ private function initSchema() { - File::makeDirectory(database_path('schemas')); - if ($this->option('legacy')) { - File::copy(__DIR__ . '/../Storage/schemas/default.legacy.yml', database_path('schemas/default.yml')); + File::copy($this->getStoragePath() . '/default.legacy.yml', database_path('schemas/default.yml')); } else { - File::copy(__DIR__ . '/../Storage/schemas/default.yml', database_path('schemas/default.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'; + } } From b605d96251e821d630065920b173fa1ede64448a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=86=E3=81=86=E3=81=8D=E3=82=83=E3=82=93?= Date: Fri, 16 Aug 2019 17:50:41 +0900 Subject: [PATCH 05/11] style maintenance --- src/Console/DacapoClearCommand.php | 4 ++-- src/Console/DacapoGenerateCommand.php | 2 +- src/Console/DacapoInitCommand.php | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) 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..335039a2 100644 --- a/src/Console/DacapoGenerateCommand.php +++ b/src/Console/DacapoGenerateCommand.php @@ -35,7 +35,7 @@ class DacapoGenerateCommand extends Command /** * @return void */ - public function handle() + public function handle(): void { if (! $this->confirmToProceed()) { return; diff --git a/src/Console/DacapoInitCommand.php b/src/Console/DacapoInitCommand.php index eed90f07..645a870a 100644 --- a/src/Console/DacapoInitCommand.php +++ b/src/Console/DacapoInitCommand.php @@ -51,7 +51,7 @@ public function handle() * * @return void */ - private function initSchema() + private function initSchema(): void { if ($this->option('legacy')) { File::copy($this->getStoragePath() . '/default.legacy.yml', database_path('schemas/default.yml')); From 88fad335bd7435120010994255211215531c086d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=86=E3=81=86=E3=81=8D=E3=82=83=E3=82=93?= Date: Fri, 16 Aug 2019 18:48:27 +0900 Subject: [PATCH 06/11] feat dacapo:uninstall --- src/Console/DacapoUninstallCommand.php | 63 ++++++++++++++++++++++++ src/Providers/ConsoleServiceProvider.php | 8 +++ 2 files changed, 71 insertions(+) create mode 100644 src/Console/DacapoUninstallCommand.php 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/Providers/ConsoleServiceProvider.php b/src/Providers/ConsoleServiceProvider.php index 62f9a205..c051de6c 100644 --- a/src/Providers/ConsoleServiceProvider.php +++ b/src/Providers/ConsoleServiceProvider.php @@ -6,6 +6,7 @@ use UcanLab\LaravelDacapo\Console\DacapoInitCommand; use UcanLab\LaravelDacapo\Console\DacapoGenerateCommand; use UcanLab\LaravelDacapo\Console\DacapoClearCommand; +use UcanLab\LaravelDacapo\Console\DacapoUninstallCommand; /** * Class ConsoleServiceProvider @@ -60,10 +61,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 +79,10 @@ protected function registerCommands() public function provides() { return [ + 'command.ucan.dacapo.init', 'command.ucan.dacapo.generate', 'command.ucan.dacapo.clear', + 'command.ucan.dacapo.uninstall', ]; } } From 84352a07d534035d8fed355b9f7d7db9b4366087 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=86=E3=81=86=E3=81=8D=E3=82=83=E3=82=93?= Date: Sat, 17 Aug 2019 02:11:52 +0900 Subject: [PATCH 07/11] feat Schema/Table get model name --- src/Migrations/Schema/Table.php | 8 ++++++++ 1 file changed, 8 insertions(+) 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 */ From c7ed596151af25d1e3e093a6da2d4af270224415 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=86=E3=81=86=E3=81=8D=E3=82=83=E3=82=93?= Date: Sat, 17 Aug 2019 02:12:59 +0900 Subject: [PATCH 08/11] feat dacapo:generate add --model option --- src/Console/DacapoGenerateCommand.php | 3 +- src/Generator/ModelTemplateGenerator.php | 41 ++++++++++++++++++++++++ src/Migrations/DacapoGenerator.php | 20 ++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 src/Generator/ModelTemplateGenerator.php diff --git a/src/Console/DacapoGenerateCommand.php b/src/Console/DacapoGenerateCommand.php index 335039a2..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} '; /** @@ -43,7 +44,7 @@ public function handle(): void $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/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(); + } } } From 938e25711eaca1b5779401172e0810bd6f4f7764 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=86=E3=81=86=E3=81=8D=E3=82=83=E3=82=93?= Date: Sat, 17 Aug 2019 02:15:05 +0900 Subject: [PATCH 09/11] style code maintenance --- src/Providers/ConsoleServiceProvider.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Providers/ConsoleServiceProvider.php b/src/Providers/ConsoleServiceProvider.php index c051de6c..337e8244 100644 --- a/src/Providers/ConsoleServiceProvider.php +++ b/src/Providers/ConsoleServiceProvider.php @@ -3,9 +3,9 @@ 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; /** From 41104dc18a8f48d67271796b93fa9e39af8c56d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=86=E3=81=86=E3=81=8D=E3=82=83=E3=82=93?= Date: Mon, 19 Aug 2019 15:59:25 +0900 Subject: [PATCH 10/11] fix abolished vender:published --- README.md | 6 ++++-- src/Providers/ConsoleServiceProvider.php | 15 --------------- 2 files changed, 4 insertions(+), 17 deletions(-) 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/Providers/ConsoleServiceProvider.php b/src/Providers/ConsoleServiceProvider.php index 337e8244..dad66977 100644 --- a/src/Providers/ConsoleServiceProvider.php +++ b/src/Providers/ConsoleServiceProvider.php @@ -18,7 +18,6 @@ class ConsoleServiceProvider extends ServiceProvider public function boot() { - $this->bootPublishes(); $this->registerCommands(); } @@ -30,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 */ From fe0dff28e8f026d70f5aa467dccffec7f8425cda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=86=E3=81=86=E3=81=8D=E3=82=83=E3=82=93?= Date: Wed, 21 Aug 2019 17:41:20 +0900 Subject: [PATCH 11/11] feat column modifiers --- src/Migrations/Schema/Column.php | 75 ++++++++++++++++++++++---------- 1 file changed, 51 insertions(+), 24 deletions(-) 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;