From 6a5468df53472e1bb91c9fd072cbaa622fb37709 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Mon, 20 Nov 2023 15:42:16 +0100 Subject: [PATCH] Add theme switch command --- app/Console/Commands/Theme/SwitchCommand.php | 77 ++++++++++++++++++++ app/Lib/Helpers/ToolboxHelper.php | 38 +++++++++- resources/theme | 2 +- 3 files changed, 112 insertions(+), 5 deletions(-) create mode 100644 app/Console/Commands/Theme/SwitchCommand.php diff --git a/app/Console/Commands/Theme/SwitchCommand.php b/app/Console/Commands/Theme/SwitchCommand.php new file mode 100644 index 0000000..fe5c12c --- /dev/null +++ b/app/Console/Commands/Theme/SwitchCommand.php @@ -0,0 +1,77 @@ + fn () => search( + label: 'Search for a theme:', + placeholder: 'default-theme', + options: fn (string $value): array => \collect(File::directories(\resource_path('theme'))) + ->map(fn (string $dir_path) => \pathinfo($dir_path, \PATHINFO_BASENAME)) + ->filter(fn (string $dir_path) => $value ? Str::of($dir_path)->contains($value) : true) + ->filter(fn (string $dir_path) => $dir_path !== 'mounted') + ->all() + ) + ]; + } + + /** + * Execute the console command. + * + * @return void + */ + public function handle(): void + { + $mount_path = \resource_path('theme/mounted'); + + /** @var string */ + $theme = $this->argument('theme'); + + if (File::exists($mount_path)) { + \throw_if( + empty(ToolboxHelper::runConsoleCommand("readlink {$mount_path}", false)), + "{$mount_path} is not a link, cannot remove it" + ); + \throw_if( + !File::delete($mount_path), + "{$mount_path} Failed to remove file" + ); + } + + File::link(\resource_path("theme/{$theme}"), $mount_path); + + Artisan::call('optimize:clear'); + } +} diff --git a/app/Lib/Helpers/ToolboxHelper.php b/app/Lib/Helpers/ToolboxHelper.php index 6a9b932..d6b6fff 100644 --- a/app/Lib/Helpers/ToolboxHelper.php +++ b/app/Lib/Helpers/ToolboxHelper.php @@ -20,6 +20,36 @@ use Symfony\Component\Finder\SplFileInfo; class ToolboxHelper { + /** + * Run a terminal command and gives the output + * + * @param array|string $command The command to execute on terminal. + * @param boolean $throw Wether to throw en esception on failure or not. + * @return string|false Return false on failure only if $throw is false. + * @phpcs:disable Squiz.Commenting.FunctionComment.EmptyThrows + * @phpcs:disable Squiz.Commenting.FunctionCommentThrowTag.WrongNumber + * @throws \Illuminate\Process\Exceptions\ProcessTimedOutException + * @throws \Symfony\Component\Process\Exception\LogicException + * @throws \Symfony\Component\Process\Exception\InvalidArgumentException + * @throws \Symfony\Component\Process\Exception\RuntimeException + * @throws \LogicException + * @throws \RuntimeException If the command fails. + */ + public static function runConsoleCommand(array|string $command, bool $throw = true): string | false + { + // phpcs:enable + $process = \Illuminate\Support\Facades\Process::command($command)->run(); + if ($process->failed()) { + $code = $process->exitCode(); + $ouput = $process->errorOutput(); + if ($throw) { + throw new \RuntimeException("Failed to run command exit code {$code} with output \n{$ouput}"); + } + return false; + } + return $process->output(); + } + /** * @param array $array1 * @param array $array2 @@ -314,7 +344,7 @@ class ToolboxHelper ): Builder { $currentLocale = app()->currentLocale(); $locales = collect(config('app.locales')) - ->mapWithKeys(fn (string $locale) => [$locale => $locale]); + ->mapWithKeys(fn (string $locale) => [$locale => $locale]); if (!$locales->has($defaultLocale)) { // * Remove default locale. $locales->pull($defaultLocale); @@ -496,9 +526,9 @@ class ToolboxHelper public static function getClassesUsingNamespace(string $namespace): array { return collect(File::files(\app_path(Str::of($namespace)->replaceFirst('\App', '')))) - ->filter(fn (SplFileInfo $splFileInfo) => $splFileInfo->getExtension() === 'php') - ->map(fn (SplFileInfo $splFileInfo) => Str::of($splFileInfo->getBasename()) - ->replace('.' . $splFileInfo->getExtension(), '')->prepend("{$namespace}\\"))->all(); + ->filter(fn (SplFileInfo $splFileInfo) => $splFileInfo->getExtension() === 'php') + ->map(fn (SplFileInfo $splFileInfo) => Str::of($splFileInfo->getBasename()) + ->replace('.' . $splFileInfo->getExtension(), '')->prepend("{$namespace}\\"))->all(); } /** diff --git a/resources/theme b/resources/theme index 8f5a9df..dfaf7e3 160000 --- a/resources/theme +++ b/resources/theme @@ -1 +1 @@ -Subproject commit 8f5a9df62d44d9b60fa07da8cf4bf04a85d5694b +Subproject commit dfaf7e3bbb4758a6207543d42fa91ea62bca3da6