Add theme switch command
This commit is contained in:
parent
9011378232
commit
6a5468df53
3 changed files with 112 additions and 5 deletions
77
app/Console/Commands/Theme/SwitchCommand.php
Normal file
77
app/Console/Commands/Theme/SwitchCommand.php
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
<?php
|
||||
|
||||
namespace App\Console\Commands\Theme;
|
||||
|
||||
use App\Lib\Helpers\ToolboxHelper;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Contracts\Console\PromptsForMissingInput;
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
use function Laravel\Prompts\search;
|
||||
|
||||
class SwitchCommand extends Command implements PromptsForMissingInput
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'theme:switch {theme : The theme folder name on theme-merge project}';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Switch GGW theme and install it';
|
||||
|
||||
/**
|
||||
* Prompt for missing input arguments using the returned questions.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function promptForMissingArgumentsUsing()
|
||||
{
|
||||
return [
|
||||
'theme' => 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');
|
||||
}
|
||||
}
|
||||
|
|
@ -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<int|string, mixed> $array1
|
||||
* @param array<int|string, mixed> $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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -1 +1 @@
|
|||
Subproject commit 8f5a9df62d44d9b60fa07da8cf4bf04a85d5694b
|
||||
Subproject commit dfaf7e3bbb4758a6207543d42fa91ea62bca3da6
|
||||
Loading…
Add table
Reference in a new issue