From ce803907a0e9c3787aecdbacd763b227e64e312e Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Thu, 13 Oct 2022 22:35:01 +0300 Subject: [PATCH 1/6] Removed `force` flag to install a repository from migrate command --- src/Processors/Migrate.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Processors/Migrate.php b/src/Processors/Migrate.php index b3dc1a84..78bc7f15 100644 --- a/src/Processors/Migrate.php +++ b/src/Processors/Migrate.php @@ -25,7 +25,6 @@ protected function ensureRepository(): void { $this->runCommand(Names::INSTALL, [ '--' . Options::CONNECTION => $this->options->connection, - '--' . Options::FORCE => true, ]); } From a570f2d108e7eba41d437c9b4e9539979165b60a Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Thu, 13 Oct 2022 22:44:42 +0300 Subject: [PATCH 2/6] Added silent mode for commands --- src/Concerns/Artisan.php | 6 +++--- src/Concerns/Optionable.php | 2 ++ src/Console/Fresh.php | 1 + src/Console/Install.php | 6 ------ src/Console/Make.php | 1 + src/Console/Migrate.php | 1 + src/Console/Refresh.php | 1 + src/Console/Reset.php | 1 + src/Console/Rollback.php | 1 + src/Console/Status.php | 1 + src/Constants/Options.php | 2 ++ src/Contracts/Notification.php | 4 ++-- src/Notifications/Basic.php | 8 +++++--- src/Notifications/Beautiful.php | 2 +- src/Notifications/Notification.php | 13 +++++++++++-- src/Processors/Processor.php | 28 ++++++++++++++-------------- src/Services/Migrator.php | 12 ++++++------ src/Values/Options.php | 2 ++ 18 files changed, 55 insertions(+), 37 deletions(-) diff --git a/src/Concerns/Artisan.php b/src/Concerns/Artisan.php index fb3c4d1e..d6b4e627 100644 --- a/src/Concerns/Artisan.php +++ b/src/Concerns/Artisan.php @@ -4,8 +4,8 @@ namespace DragonCode\LaravelActions\Concerns; +use Illuminate\Console\OutputStyle; use Illuminate\Support\Facades\Artisan as Command; -use Symfony\Component\Console\Output\OutputInterface; trait Artisan { @@ -14,11 +14,11 @@ trait Artisan * * @param string $command * @param array $parameters - * @param \Symfony\Component\Console\Output\OutputInterface|null $outputBuffer + * @param \Illuminate\Console\OutputStyle|null $outputBuffer * * @return void */ - protected function artisan(string $command, array $parameters = [], ?OutputInterface $outputBuffer = null): void + protected function artisan(string $command, array $parameters = [], ?OutputStyle $outputBuffer = null): void { Command::call($command, $parameters, $outputBuffer); } diff --git a/src/Concerns/Optionable.php b/src/Concerns/Optionable.php index 14357b28..71134eb3 100644 --- a/src/Concerns/Optionable.php +++ b/src/Concerns/Optionable.php @@ -17,6 +17,7 @@ trait Optionable protected array $options = [ Options::CONNECTION, Options::FORCE, + Options::SILENT, ]; protected function configure(): void @@ -47,6 +48,7 @@ protected function availableOptions(): array [Options::PATH, null, InputOption::VALUE_OPTIONAL, 'The path to the actions files to be executed'], [Options::REALPATH, null, InputOption::VALUE_NONE, 'Indicate any provided action file paths are pre-resolved absolute path'], [Options::STEP, null, InputOption::VALUE_OPTIONAL, 'Force the actions to be run so they can be rolled back individually'], + [Options::SILENT, null, InputOption::VALUE_NONE, 'Turns off the output of informational messages'], ]; } diff --git a/src/Console/Fresh.php b/src/Console/Fresh.php index cc31fa4e..abf40372 100644 --- a/src/Console/Fresh.php +++ b/src/Console/Fresh.php @@ -22,5 +22,6 @@ class Fresh extends Command Options::FORCE, Options::PATH, Options::REALPATH, + Options::SILENT, ]; } diff --git a/src/Console/Install.php b/src/Console/Install.php index c2d4ab7d..aa6e3c2c 100644 --- a/src/Console/Install.php +++ b/src/Console/Install.php @@ -3,7 +3,6 @@ namespace DragonCode\LaravelActions\Console; use DragonCode\LaravelActions\Constants\Names; -use DragonCode\LaravelActions\Constants\Options; use DragonCode\LaravelActions\Processors\Install as InstallProcessor; use DragonCode\LaravelActions\Processors\Processor; @@ -14,9 +13,4 @@ class Install extends Command protected $description = 'Create the actions repository'; protected Processor|string $processor = InstallProcessor::class; - - protected array $options = [ - Options::CONNECTION, - Options::FORCE, - ]; } diff --git a/src/Console/Make.php b/src/Console/Make.php index 52020688..79b47987 100644 --- a/src/Console/Make.php +++ b/src/Console/Make.php @@ -24,5 +24,6 @@ class Make extends Command Options::FORCE, Options::PATH, Options::REALPATH, + Options::SILENT, ]; } diff --git a/src/Console/Migrate.php b/src/Console/Migrate.php index 6aab749c..83f53fc0 100644 --- a/src/Console/Migrate.php +++ b/src/Console/Migrate.php @@ -22,5 +22,6 @@ class Migrate extends Command Options::CONNECTION, Options::PATH, Options::REALPATH, + Options::SILENT, ]; } diff --git a/src/Console/Refresh.php b/src/Console/Refresh.php index 2d665a62..ed0873a1 100644 --- a/src/Console/Refresh.php +++ b/src/Console/Refresh.php @@ -20,5 +20,6 @@ class Refresh extends Command Options::FORCE, Options::PATH, Options::REALPATH, + Options::SILENT, ]; } diff --git a/src/Console/Reset.php b/src/Console/Reset.php index b96d764b..e27de5f7 100644 --- a/src/Console/Reset.php +++ b/src/Console/Reset.php @@ -20,5 +20,6 @@ class Reset extends Command Options::FORCE, Options::PATH, Options::REALPATH, + Options::SILENT, ]; } diff --git a/src/Console/Rollback.php b/src/Console/Rollback.php index d4c23fda..e0e07709 100644 --- a/src/Console/Rollback.php +++ b/src/Console/Rollback.php @@ -21,5 +21,6 @@ class Rollback extends Command Options::PATH, Options::REALPATH, Options::STEP, + Options::SILENT, ]; } diff --git a/src/Console/Status.php b/src/Console/Status.php index 072b55d8..48208423 100644 --- a/src/Console/Status.php +++ b/src/Console/Status.php @@ -21,5 +21,6 @@ class Status extends Command Options::CONNECTION, Options::PATH, Options::REALPATH, + Options::SILENT, ]; } diff --git a/src/Constants/Options.php b/src/Constants/Options.php index 3fdea976..da514fb4 100644 --- a/src/Constants/Options.php +++ b/src/Constants/Options.php @@ -18,5 +18,7 @@ class Options public const REALPATH = 'realpath'; + public const SILENT = 'silent'; + public const STEP = 'step'; } diff --git a/src/Contracts/Notification.php b/src/Contracts/Notification.php index 0cbe3b37..c28bacce 100644 --- a/src/Contracts/Notification.php +++ b/src/Contracts/Notification.php @@ -5,7 +5,7 @@ namespace DragonCode\LaravelActions\Contracts; use Closure; -use Symfony\Component\Console\Output\OutputInterface; +use Illuminate\Console\OutputStyle; interface Notification { @@ -13,7 +13,7 @@ public function info(string $string): void; public function line(string $string, ?string $style = null): void; - public function setOutput(OutputInterface $output): self; + public function setOutput(OutputStyle $output, bool $silent = false): self; public function task(string $description, Closure $task): void; diff --git a/src/Notifications/Basic.php b/src/Notifications/Basic.php index 3d5c217c..b803774a 100644 --- a/src/Notifications/Basic.php +++ b/src/Notifications/Basic.php @@ -10,9 +10,11 @@ class Basic extends Notification { public function line(string $string, ?string $style = null): void { - $styled = $style ? "<$style>$string" : $string; + if ($this->canSpeak()) { + $styled = $style ? "<$style>$string" : $string; - $this->output->writeln($styled, $this->verbosity); + $this->output->writeln($styled, $this->verbosity); + } } public function info(string $string): void @@ -35,7 +37,7 @@ public function task(string $description, Closure $task): void $run_time = number_format((microtime(true) - $start) * 1000, 2); - $this->info("Migrated: {$run_time}ms"); + $this->info("Done in {$run_time}ms"); } public function twoColumn(string $first, string $second): void diff --git a/src/Notifications/Beautiful.php b/src/Notifications/Beautiful.php index 9f8d2285..d28c07c4 100644 --- a/src/Notifications/Beautiful.php +++ b/src/Notifications/Beautiful.php @@ -42,6 +42,6 @@ protected function components(): Factory return $this->components; } - return $this->component = new Factory($this->output); + return $this->component = $this->canSpeak() ? new Factory($this->output) : optional(); } } diff --git a/src/Notifications/Notification.php b/src/Notifications/Notification.php index a0707169..ee181712 100644 --- a/src/Notifications/Notification.php +++ b/src/Notifications/Notification.php @@ -6,11 +6,14 @@ use Closure; use DragonCode\LaravelActions\Contracts\Notification as NotificationContract; +use Illuminate\Console\OutputStyle; use Symfony\Component\Console\Output\OutputInterface; abstract class Notification implements NotificationContract { - protected ?OutputInterface $output = null; + protected ?OutputStyle $output = null; + + protected bool $silent = false; protected int $verbosity = OutputInterface::VERBOSITY_NORMAL; @@ -24,10 +27,16 @@ abstract public function task(string $description, Closure $task): void; abstract public function twoColumn(string $first, string $second): void; - public function setOutput(OutputInterface $output): NotificationContract + public function setOutput(OutputStyle $output, bool $silent = false): NotificationContract { $this->output = $output; + $this->silent = $silent; return $this; } + + protected function canSpeak(): bool + { + return ! $this->silent; + } } diff --git a/src/Processors/Processor.php b/src/Processors/Processor.php index 7e8b6cf2..3deefe6c 100644 --- a/src/Processors/Processor.php +++ b/src/Processors/Processor.php @@ -16,34 +16,34 @@ use DragonCode\Support\Facades\Helpers\Arr; use DragonCode\Support\Facades\Helpers\Str; use DragonCode\Support\Filesystem\File; +use Illuminate\Console\OutputStyle; use Illuminate\Contracts\Events\Dispatcher; use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Output\OutputInterface; abstract class Processor { use Artisan; - abstract public function handle(): void; - public function __construct( - protected Options $options, - protected InputInterface $input, - protected OutputInterface $output, - protected Config $config, + protected Options $options, + protected InputInterface $input, + protected OutputStyle $output, + protected Config $config, protected ActionRepository $repository, - protected Git $git, - protected File $file, - protected Migrator $migrator, - protected Notification $notification, - protected Dispatcher $events, - protected Sorter $sorter + protected Git $git, + protected File $file, + protected Migrator $migrator, + protected Notification $notification, + protected Dispatcher $events, + protected Sorter $sorter ) { - $this->notification->setOutput($this->output); + $this->notification->setOutput($this->output, $this->options->silent); $this->repository->setConnection($this->options->connection); $this->migrator->setConnection($this->options->connection)->setOutput($this->output); } + abstract public function handle(): void; + protected function getFiles(string $path, ?Closure $filter = null): array { $file = Str::finish($path, '.php'); diff --git a/src/Services/Migrator.php b/src/Services/Migrator.php index 457c3286..0c3bc2af 100644 --- a/src/Services/Migrator.php +++ b/src/Services/Migrator.php @@ -12,19 +12,19 @@ use DragonCode\Support\Exceptions\FileNotFoundException; use DragonCode\Support\Facades\Helpers\Str; use DragonCode\Support\Filesystem\File; +use Illuminate\Console\OutputStyle; use Illuminate\Contracts\Foundation\Application; use Illuminate\Support\Facades\DB; -use Symfony\Component\Console\Output\OutputInterface; use Throwable; class Migrator { public function __construct( - protected File $file, - protected Notification $notification, + protected File $file, + protected Notification $notification, protected ActionRepository $repository, - protected Config $config, - protected Application $laravel + protected Config $config, + protected Application $laravel ) { } @@ -35,7 +35,7 @@ public function setConnection(?string $connection): self return $this; } - public function setOutput(OutputInterface $output): self + public function setOutput(OutputStyle $output): self { $this->notification->setOutput($output); diff --git a/src/Values/Options.php b/src/Values/Options.php index a52f7ac6..9ee7136b 100644 --- a/src/Values/Options.php +++ b/src/Values/Options.php @@ -25,6 +25,8 @@ class Options extends DataTransferObject public bool $realpath = false; public ?int $step = null; + + public bool $silent = false; public function resolvePath(): self { From b9c3028fa9b49e5f22c99a57afca520ef76f16aa Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Thu, 13 Oct 2022 22:45:23 +0300 Subject: [PATCH 3/6] Fix code-style --- src/Processors/Processor.php | 24 ++++++++++++------------ src/Services/Migrator.php | 8 ++++---- src/Values/Options.php | 2 +- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/Processors/Processor.php b/src/Processors/Processor.php index 3deefe6c..42802f58 100644 --- a/src/Processors/Processor.php +++ b/src/Processors/Processor.php @@ -24,26 +24,26 @@ abstract class Processor { use Artisan; + abstract public function handle(): void; + public function __construct( - protected Options $options, - protected InputInterface $input, - protected OutputStyle $output, - protected Config $config, + protected Options $options, + protected InputInterface $input, + protected OutputStyle $output, + protected Config $config, protected ActionRepository $repository, - protected Git $git, - protected File $file, - protected Migrator $migrator, - protected Notification $notification, - protected Dispatcher $events, - protected Sorter $sorter + protected Git $git, + protected File $file, + protected Migrator $migrator, + protected Notification $notification, + protected Dispatcher $events, + protected Sorter $sorter ) { $this->notification->setOutput($this->output, $this->options->silent); $this->repository->setConnection($this->options->connection); $this->migrator->setConnection($this->options->connection)->setOutput($this->output); } - abstract public function handle(): void; - protected function getFiles(string $path, ?Closure $filter = null): array { $file = Str::finish($path, '.php'); diff --git a/src/Services/Migrator.php b/src/Services/Migrator.php index 0c3bc2af..5d4e6ac3 100644 --- a/src/Services/Migrator.php +++ b/src/Services/Migrator.php @@ -20,11 +20,11 @@ class Migrator { public function __construct( - protected File $file, - protected Notification $notification, + protected File $file, + protected Notification $notification, protected ActionRepository $repository, - protected Config $config, - protected Application $laravel + protected Config $config, + protected Application $laravel ) { } diff --git a/src/Values/Options.php b/src/Values/Options.php index 9ee7136b..e5f70c6f 100644 --- a/src/Values/Options.php +++ b/src/Values/Options.php @@ -25,7 +25,7 @@ class Options extends DataTransferObject public bool $realpath = false; public ?int $step = null; - + public bool $silent = false; public function resolvePath(): self From 33c2bfd56496aa6047ef99781c6e88b23be27fc7 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Thu, 13 Oct 2022 23:20:37 +0300 Subject: [PATCH 4/6] Fixed git path --- src/Helpers/Git.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Helpers/Git.php b/src/Helpers/Git.php index 48663c10..84efc7d9 100644 --- a/src/Helpers/Git.php +++ b/src/Helpers/Git.php @@ -24,7 +24,7 @@ public function currentBranch(?string $path = null): ?string protected function exec(string $command, ?string $path = null): ?string { - return exec(sprintf('git --git-dir "%s" %s', $path, $command)); + return exec(sprintf('git -C "%s" %s', $path, $command)); } protected function hasGitDirectory(?string $path = null): bool From 4bc113bb03edc930caeaab988d70788d401a5bca Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Thu, 13 Oct 2022 23:37:14 +0300 Subject: [PATCH 5/6] Optimized commands --- src/Processors/Fresh.php | 9 --------- src/Processors/Migrate.php | 1 + 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/src/Processors/Fresh.php b/src/Processors/Fresh.php index dd7b8342..109629f1 100644 --- a/src/Processors/Fresh.php +++ b/src/Processors/Fresh.php @@ -12,7 +12,6 @@ class Fresh extends Processor public function handle(): void { $this->drop(); - $this->create(); $this->migrate(); } @@ -23,14 +22,6 @@ protected function drop(): void } } - protected function create(): void - { - $this->runCommand(Names::INSTALL, [ - '--' . Options::CONNECTION => $this->options->connection, - '--' . Options::FORCE => $this->options->force, - ]); - } - protected function migrate(): void { $this->runCommand(Names::MIGRATE, [ diff --git a/src/Processors/Migrate.php b/src/Processors/Migrate.php index 78bc7f15..b3dc1a84 100644 --- a/src/Processors/Migrate.php +++ b/src/Processors/Migrate.php @@ -25,6 +25,7 @@ protected function ensureRepository(): void { $this->runCommand(Names::INSTALL, [ '--' . Options::CONNECTION => $this->options->connection, + '--' . Options::FORCE => true, ]); } From 4880e6921ab226b14d8600e9ebb983b845331c75 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Thu, 13 Oct 2022 23:49:52 +0300 Subject: [PATCH 6/6] Added captions --- src/Processors/Migrate.php | 6 ++++++ src/Processors/Rollback.php | 6 ++++++ src/Processors/Status.php | 6 ++++++ 3 files changed, 18 insertions(+) diff --git a/src/Processors/Migrate.php b/src/Processors/Migrate.php index b3dc1a84..087fce47 100644 --- a/src/Processors/Migrate.php +++ b/src/Processors/Migrate.php @@ -18,9 +18,15 @@ class Migrate extends Processor public function handle(): void { $this->ensureRepository(); + $this->showCaption(); $this->runActions($this->getCompleted()); } + protected function showCaption(): void + { + $this->notification->info('Launching Actions'); + } + protected function ensureRepository(): void { $this->runCommand(Names::INSTALL, [ diff --git a/src/Processors/Rollback.php b/src/Processors/Rollback.php index f200ee51..643e6b6a 100644 --- a/src/Processors/Rollback.php +++ b/src/Processors/Rollback.php @@ -21,6 +21,7 @@ public function handle(): void if ($actions = $this->getActions($this->options->step)) { $this->fireEvent(ActionStarted::class, 'down'); + $this->showCaption(); $this->run($actions); $this->fireEvent(ActionEnded::class, 'down'); @@ -31,6 +32,11 @@ public function handle(): void $this->fireEvent(NoPendingActions::class, 'down'); } + protected function showCaption(): void + { + $this->notification->info('Rollback Actions'); + } + protected function run(array $actions): void { foreach ($actions as $row) { diff --git a/src/Processors/Status.php b/src/Processors/Status.php index 2effca8d..10741428 100644 --- a/src/Processors/Status.php +++ b/src/Processors/Status.php @@ -31,10 +31,16 @@ public function handle(): void } $this->showCaption(); + $this->showHeaders(); $this->showStatus($files, $completed); } protected function showCaption(): void + { + $this->notification->info('Show Status'); + } + + protected function showHeaders(): void { $this->notification->twoColumn($this->columnName, $this->columnStatus); }