Skip to content

Commit 7831480

Browse files
Add erase command (#43)
Co-authored-by: Krishan König
1 parent ad4fffe commit 7831480

File tree

8 files changed

+134
-23
lines changed

8 files changed

+134
-23
lines changed

src/Blueprint.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22

33
namespace Blueprint;
44

5-
6-
use Blueprint\Contracts\Generator;
75
use Blueprint\Contracts\Lexer;
86
use Symfony\Component\Yaml\Yaml;
7+
use Blueprint\Contracts\Generator;
98

109
class Blueprint
1110
{
@@ -25,7 +24,7 @@ public function analyze(array $tokens)
2524
{
2625
$registry = [
2726
'models' => [],
28-
'controllers' => []
27+
'controllers' => [],
2928
];
3029

3130
foreach ($this->lexers as $lexer) {
@@ -46,6 +45,11 @@ public function generate(array $tree): array
4645
return $components;
4746
}
4847

48+
public function dump(array $generated)
49+
{
50+
return Yaml::dump($generated);
51+
}
52+
4953
public function registerLexer(Lexer $lexer)
5054
{
5155
$this->lexers[] = $lexer;
@@ -55,4 +59,4 @@ public function registerGenerator(Generator $generator)
5559
{
5660
$this->generators[] = $generator;
5761
}
58-
}
62+
}

src/BlueprintCommand.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace Blueprint;
44

5+
use Illuminate\Support\Str;
56
use Illuminate\Console\Command;
67
use Illuminate\Filesystem\Filesystem;
7-
use Illuminate\Support\Str;
88
use Symfony\Component\Console\Input\InputArgument;
99

1010
class BlueprintCommand extends Command
@@ -80,8 +80,12 @@ public function handle()
8080

8181
$this->line('');
8282
});
83-
}
8483

84+
$this->files->put(
85+
'.blueprint',
86+
$blueprint->dump($generated)
87+
);
88+
}
8589

8690
/**
8791
* Get the console command arguments.

src/BlueprintServiceProvider.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ class BlueprintServiceProvider extends ServiceProvider implements DeferrableProv
1414
*/
1515
public function boot()
1616
{
17-
// ...
1817
if (!defined('STUBS_PATH')) {
1918
define('STUBS_PATH', dirname(__DIR__) . '/stubs');
2019
}
@@ -32,8 +31,16 @@ function ($app) {
3231
return new BlueprintCommand($app['files']);
3332
}
3433
);
34+
$this->app->bind('command.blueprint.erase',
35+
function ($app) {
36+
return new EraseCommand($app['files']);
37+
}
38+
);
3539

36-
$this->commands('command.blueprint.build');
40+
$this->commands([
41+
'command.blueprint.build',
42+
'command.blueprint.erase',
43+
]);
3744
}
3845

3946
/**
@@ -43,7 +50,9 @@ function ($app) {
4350
*/
4451
public function provides()
4552
{
46-
return ['command.blueprint.build'];
53+
return [
54+
'command.blueprint.build',
55+
'command.blueprint.erase',
56+
];
4757
}
48-
49-
}
58+
}

