Skip to content

Commit

Permalink
Merge pull request #24 from ucan-lab/develop
Browse files Browse the repository at this point in the history
ver2.0.0 Release👏👏👏
  • Loading branch information
ucan-lab authored Aug 22, 2019
2 parents 6468160 + 2a6ceb5 commit 4aa91c6
Show file tree
Hide file tree
Showing 13 changed files with 289 additions and 55 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

```
Expand Down
4 changes: 2 additions & 2 deletions src/Console/DacapoClearCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function __construct()
/**
* @return void
*/
public function handle()
public function handle(): void
{
if (! $this->confirmToProceed()) {
return;
Expand All @@ -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'));
Expand Down
5 changes: 3 additions & 2 deletions src/Console/DacapoGenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}
';

/**
Expand All @@ -35,15 +36,15 @@ class DacapoGenerateCommand extends Command
/**
* @return void
*/
public function handle()
public function handle(): void
{
if (! $this->confirmToProceed()) {
return;
}

$this->call('dacapo:clear', ['--force' => true]);

(new DacapoGenerator())->run();
(new DacapoGenerator($this->option('model')))->run();
$this->info('Generated migration files.');

if ($this->option('seed')) {
Expand Down
58 changes: 55 additions & 3 deletions src/Console/DacapoInitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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';
}
}
63 changes: 63 additions & 0 deletions src/Console/DacapoUninstallCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace UcanLab\LaravelDacapo\Console;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;

/**
* Class DacapoUninstallCommand
*/
class DacapoUninstallCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'dacapo:uninstall';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Uninstall dacapo.';

/**
* @return void
*/
public function handle()
{
$this->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');
}
}
41 changes: 41 additions & 0 deletions src/Generator/ModelTemplateGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace UcanLab\LaravelDacapo\Generator;

use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\File;
use UcanLab\LaravelDacapo\Migrations\Schema\Tables;

class ModelTemplateGenerator
{
private $tables;

public function __construct(Tables $tables)
{
$this->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');
}
}
20 changes: 20 additions & 0 deletions src/Migrations/DacapoGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -16,5 +32,9 @@ public function run(): void
(new GenerateCreateIndexMigration($table))->run();
(new GenerateConstraintForeignKeyMigration($table))->run();
}

if ($this->enabledMakeModel) {
(new ModelTemplateGenerator($tables))->run();
}
}
}
75 changes: 51 additions & 24 deletions src/Migrations/Schema/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions src/Migrations/Schema/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Loading

0 comments on commit 4aa91c6

Please sign in to comment.