|
| 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 | +} |
0 commit comments