src/EraseCommand.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
namespace Blueprint;
4+
5+
use Illuminate\Support\Str;
6+
use Illuminate\Console\Command;
7+
use Illuminate\Filesystem\Filesystem;
8+
use Symfony\Component\Console\Input\InputArgument;
9+
10+
class EraseCommand extends Command
11+
{
12+
/**
13+
* The name and signature of the console command.
14+
*
15+
* @var string
16+
*/
17+
protected $signature = 'blueprint:erase';
18+
19+
/**
20+
* The console command description.
21+
*
22+
* @var string
23+
*/
24+
protected $description = 'Erase components created from last Blueprint build';
25+
26+
/** @var Filesystem $files */
27+
protected $files;
28+
29+
/**
30+
* @param Filesystem $files
31+
* @param \Illuminate\Contracts\View\Factory $view
32+
*/
33+
public function __construct(Filesystem $files)
34+
{
35+
parent::__construct();
36+
37+
$this->files = $files;
38+
}
39+
40+
/**
41+
* Execute the console command.
42+
*
43+
* @return mixed
44+
*/
45+
public function handle()
46+
{
47+
$contents = $this->files->get('.blueprint');
48+
49+
$blueprint = new Blueprint();
50+
$generated = $blueprint->parse($contents);
51+
52+
collect($generated)->each(function ($files, $action) {
53+
if ($action === 'created') {
54+
$this->line('Deleted:', $this->outputStyle($action));
55+
$this->files->delete($files);
56+
} elseif ($action === 'updated') {
57+
$this->comment('The updates to the following files can not be erased automatically.');
58+
}
59+
60+
collect($files)->each(function ($file) {
61+
$this->line('- ' . $file);
62+
});
63+
64+
$this->line('');
65+
});
66+
}
67+
68+
/**
69+
* Get the console command arguments.
70+
*
71+
* @return array
72+
*/
73+
protected function getArguments()
74+
{
75+
return [
76+
['draft', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'Which models to include', []],
77+
];
78+
}
79+
80+
/**
81+
* Get the console command options.
82+
*
83+
* @return array
84+
*/
85+
protected function getOptions()
86+
{
87+
return [];
88+
}
89+
90+
private function outputStyle($action)
91+
{
92+
if ($action === 'created') {
93+
return 'error';
94+
}
95+
96+
return 'comment';
97+
}
98+
}

tests/Feature/Generator/ControllerGeneratorTest.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
namespace Tests\Feature\Generators;
44

5+
use Tests\TestCase;
56
use Blueprint\Blueprint;
6-
use Blueprint\Generators\ControllerGenerator;
77
use Blueprint\Lexers\StatementLexer;
8-
use Tests\TestCase;
8+
use Blueprint\Generators\ControllerGenerator;
99

1010
/**
1111
* @see ControllerGenerator
@@ -70,10 +70,9 @@ public function output_writes_migration_for_controller_tree($definition, $path,
7070
$tree = $this->blueprint->analyze($tokens);
7171

7272
$this->assertEquals(['created' => [$path]], $this->subject->output($tree));
73-
++$iteration;
73+
$iteration++;
7474
}
7575

76-
7776
public function controllerTreeDataProvider()
7877
{
7978
return [

tests/Feature/Generator/FactoryGeneratorTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,11 @@ public function output_writes_migration_for_model_tree($definition, $path, $migr
6060
$this->assertEquals(['created' => [$path]], $this->subject->output($tree));
6161
}
6262

63-
6463
public function modelTreeDataProvider()
6564
{
6665
return [
6766
['definitions/post.bp', 'database/factories/PostFactory.php', 'factories/post.php'],
6867
['definitions/team.bp', 'database/factories/TeamFactory.php', 'factories/team.php']
6968
];
7069
}
71-
}
70+
}

tests/Feature/Generator/ModelGeneratorTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace Tests\Feature\Generators;
44

5+
use Tests\TestCase;
56
use Blueprint\Blueprint;
67
use Blueprint\Generators\ModelGenerator;
7-
use Tests\TestCase;
88

99
class ModelGeneratorTest extends TestCase
1010
{
@@ -81,10 +81,9 @@ public function output_writes_migration_for_model_tree($definition, $path, $mode
8181
$tree = $this->blueprint->analyze($tokens);
8282

8383
$this->assertEquals(['created' => [$path]], $this->subject->output($tree));
84-
++$iteration;
84+
$iteration++;
8585
}
8686

87-
8887
public function modelTreeDataProvider()
8988
{
9089
return [

tests/Feature/Generator/RouteGeneratorTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
namespace Tests\Feature\Generators;
44

5+
use Tests\TestCase;
56
use Blueprint\Blueprint;
6-
use Blueprint\Generators\RouteGenerator;
77
use Blueprint\Lexers\StatementLexer;
8-
use Tests\TestCase;
8+
use Blueprint\Generators\RouteGenerator;
99

1010
/**
1111
* @see RouteGenerator
@@ -57,7 +57,6 @@ public function output_writes_migration_for_route_tree($definition, $routes)
5757
$this->assertEquals(['updated' => [$path]], $this->subject->output($tree));
5858
}
5959

60-
6160
public function controllerTreeDataProvider()
6261
{
6362
return [

0 commit comments

Comments
 (0)