Skip to content

Commit

Permalink
🚧 Make new theme UI
Browse files Browse the repository at this point in the history
  • Loading branch information
juzaweb committed Aug 11, 2023
1 parent 1e63d3e commit 0531ff3
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 34 deletions.
71 changes: 47 additions & 24 deletions modules/DevTool/Commands/Theme/ThemeGeneratorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

class ThemeGeneratorCommand extends Command
{
Expand All @@ -14,7 +16,7 @@ class ThemeGeneratorCommand extends Command
*
* @var string
*/
protected $signature = 'theme:make {name}';
protected $name = 'theme:make {name}';

/**
* The console command description.
Expand All @@ -35,14 +37,14 @@ class ThemeGeneratorCommand extends Command
*
* @var array
*/
protected $theme;
protected array $theme;

/**
* Created Theme Structure.
*
* @var array
*/
protected $themeFolders;
protected array $themeFolders;

/**
* Theme Stubs.
Expand All @@ -51,7 +53,7 @@ class ThemeGeneratorCommand extends Command
*/
protected string $themeStubPath;

public function handle()
public function handle(): void
{
$this->themePath = config('juzaweb.theme.path');
$this->themeFolders = config('theme.stubs.folders');
Expand All @@ -64,21 +66,22 @@ public function handle()
* Theme Initialize.
*
* @return void
* @throws FileNotFoundException
*/
protected function init(): void
{
$createdThemePath = $this->themePath . '/' . $this->theme['name'];
$createdThemePath = $this->themePath.'/'.$this->theme['name'];

if (File::isDirectory($createdThemePath)) {
$this->error('Sorry Boss '. ucfirst($this->theme['name']) .' Theme Folder Already Exist !!!');
$this->error('Sorry Boss '.ucfirst($this->theme['name']).' Theme Folder Already Exist !!!');
exit();
}

$this->generateThemeInfo();

$themeStubFiles = config('theme.stubs.files');
$themeStubFiles['theme'] = 'theme.json';
$themeStubFiles['changelog'] = 'changelog.yml';
$themeStubFiles['changelog'] = 'changelog.md';
$this->makeDir($createdThemePath);

foreach ($this->themeFolders as $key => $folder) {
Expand All @@ -97,44 +100,46 @@ protected function init(): void
*/
public function generateThemeInfo(): void
{
$this->theme['title'] = Str::ucfirst($this->theme['name']);
$this->theme['description'] = Str::ucfirst($this->theme['name']) . ' description';
$this->theme['author'] = 'Author Name';
$this->theme['version'] = '1.0';
$this->theme['title'] = $this->option('title') ?? Str::ucfirst($this->theme['name']);
$this->theme['description'] = $this->option('description')
?? Str::ucfirst($this->theme['name']).' description';
$this->theme['author'] = $this->option('author');
$this->theme['version'] = $this->option('version');
$this->theme['parent'] = '';
}

/**
* Make directory.
*
* @param string $directory
* @param string $directory
*
* @return void
*/
protected function makeDir(string $directory): void
{
if (! File::isDirectory($directory)) {
if (!File::isDirectory($directory)) {
File::makeDirectory($directory, 0755, true);
}
}

/**
* Create theme stubs.
*
* @param array $themeStubFiles
* @param string $createdThemePath
* @param array $themeStubFiles
* @param string $createdThemePath
* @throws FileNotFoundException
*/
public function createStubs($themeStubFiles, $createdThemePath)
public function createStubs($themeStubFiles, $createdThemePath): void
{
foreach ($themeStubFiles as $filename => $storePath) {
if ($filename == 'changelog') {
$filename = 'changelog' . pathinfo($storePath, PATHINFO_EXTENSION);
$filename = 'changelog'.pathinfo($storePath, PATHINFO_EXTENSION);
} elseif ($filename == 'theme') {
$filename = pathinfo($storePath, PATHINFO_EXTENSION);
} elseif ($filename == 'css' || $filename == 'js') {
$this->theme[$filename] = ltrim(
$storePath,
rtrim('assets', '/') . '/'
rtrim('assets', '/').'/'
);
}

Expand All @@ -146,12 +151,13 @@ public function createStubs($themeStubFiles, $createdThemePath)
/**
* Make file.
*
* @param string $file
* @param string $storePath
* @param string $file
* @param string $storePath
*
* @return void
* @throws FileNotFoundException
*/
protected function makeFile($file, $storePath)
protected function makeFile($file, $storePath): void
{
if (File::exists($file)) {
$content = $this->replaceStubs(File::get($file));
Expand All @@ -162,11 +168,11 @@ protected function makeFile($file, $storePath)
/**
* Replace Stub string.
*
* @param string $contents
* @param string $contents
*
* @return string
*/
protected function replaceStubs($contents)
protected function replaceStubs($contents): string
{
$mainString = [
'[NAME]',
Expand All @@ -192,6 +198,23 @@ protected function replaceStubs($contents)

protected function getThemeStubPath(): string
{
return __DIR__ . '/../../stubs/theme';
return __DIR__.'/../../stubs/theme';
}

protected function getArguments(): array
{
return [
['name', InputArgument::REQUIRED, 'Theme Name'],
];
}

protected function getOptions(): array
{
return [
['title', null, InputOption::VALUE_OPTIONAL, 'Theme Title'],
['description', null, InputOption::VALUE_OPTIONAL, 'Theme Description'],
['author', null, InputOption::VALUE_OPTIONAL, 'Theme Author', 'Author Name'],
['version', null, InputOption::VALUE_OPTIONAL, 'Theme Version', '1.0'],
];
}
}
35 changes: 34 additions & 1 deletion modules/DevTool/Http/Controllers/ThemeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@
namespace Juzaweb\DevTool\Http\Controllers;

use Illuminate\Contracts\View\View;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Artisan;
use Inertia\Response;
use Juzaweb\CMS\Contracts\LocalThemeRepositoryContract;
use Juzaweb\DevTool\Http\Requests\Theme\StoreRequest;
use Symfony\Component\Console\Output\BufferedOutput;

class ThemeController extends Controller
{
Expand Down Expand Up @@ -50,7 +55,7 @@ public function edit(Request $request, string $name): View|Response
);
}

public function create()
public function create(): View|Response
{
$title = "Make new themes";
$configs = $this->getConfigs('themes');
Expand All @@ -60,4 +65,32 @@ public function create()
compact('configs', 'title')
);
}

public function store(StoreRequest $request): JsonResponse|RedirectResponse
{
$outputBuffer = new BufferedOutput();

try {
Artisan::call(
'theme:make',
[
'name' => $request->input('name'),
'title' => $request->input('title'),
'description' => $request->input('description'),
'author' => $request->input('author'),
'version' => $request->input('version'),
],
$outputBuffer
);
} catch (\Throwable $e) {
return $this->error($e->getMessage());
}

return $this->success(
[
'message' => 'New theme created successfully!',
'output' => $outputBuffer->fetch(),
]
);
}
}
24 changes: 24 additions & 0 deletions modules/DevTool/Http/Requests/Theme/StoreRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* JUZAWEB CMS - Laravel CMS for Your Project
*
* @package juzaweb/juzacms
* @author The Anh Dang
* @link https://juzaweb.com
* @license GNU V2
*/

namespace Juzaweb\DevTool\Http\Requests\Theme;

use Illuminate\Foundation\Http\FormRequest;

class StoreRequest extends FormRequest
{
public function rules(): array
{
return [
'name' => ['required', 'string'],
'title' => ['required', 'string'],
];
}
}
2 changes: 1 addition & 1 deletion resources/js/components/form/buttons/button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default function Button(props: ButtonProps) {
<button
type={props.type || 'button'}
id={props.id}
className={`btn ${props.class || 'btn-primary'}`}
className={`btn btn-primary ${props.class}`}
disabled={props.loading}
>
{/*{props.loading ? (<>
Expand Down
5 changes: 5 additions & 0 deletions resources/js/components/form/inputs/input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface InputProps {
class?: string;
placeholder?: string;
required?: boolean;
description?: string;
}

export default function Input(props: InputProps = {type: 'text', required: false, class: ''}) {
Expand All @@ -29,6 +30,10 @@ export default function Input(props: InputProps = {type: 'text', required: false
placeholder={props.placeholder}
required={props.required}
/>

{props.description && (
<small className="form-text text-muted">{props.description}</small>
)}
</div>
);
}
9 changes: 8 additions & 1 deletion resources/js/components/form/inputs/textarea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface TextareaProps {
placeholder?: string;
required?: boolean;
rows?: number;
description?: string;
}

const TextareaDefaults: TextareaProps = {
Expand All @@ -30,7 +31,13 @@ export default function Textarea(props: TextareaProps) {
id={props.id}
autoComplete="off"
rows={props.rows}
placeholder={props.placeholder}>{props.value}</textarea>
placeholder={props.placeholder}
defaultValue={props.value}
></textarea>

{props.description && (
<small className="form-text text-muted">{props.description}</small>
)}
</div>
);
}
2 changes: 1 addition & 1 deletion resources/js/pages/dev-tool/components/top-options.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export default function TopOptions(
aria-haspopup="true"
aria-expanded="false"
>
Make
New Module
</button>
<div className="dropdown-menu" aria-labelledby="dropdownMakeButton">
<Link className="dropdown-item" href={admin_url(`dev-tools/themes/create`)}>{__('Make New Theme')}</Link>
Expand Down
Loading

0 comments on commit 0531ff3

Please sign in to comment.