Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ public function analyze(array $tokens)
return new Tree($registry);
}

public function generate(Tree $tree, array $only = [], array $skip = []): array
public function generate(Tree $tree, array $only = [], array $skip = [], $overwriteMigrations = false): array
{
$components = [];

foreach ($this->generators as $generator) {
if ($this->shouldGenerate($generator->types(), $only, $skip)) {
$components = array_merge_recursive($components, $generator->output($tree));
$components = array_merge_recursive($components, $generator->output($tree, $overwriteMigrations));
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class Builder
{
public function execute(Blueprint $blueprint, Filesystem $files, string $draft, string $only = '', string $skip = '')
public function execute(Blueprint $blueprint, Filesystem $files, string $draft, string $only = '', string $skip = '', $overwriteMigrations = false)
{
$cache = [];
if ($files->exists('.blueprint')) {
Expand All @@ -20,7 +20,7 @@ public function execute(Blueprint $blueprint, Filesystem $files, string $draft,
$only = array_filter(explode(',', $only));
$skip = array_filter(explode(',', $skip));

$generated = $blueprint->generate($registry, $only, $skip);
$generated = $blueprint->generate($registry, $only, $skip, $overwriteMigrations);

$models = array_merge($tokens['cache'], $tokens['models'] ?? []);

Expand Down
4 changes: 3 additions & 1 deletion src/Commands/BuildCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class BuildCommand extends Command
{draft? : The path to the draft file, default: draft.yaml or draft.yml }
{--only= : Comma separated list of file classes to generate, skipping the rest }
{--skip= : Comma separated list of file classes to skip, generating the rest }
{--overwrite-migrations : Update existing migration files, if found }
';

/**
Expand Down Expand Up @@ -59,9 +60,10 @@ public function handle()

$only = $this->option('only') ?: '';
$skip = $this->option('skip') ?: '';
$overwriteMigrations = $this->option('overwrite-migrations') ?: false;

$blueprint = resolve(Blueprint::class);
$generated = $this->builder->execute($blueprint, $this->files, $file, $only, $skip);
$generated = $this->builder->execute($blueprint, $this->files, $file, $only, $skip, $overwriteMigrations);

collect($generated)->each(function ($files, $action) {
$this->line(Str::studly($action).':', $this->outputStyle($action));
Expand Down
32 changes: 23 additions & 9 deletions src/Generators/MigrationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function __construct($files)
$this->files = $files;
}

public function output(Tree $tree): array
public function output(Tree $tree, $overwrite = false): array
{
$output = [];

Expand All @@ -54,10 +54,11 @@ public function output(Tree $tree): array

/** @var \Blueprint\Models\Model $model */
foreach ($tree->models() as $model) {
$path = $this->getPath($model, $sequential_timestamp->addSecond());
$path = $this->getPath($model, $sequential_timestamp->addSecond(), $overwrite);
$action = $this->files->exists($path) ? 'updated' : 'created';
$this->files->put($path, $this->populateStub($stub, $model));

$output['created'][] = $path;
$output[$action][] = $path;

if (! empty($model->pivotTables())) {
foreach ($model->pivotTables() as $pivotSegments) {
Expand All @@ -68,10 +69,11 @@ public function output(Tree $tree): array
}

foreach ($created_pivot_tables as $pivotTable => $pivotSegments) {
$path = $this->getPivotTablePath($pivotTable, $sequential_timestamp);
$path = $this->getPivotTablePath($pivotTable, $sequential_timestamp, $overwrite);
$action = $this->files->exists($path) ? 'updated' : 'created';
$this->files->put($path, $this->populatePivotStub($stub, $pivotSegments));
$created_pivot_tables[] = $pivotTable;
$output['created'][] = $path;
$output[$action][] = $path;
}

return $output;
Expand Down Expand Up @@ -276,14 +278,26 @@ protected function getClassName(Model $model)
return 'Create'.Str::studly($model->tableName()).'Table';
}

protected function getPath(Model $model, Carbon $timestamp)
protected function getPath(Model $model, Carbon $timestamp, $overwrite = false)
{
return 'database/migrations/'.$timestamp->format('Y_m_d_His').'_create_'.$model->tableName().'_table.php';
return $this->getTablePath($model->tableName(), $timestamp, $overwrite);
}

protected function getPivotTablePath($tableName, Carbon $timestamp)
protected function getPivotTablePath($tableName, Carbon $timestamp, $overwrite = false)
{
return 'database/migrations/'.$timestamp->format('Y_m_d_His').'_create_'.$tableName.'_table.php';
return $this->getTablePath($tableName, $timestamp, $overwrite);
}

protected function getTablePath($tableName, Carbon $timestamp, $overwrite = false)
{
$dir = 'database/migrations/';
$name = '_create_'.$tableName.'_table.php';

$file = $overwrite ? collect($this->files->files($dir))->first(function ($file) use ($tableName) {
return str_contains($file, $tableName);
}) : false;

return $file ? (string) $file : $dir.$timestamp->format('Y_m_d_His').$name;
}

protected function isLaravel7orNewer()
Expand Down
28 changes: 14 additions & 14 deletions tests/Feature/BlueprintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ public function generate_uses_registered_generators_and_returns_generated_files(
$tree = new Tree(['branch' => ['code', 'attributes']]);

$generatorOne->expects('output')
->with($tree)
->with($tree, false)
->andReturn([
'created' => ['one/new.php'],
'updated' => ['one/existing.php'],
Expand All @@ -394,7 +394,7 @@ public function generate_uses_registered_generators_and_returns_generated_files(

$generatorTwo = \Mockery::mock(Generator::class);
$generatorTwo->expects('output')
->with($tree)
->with($tree, false)
->andReturn([
'created' => ['two/new.php'],
'updated' => ['two/existing.php'],
Expand Down Expand Up @@ -429,7 +429,7 @@ public function generate_uses_swapped_generator_and_returns_generated_files()

$generatorSwap = \Mockery::mock(Generator::class);
$generatorSwap->expects('output')
->with($tree)
->with($tree, false)
->andReturn([
'created' => ['swapped/new.php'],
'updated' => ['swapped/existing.php'],
Expand Down Expand Up @@ -464,7 +464,7 @@ public function generate_only_one_specific_type()
$skip = [];

$generatorFoo->shouldReceive('output')
->with($tree)
->with($tree, false)
->andReturn([
'created' => ['foo.php'],
]);
Expand All @@ -474,7 +474,7 @@ public function generate_only_one_specific_type()

$generatorBar = \Mockery::mock(Generator::class);
$generatorBar->shouldReceive('output')
->with($tree)
->with($tree, false)
->andReturn([
'created' => ['bar.php'],
]);
Expand All @@ -484,7 +484,7 @@ public function generate_only_one_specific_type()

$generatorBaz = \Mockery::mock(Generator::class);
$generatorBaz->shouldReceive('output')
->with($tree)
->with($tree, false)
->andReturn([
'created' => ['baz.php'],
]);
Expand Down Expand Up @@ -515,7 +515,7 @@ public function generate_only_specific_types()
$skip = [];

$generatorFoo->shouldReceive('output')
->with($tree)
->with($tree, false)
->andReturn([
'created' => ['foo.php'],
]);
Expand All @@ -525,7 +525,7 @@ public function generate_only_specific_types()

$generatorBar = \Mockery::mock(Generator::class);
$generatorBar->shouldReceive('output')
->with($tree)
->with($tree, false)
->andReturn([
'created' => ['bar.php'],
]);
Expand All @@ -535,7 +535,7 @@ public function generate_only_specific_types()

$generatorBaz = \Mockery::mock(Generator::class);
$generatorBaz->shouldReceive('output')
->with($tree)
->with($tree, false)
->andReturn([
'created' => ['baz.php'],
]);
Expand Down Expand Up @@ -566,7 +566,7 @@ public function generate_should_skip_one_specific_type()
$skip = ['bar'];

$generatorFoo->shouldReceive('output')
->with($tree)
->with($tree, false)
->andReturn([
'created' => ['foo.php'],
]);
Expand All @@ -576,7 +576,7 @@ public function generate_should_skip_one_specific_type()

$generatorBar = \Mockery::mock(Generator::class);
$generatorBar->shouldReceive('output')
->with($tree)
->with($tree, false)
->andReturn([
'created' => ['bar.php'],
]);
Expand All @@ -586,7 +586,7 @@ public function generate_should_skip_one_specific_type()

$generatorBaz = \Mockery::mock(Generator::class);
$generatorBaz->shouldReceive('output')
->with($tree)
->with($tree, false)
->andReturn([
'created' => ['baz.php'],
]);
Expand Down Expand Up @@ -617,7 +617,7 @@ public function generate_should_skip_specific_types()
$skip = ['bar', 'baz'];

$generatorFoo->shouldReceive('output')
->with($tree)
->with($tree, false)
->andReturn([
'created' => ['foo.php'],
]);
Expand All @@ -637,7 +637,7 @@ public function generate_should_skip_specific_types()

$generatorBaz = \Mockery::mock(Generator::class);
$generatorBaz->shouldReceive('output')
->with($tree)
->with($tree, false)
->andReturn([
'created' => ['baz.php'],
]);
Expand Down
6 changes: 3 additions & 3 deletions tests/Feature/Commands/BuildCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function it_uses_the_default_draft_file()
$builder = $this->mock(Builder::class);

$builder->shouldReceive('execute')
->with(resolve(Blueprint::class), $filesystem, 'draft.yaml', '', '')
->with(resolve(Blueprint::class), $filesystem, 'draft.yaml', '', '', false)
->andReturn(collect([]));

$this->artisan('blueprint:build')
Expand All @@ -44,7 +44,7 @@ public function it_passes_the_command_args_to_the_builder_in_right_order()
$builder = $this->mock(Builder::class);

$builder->shouldReceive('execute')
->with(resolve(Blueprint::class), $filesystem, 'test.yml', 'a,b,c', 'x,y,z')
->with(resolve(Blueprint::class), $filesystem, 'test.yml', 'a,b,c', 'x,y,z', false)
->andReturn(collect([]));

$this->artisan('blueprint:build test.yml --only=a,b,c --skip=x,y,z')
Expand Down Expand Up @@ -82,7 +82,7 @@ public function it_shows_the_generated_files_groupbed_by_actions()
$builder = $this->mock(Builder::class);

$builder->shouldReceive('execute')
->with(resolve(Blueprint::class), $filesystem, 'draft.yaml', '', '')
->with(resolve(Blueprint::class), $filesystem, 'draft.yaml', '', '', false)
->andReturn(collect([
"created" => [
"file1",
Expand Down
Loading