diff --git a/src/Blueprint.php b/src/Blueprint.php index ad3c50df..3713e623 100644 --- a/src/Blueprint.php +++ b/src/Blueprint.php @@ -2,10 +2,9 @@ namespace Blueprint; - -use Blueprint\Contracts\Generator; use Blueprint\Contracts\Lexer; use Symfony\Component\Yaml\Yaml; +use Blueprint\Contracts\Generator; class Blueprint { @@ -25,7 +24,7 @@ public function analyze(array $tokens) { $registry = [ 'models' => [], - 'controllers' => [] + 'controllers' => [], ]; foreach ($this->lexers as $lexer) { @@ -46,6 +45,11 @@ public function generate(array $tree): array return $components; } + public function dump(array $generated) + { + return Yaml::dump($generated); + } + public function registerLexer(Lexer $lexer) { $this->lexers[] = $lexer; @@ -55,4 +59,4 @@ public function registerGenerator(Generator $generator) { $this->generators[] = $generator; } -} \ No newline at end of file +} diff --git a/src/BlueprintCommand.php b/src/BlueprintCommand.php index d5dc7f8a..f36e01d3 100644 --- a/src/BlueprintCommand.php +++ b/src/BlueprintCommand.php @@ -2,9 +2,9 @@ namespace Blueprint; +use Illuminate\Support\Str; use Illuminate\Console\Command; use Illuminate\Filesystem\Filesystem; -use Illuminate\Support\Str; use Symfony\Component\Console\Input\InputArgument; class BlueprintCommand extends Command @@ -80,8 +80,12 @@ public function handle() $this->line(''); }); - } + $this->files->put( + '.blueprint', + $blueprint->dump($generated) + ); + } /** * Get the console command arguments. diff --git a/src/BlueprintServiceProvider.php b/src/BlueprintServiceProvider.php index 772f18a1..35306d92 100644 --- a/src/BlueprintServiceProvider.php +++ b/src/BlueprintServiceProvider.php @@ -14,7 +14,6 @@ class BlueprintServiceProvider extends ServiceProvider implements DeferrableProv */ public function boot() { - // ... if (!defined('STUBS_PATH')) { define('STUBS_PATH', dirname(__DIR__) . '/stubs'); } @@ -32,8 +31,16 @@ function ($app) { return new BlueprintCommand($app['files']); } ); + $this->app->bind('command.blueprint.erase', + function ($app) { + return new EraseCommand($app['files']); + } + ); - $this->commands('command.blueprint.build'); + $this->commands([ + 'command.blueprint.build', + 'command.blueprint.erase', + ]); } /** @@ -43,7 +50,9 @@ function ($app) { */ public function provides() { - return ['command.blueprint.build']; + return [ + 'command.blueprint.build', + 'command.blueprint.erase', + ]; } - -} \ No newline at end of file +} diff --git a/src/EraseCommand.php b/src/EraseCommand.php new file mode 100644 index 00000000..fbf1d55d --- /dev/null +++ b/src/EraseCommand.php @@ -0,0 +1,98 @@ +files = $files; + } + + /** + * Execute the console command. + * + * @return mixed + */ + public function handle() + { + $contents = $this->files->get('.blueprint'); + + $blueprint = new Blueprint(); + $generated = $blueprint->parse($contents); + + collect($generated)->each(function ($files, $action) { + if ($action === 'created') { + $this->line('Deleted:', $this->outputStyle($action)); + $this->files->delete($files); + } elseif ($action === 'updated') { + $this->comment('The updates to the following files can not be erased automatically.'); + } + + collect($files)->each(function ($file) { + $this->line('- ' . $file); + }); + + $this->line(''); + }); + } + + /** + * Get the console command arguments. + * + * @return array + */ + protected function getArguments() + { + return [ + ['draft', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'Which models to include', []], + ]; + } + + /** + * Get the console command options. + * + * @return array + */ + protected function getOptions() + { + return []; + } + + private function outputStyle($action) + { + if ($action === 'created') { + return 'error'; + } + + return 'comment'; + } +} diff --git a/tests/Feature/Generator/ControllerGeneratorTest.php b/tests/Feature/Generator/ControllerGeneratorTest.php index f229182c..db517062 100644 --- a/tests/Feature/Generator/ControllerGeneratorTest.php +++ b/tests/Feature/Generator/ControllerGeneratorTest.php @@ -2,10 +2,10 @@ namespace Tests\Feature\Generators; +use Tests\TestCase; use Blueprint\Blueprint; -use Blueprint\Generators\ControllerGenerator; use Blueprint\Lexers\StatementLexer; -use Tests\TestCase; +use Blueprint\Generators\ControllerGenerator; /** * @see ControllerGenerator @@ -70,10 +70,9 @@ public function output_writes_migration_for_controller_tree($definition, $path, $tree = $this->blueprint->analyze($tokens); $this->assertEquals(['created' => [$path]], $this->subject->output($tree)); - ++$iteration; + $iteration++; } - public function controllerTreeDataProvider() { return [ diff --git a/tests/Feature/Generator/FactoryGeneratorTest.php b/tests/Feature/Generator/FactoryGeneratorTest.php index c0934b7f..a59001fe 100644 --- a/tests/Feature/Generator/FactoryGeneratorTest.php +++ b/tests/Feature/Generator/FactoryGeneratorTest.php @@ -60,7 +60,6 @@ public function output_writes_migration_for_model_tree($definition, $path, $migr $this->assertEquals(['created' => [$path]], $this->subject->output($tree)); } - public function modelTreeDataProvider() { return [ @@ -68,4 +67,4 @@ public function modelTreeDataProvider() ['definitions/team.bp', 'database/factories/TeamFactory.php', 'factories/team.php'] ]; } -} \ No newline at end of file +} diff --git a/tests/Feature/Generator/ModelGeneratorTest.php b/tests/Feature/Generator/ModelGeneratorTest.php index 70fee269..063810c9 100644 --- a/tests/Feature/Generator/ModelGeneratorTest.php +++ b/tests/Feature/Generator/ModelGeneratorTest.php @@ -2,9 +2,9 @@ namespace Tests\Feature\Generators; +use Tests\TestCase; use Blueprint\Blueprint; use Blueprint\Generators\ModelGenerator; -use Tests\TestCase; class ModelGeneratorTest extends TestCase { @@ -81,10 +81,9 @@ public function output_writes_migration_for_model_tree($definition, $path, $mode $tree = $this->blueprint->analyze($tokens); $this->assertEquals(['created' => [$path]], $this->subject->output($tree)); - ++$iteration; + $iteration++; } - public function modelTreeDataProvider() { return [ diff --git a/tests/Feature/Generator/RouteGeneratorTest.php b/tests/Feature/Generator/RouteGeneratorTest.php index 5deba954..064fdf85 100644 --- a/tests/Feature/Generator/RouteGeneratorTest.php +++ b/tests/Feature/Generator/RouteGeneratorTest.php @@ -2,10 +2,10 @@ namespace Tests\Feature\Generators; +use Tests\TestCase; use Blueprint\Blueprint; -use Blueprint\Generators\RouteGenerator; use Blueprint\Lexers\StatementLexer; -use Tests\TestCase; +use Blueprint\Generators\RouteGenerator; /** * @see RouteGenerator @@ -57,7 +57,6 @@ public function output_writes_migration_for_route_tree($definition, $routes) $this->assertEquals(['updated' => [$path]], $this->subject->output($tree)); } - public function controllerTreeDataProvider() { return [