diff --git a/formats/schemas.json b/formats/schema.json similarity index 100% rename from formats/schemas.json rename to formats/schema.json diff --git a/lumen-test/tests/acceptance/MigrationCommandCept.php b/lumen-test/tests/acceptance/MigrationCommandCept.php new file mode 100644 index 0000000..f8deae9 --- /dev/null +++ b/lumen-test/tests/acceptance/MigrationCommandCept.php @@ -0,0 +1,63 @@ +wantTo('generate a migration without schema'); +$I->runShellCommand('php artisan wn:migration tasks'); +$I->seeInShellOutput('tasks migration generated'); +$I->seeFileFound('./database/migrations/create_tasks.php'); +$I->openFile('./database/migrations/create_tasks.php'); +$I->seeFileContentsEqual('increments(\'id\'); + // Schema declaration + $table->timestamps(); + }); + } + + public function down() + { + Schema::drop(\'tasks\'); + } +} +'); +$I->deleteFile('./database/migrations/create_tasks.php'); + +$I->wantTo('generate a migration without schema'); +$I->runShellCommand('php artisan wn:migration tasks --schema="amount:decimal.5,2:after.\'size\':default.8 title:string:nullable"'); +$I->seeInShellOutput('tasks migration generated'); +$I->seeFileFound('./database/migrations/create_tasks.php'); +$I->openFile('./database/migrations/create_tasks.php'); +$I->seeFileContentsEqual('increments(\'id\'); + $table->decimal(\'amount\', 5, 2)->after(\'size\')->default(8); + $table->string(\'title\')->nullable(); + $table->timestamps(); + }); + } + + public function down() + { + Schema::drop(\'tasks\'); + } +} +'); +$I->deleteFile('./database/migrations/create_tasks.php'); \ No newline at end of file diff --git a/src/Commands/MigrationCommand.php b/src/Commands/MigrationCommand.php index e69de29..927c377 100644 --- a/src/Commands/MigrationCommand.php +++ b/src/Commands/MigrationCommand.php @@ -0,0 +1,61 @@ +argument('table'); + $name = 'Create' . ucwords(camel_case($table)); + + $content = $this->getTemplate('migration') + ->with([ + 'table' => $table, + 'name' => $name, + 'schema' => $this->getSchema() + ]) + ->get(); + + $name = snake_case($name); + $this->save($content, "./database/migrations/{$name}.php"); + + $this->info("{$table} migration generated !"); + } + + protected function getSchema() + { + $schema = $this->option('schema'); + if(! $schema){ + return "\t\t\t// Schema declaration"; + } + + $items = $this->getArgumentParser('schema')->parse($schema); + + $fields = []; + foreach ($items as $item) { + $fields[] = $this->getFieldDeclaration($item); + } + + return implode(PHP_EOL, $fields); + } + + protected function getFieldDeclaration($parts) + { + $name = $parts[0]['name']; + $parts[1]['args'] = array_merge(["'{$name}'"], $parts[1]['args']); + unset($parts[0]); + $parts = array_map(function($part){ + return '->' . $part['name'] . '(' . implode(', ', $part['args']) . ')'; + }, $parts); + return "\t\t\t\$table" . implode('', $parts) . ';'; + } + +} \ No newline at end of file diff --git a/src/Commands/ModelCommand.php b/src/Commands/ModelCommand.php index 2b113f7..292329b 100644 --- a/src/Commands/ModelCommand.php +++ b/src/Commands/ModelCommand.php @@ -15,8 +15,6 @@ class ModelCommand extends BaseCommand { protected $description = 'Generates a model class for a RESTfull resource'; - protected $fields = []; - public function handle() { $name = $this->argument('name'); diff --git a/src/CommandsServiceProvider.php b/src/CommandsServiceProvider.php index 75f62e2..4de9d9f 100644 --- a/src/CommandsServiceProvider.php +++ b/src/CommandsServiceProvider.php @@ -11,7 +11,7 @@ class CommandsServiceProvider extends ServiceProvider $this->registerControllerRestActionsCommand(); $this->registerControllerCommand(); $this->registerRouteCommand(); - // $this->registerMigrationCommand(); + $this->registerMigrationCommand(); // $this->registerSeedCommand(); // $this->registerTestCommand(); // $this->registerResourceCommand(); diff --git a/templates/migration.wnt b/templates/migration.wnt index 5d32e09..6ba5067 100644 --- a/templates/migration.wnt +++ b/templates/migration.wnt @@ -10,7 +10,7 @@ class {{name}}Migration extends Migration { Schema::create('{{table}}', function(Blueprint $table) { $table->increments('id'); -{{fields}} +{{schema}} $table->timestamps(); }); }