Skip to content

Commit

Permalink
refactor(commands)
Browse files Browse the repository at this point in the history
  • Loading branch information
shevelev-anatoliy committed Dec 19, 2024
1 parent ea8d0bf commit 27c2071
Show file tree
Hide file tree
Showing 14 changed files with 118 additions and 172 deletions.
17 changes: 12 additions & 5 deletions src/Laravel/src/Commands/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function handle(): int
}

if (! $this->testsMode) {
confirm('Can you quickly star our GitHub repository? 🙏🏻', true);
confirm('Can you quickly star our GitHub repository? 🙏🏻');

$this->components->bulletList([
'Star or contribute to MoonShine: https://github.com/moonshine-software/moonshine',
Expand Down Expand Up @@ -259,7 +259,7 @@ protected function initDirectories(): void
);
}

$this->makeDir($this->getDirectory() . '/Resources');
$this->makeDir($this->getDirectory('/Resources'));

$this->components->task('Resources directory created');
}
Expand All @@ -283,10 +283,14 @@ protected function initDashboard(): void
$this->components->task('Dashboard created');
}


protected function initLayout(): void
{
$compact = $this->confirmAction('Want to use a minimalist theme?', skipOption: 'default-layout', autoEnable: $this->testsMode, default: false);
$compact = $this->confirmAction(
'Want to use a minimalist theme?',
skipOption: 'default-layout',
autoEnable: $this->testsMode,
default: false,
);

$this->call(MakeLayoutCommand::class, [
'className' => 'MoonShineLayout',
Expand Down Expand Up @@ -323,7 +327,10 @@ private function confirmAction(

private function registerServiceProvider(): void
{
if (method_exists(ServiceProvider::class, 'addProviderToBootstrapFile') && file_exists(base_path('bootstrap/app.php'))) {
if (
method_exists(ServiceProvider::class, 'addProviderToBootstrapFile')
&& file_exists(base_path('bootstrap/app.php'))
) {
// @phpstan-ignore-next-line
ServiceProvider::addProviderToBootstrapFile(\App\Providers\MoonShineServiceProvider::class);

Expand Down
16 changes: 5 additions & 11 deletions src/Laravel/src/Commands/MakeApplyCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

use Illuminate\Contracts\Filesystem\FileNotFoundException;

use function Laravel\Prompts\outro;
use function Laravel\Prompts\text;
use function Laravel\Prompts\{outro, text};

use Symfony\Component\Console\Attribute\AsCommand;

Expand All @@ -28,23 +27,18 @@ public function handle(): int
required: true
);

$apply = $this->getDirectory() . "/Applies/$className.php";
$appliesDir = $this->getDirectory('/Applies');
$apply = "$appliesDir/$className.php";

if (! is_dir($this->getDirectory() . '/Applies')) {
$this->makeDir($this->getDirectory() . '/Applies');
}
$this->makeDirectory($appliesDir);

$this->copyStub('Apply', $apply, [
'{namespace}' => moonshineConfig()->getNamespace('\Applies'),
'DummyClass' => $className,
]);

outro(
"$className was created: " . str_replace(
base_path(),
'',
$apply
)
"$className was created: " . $this->getRelativePath($apply)
);

return self::SUCCESS;
Expand Down
30 changes: 10 additions & 20 deletions src/Laravel/src/Commands/MakeComponentCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

use Illuminate\Contracts\Filesystem\FileNotFoundException;

use function Laravel\Prompts\outro;
use function Laravel\Prompts\text;
use function Laravel\Prompts\{outro, text};

use Symfony\Component\Console\Attribute\AsCommand;

Expand Down Expand Up @@ -41,42 +40,33 @@ public function handle(): int
required: true
);

$component = $this->getDirectory() . "/Components/$className.php";
$componentsDir = $this->getDirectory('/Components');
$componentPath = "$componentsDir/$className.php";

if (! is_dir($this->getDirectory() . '/Components')) {
$this->makeDir($this->getDirectory() . '/Components');
}
$this->makeDirectory($componentsDir);

$view = str_replace('.blade.php', '', $view);
$viewPath = resource_path('views/' . str_replace('.', DIRECTORY_SEPARATOR, $view));
$viewPath .= '.blade.php';

if (! is_dir(\dirname($viewPath))) {
$this->makeDir(\dirname($viewPath));
}
$this->makeDirectory(
\dirname($viewPath)
);

$this->copyStub('view', $viewPath);

$this->copyStub('Component', $component, [
$this->copyStub('Component', $componentPath, [
'{namespace}' => moonshineConfig()->getNamespace('\Components'),
'{view}' => $view,
'DummyClass' => $className,
]);

outro(
"$className was created: " . str_replace(
base_path(),
'',
$component
)
"$className was created: " . $this->getRelativePath($componentPath)
);

outro(
"View was created: " . str_replace(
base_path(),
'',
$viewPath
)
"View was created: " . $this->getRelativePath($viewPath)
);

return self::SUCCESS;
Expand Down
24 changes: 9 additions & 15 deletions src/Laravel/src/Commands/MakeControllerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@

use Illuminate\Contracts\Filesystem\FileNotFoundException;

use function Laravel\Prompts\outro;
use function Laravel\Prompts\text;
use function Laravel\Prompts\{outro, text};

use Symfony\Component\Console\Attribute\AsCommand;

#[AsCommand(name: 'moonshine:controller')]
class MakeControllerCommand extends MoonShineCommand
{
protected $signature = 'moonshine:controller {className?}';
protected $signature = 'moonshine:controller {name?}';

protected $description = 'Create controller';

Expand All @@ -23,28 +22,23 @@ class MakeControllerCommand extends MoonShineCommand
*/
public function handle(): int
{
$className = $this->argument('className') ?? text(
$name = $this->argument('name') ?? text(
'Class name',
required: true
);

$controller = $this->getDirectory() . "/Controllers/$className.php";
$controllersDir = $this->getDirectory('/Controllers');
$controllerPath = "$controllersDir/$name.php";

if (! is_dir($this->getDirectory() . '/Controllers')) {
$this->makeDir($this->getDirectory() . '/Controllers');
}
$this->makeDirectory($controllersDir);

$this->copyStub('Controller', $controller, [
$this->copyStub('Controller', $controllerPath, [
'{namespace}' => moonshineConfig()->getNamespace('\Controllers'),
'DummyClass' => $className,
'DummyClass' => $name,
]);

outro(
"$className was created: " . str_replace(
base_path(),
'',
$controller
)
"$name was created: " . $this->getRelativePath($controllerPath)
);

return self::SUCCESS;
Expand Down
31 changes: 10 additions & 21 deletions src/Laravel/src/Commands/MakeFieldCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Support\Facades\File;

use function Laravel\Prompts\outro;
use function Laravel\Prompts\select;
use function Laravel\Prompts\text;
use function Laravel\Prompts\{outro, select, text};

use MoonShine\UI\Fields\Field;
use Symfony\Component\Console\Attribute\AsCommand;
Expand Down Expand Up @@ -61,13 +59,12 @@ public function handle(): int
Field::class
);

$field = $this->getDirectory() . "/Fields/$className.php";
$fieldsDir = $this->getDirectory('/Fields');
$fieldPath = "$fieldsDir/$className.php";

if (! is_dir($this->getDirectory() . '/Fields')) {
$this->makeDir($this->getDirectory() . '/Fields');
}
$this->makeDirectory($fieldsDir);

$this->copyStub('Field', $field, [
$this->copyStub('Field', $fieldPath, [
'{namespace}' => moonshineConfig()->getNamespace('\Fields'),
'{view}' => $view,
'{extend}' => $extends,
Expand All @@ -79,26 +76,18 @@ public function handle(): int
$viewPath = resource_path('views/' . str_replace('.', DIRECTORY_SEPARATOR, $view));
$viewPath .= '.blade.php';

if (! is_dir(\dirname($viewPath))) {
$this->makeDir(\dirname($viewPath));
}
$this->makeDirectory(
\dirname($viewPath)
);

$this->copyStub('view', $viewPath);

outro(
"$className was created: " . str_replace(
base_path(),
'',
$field
)
"$className was created: " . $this->getRelativePath($fieldPath)
);

outro(
"View was created: " . str_replace(
base_path(),
'',
$viewPath
)
"View was created: " . $this->getRelativePath($viewPath)
);

return self::SUCCESS;
Expand Down
15 changes: 5 additions & 10 deletions src/Laravel/src/Commands/MakeHandlerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,18 @@ public function handle(): int
required: true
);

$path = $this->getDirectory() . "/Handlers/$className.php";
$handlersDir = $this->getDirectory() . '/Handlers';
$handlerPath = "$handlersDir/$className.php";

if (! is_dir($this->getDirectory() . '/Handlers')) {
$this->makeDir($this->getDirectory() . '/Handlers');
}
$this->makeDirectory($handlersDir);

$this->copyStub('Handler', $path, [
$this->copyStub('Handler', $handlerPath, [
'{namespace}' => moonshineConfig()->getNamespace('\Handlers'),
'DummyHandler' => $className,
]);

outro(
"$className was created: " . str_replace(
base_path(),
'',
$path
)
"$className was created: " . $this->getRelativePath($handlerPath)
);

return self::SUCCESS;
Expand Down
19 changes: 6 additions & 13 deletions src/Laravel/src/Commands/MakeLayoutCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@

use Illuminate\Contracts\Filesystem\FileNotFoundException;

use function Laravel\Prompts\confirm;
use function Laravel\Prompts\outro;
use function Laravel\Prompts\text;
use function Laravel\Prompts\{confirm, outro, text};

use Symfony\Component\Console\Attribute\AsCommand;

Expand Down Expand Up @@ -36,30 +34,25 @@ public function handle(): int
$dir = 'Layouts';
}

$layout = $this->getDirectory() . "/$dir/$className.php";
$layoutsDir = $this->getDirectory() . "/$dir";
$layoutPath = "$layoutsDir/$className.php";

if (! is_dir($this->getDirectory() . "/$dir")) {
$this->makeDir($this->getDirectory() . "/$dir");
}
$this->makeDirectory($layoutsDir);

$compact = ! $this->option('full') && ($this->option('compact') || confirm('Want to use a minimalist theme?'));

$extendClassName = $compact ? 'CompactLayout' : 'AppLayout';
$extends = "MoonShine\Laravel\Layouts\\$extendClassName";

$this->copyStub('Layout', $layout, [
$this->copyStub('Layout', $layoutPath, [
'{namespace}' => moonshineConfig()->getNamespace('\\' . str_replace('/', '\\', $dir)),
'{extend}' => $extends,
'{extendShort}' => class_basename($extends),
'DummyLayout' => $className,
]);

outro(
"$className was created: " . str_replace(
base_path(),
'',
$layout
)
"$className was created: " . $this->getRelativePath($layoutPath)
);

if ($this->option('default') || confirm('Use the default template in the system?')) {
Expand Down
19 changes: 6 additions & 13 deletions src/Laravel/src/Commands/MakePageCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@

use Illuminate\Contracts\Filesystem\FileNotFoundException;

use function Laravel\Prompts\outro;
use function Laravel\Prompts\select;
use function Laravel\Prompts\text;
use function Laravel\Prompts\{outro, select, text};

use Symfony\Component\Console\Attribute\AsCommand;

Expand Down Expand Up @@ -85,25 +83,20 @@ private function makePage(
$dir = \is_null($dir) ? 'Pages' : $dir;
$extends = $extends === null || $extends === '' || $extends === '0' ? 'Page' : $extends;

$page = $this->getDirectory() . "/$dir/$className.php";
$pagesDir = $this->getDirectory("/$dir");
$pagePath = "$pagesDir/$className.php";

if (! is_dir($this->getDirectory() . "/$dir")) {
$this->makeDir($this->getDirectory() . "/$dir");
}
$this->makeDirectory($pagesDir);

$this->copyStub($stub, $page, [
$this->copyStub($stub, $pagePath, [
'{namespace}' => moonshineConfig()->getNamespace('\\' . str_replace('/', '\\', $dir)),
'DummyPage' => $className,
'DummyTitle' => $className,
'{extendShort}' => $extends,
]);

outro(
"$className was created: " . str_replace(
base_path(),
'',
$page
)
"$className was created: " . $this->getRelativePath($pagePath)
);

if (! $this->option('without-register')) {
Expand Down
Loading

0 comments on commit 27c2071

Please sign in to comment.