Skip to content

Commit

Permalink
Merge pull request #1457 from moonshine-software/base-dir-for-commands
Browse files Browse the repository at this point in the history
feat: BaseDir/BaseNamespace
  • Loading branch information
lee-to authored Jan 9, 2025
2 parents 37ce62b + d795960 commit c393f50
Show file tree
Hide file tree
Showing 13 changed files with 72 additions and 56 deletions.
2 changes: 1 addition & 1 deletion src/Laravel/src/Commands/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ protected function initDashboard(): void

$this->replaceInConfig(
'dashboard',
moonshineConfig()->getNamespace('\Pages\Dashboard') . "::class"
$this->getNamespace('\Pages\Dashboard') . "::class"
);

$this->components->task('Dashboard created');
Expand Down
2 changes: 1 addition & 1 deletion src/Laravel/src/Commands/MakeApplyCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#[AsCommand(name: 'moonshine:apply')]
class MakeApplyCommand extends MoonShineCommand
{
protected $signature = 'moonshine:apply {className?}';
protected $signature = 'moonshine:apply {className?} {--base-dir=} {--base-namespace=}';

protected $description = 'Create apply for Field';

Expand Down
8 changes: 2 additions & 6 deletions src/Laravel/src/Commands/MakeComponentCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#[AsCommand(name: 'moonshine:component')]
class MakeComponentCommand extends MoonShineCommand
{
protected $signature = 'moonshine:component {className?}';
protected $signature = 'moonshine:component {className?} {--base-dir=} {--base-namespace=}';

protected $description = 'Create component';

Expand All @@ -32,11 +32,7 @@ public function handle(): int

$view = $this->makeViewFromStub('admin.components', $stubsPath->name, $stubsPath->dir);

$stubsPath->prependDir(
$this->getDirectory('Components')
)->prependNamespace(
moonshineConfig()->getNamespace('Components')
);
$stubsPath = $this->qualifyStubsDir($stubsPath, 'Components');

$this->makeDir($stubsPath->dir);

Expand Down
2 changes: 1 addition & 1 deletion src/Laravel/src/Commands/MakeControllerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#[AsCommand(name: 'moonshine:controller')]
class MakeControllerCommand extends MoonShineCommand
{
protected $signature = 'moonshine:controller {className?}';
protected $signature = 'moonshine:controller {className?} {--base-dir=} {--base-namespace=}';

protected $description = 'Create controller';

Expand Down
8 changes: 2 additions & 6 deletions src/Laravel/src/Commands/MakeFieldCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#[AsCommand(name: 'moonshine:field')]
class MakeFieldCommand extends MoonShineCommand
{
protected $signature = 'moonshine:field {className?}';
protected $signature = 'moonshine:field {className?} {--base-dir=} {--base-namespace=}';

protected $description = 'Create field';

Expand All @@ -36,11 +36,7 @@ public function handle(): int

$view = $this->makeViewFromStub('admin.fields', $stubsPath->name, $stubsPath->dir);

$stubsPath->prependDir(
$this->getDirectory('Fields'),
)->prependNamespace(
moonshineConfig()->getNamespace('Fields'),
);
$stubsPath = $this->qualifyStubsDir($stubsPath, 'Fields');

$extends = select('Extends', $this->findExtends(), Field::class);

Expand Down
2 changes: 1 addition & 1 deletion src/Laravel/src/Commands/MakeHandlerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#[AsCommand(name: 'moonshine:handler')]
class MakeHandlerCommand extends MoonShineCommand
{
protected $signature = 'moonshine:handler {className?}';
protected $signature = 'moonshine:handler {className?} {--base-dir=} {--base-namespace=}';

protected $description = 'Create handler class';

Expand Down
8 changes: 2 additions & 6 deletions src/Laravel/src/Commands/MakeLayoutCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#[AsCommand(name: 'moonshine:layout')]
class MakeLayoutCommand extends MoonShineCommand
{
protected $signature = 'moonshine:layout {className?} {--compact} {--full} {--default} {--dir=}';
protected $signature = 'moonshine:layout {className?} {--compact} {--full} {--default} {--dir=} {--base-dir=} {--base-namespace=}';

protected $description = 'Create layout';

Expand All @@ -32,11 +32,7 @@ public function handle(): int

$dir = $this->option('dir') ?: 'Layouts';

$stubsPath->prependDir(
$this->getDirectory($dir),
)->prependNamespace(
moonshineConfig()->getNamespace($dir),
);
$stubsPath = $this->qualifyStubsDir($stubsPath, $dir);

$this->makeDir($stubsPath->dir);

Expand Down
10 changes: 3 additions & 7 deletions src/Laravel/src/Commands/MakePageCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#[AsCommand(name: 'moonshine:page')]
class MakePageCommand extends MoonShineCommand
{
protected $signature = 'moonshine:page {className?} {--force} {--without-register} {--crud} {--dir=} {--extends=}';
protected $signature = 'moonshine:page {className?} {--force} {--without-register} {--crud} {--dir=} {--extends=} {--base-dir=} {--base-namespace=}';

protected $description = 'Create page';

Expand All @@ -34,11 +34,7 @@ public function handle(): int

$dir = $this->option('dir') ?: 'Pages';

$stubsPath->prependDir(
$this->getDirectory($dir),
)->prependNamespace(
moonshineConfig()->getNamespace($dir),
);
$stubsPath = $this->qualifyStubsDir($stubsPath, $dir);

if (! $this->option('force') && ! $this->option('extends') && ! $this->option('crud')) {
$types = [
Expand Down Expand Up @@ -70,7 +66,7 @@ public function handle(): int
$stubsPath->prependDir(
$this->getDirectory("$dir/$name"),
)->prependNamespace(
moonshineConfig()->getNamespace("$dir\\$name"),
$this->getNamespace("$dir\\$name"),
);

$this->makePage($stubsPath, 'CrudPage', $type);
Expand Down
10 changes: 3 additions & 7 deletions src/Laravel/src/Commands/MakeResourceCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#[AsCommand(name: 'moonshine:resource')]
class MakeResourceCommand extends MoonShineCommand
{
protected $signature = 'moonshine:resource {className?} {--type=} {--m|model=} {--t|title=} {--test} {--pest} {--p|policy}';
protected $signature = 'moonshine:resource {className?} {--type=} {--m|model=} {--t|title=} {--test} {--pest} {--p|policy} {--base-dir=} {--base-namespace=}';

protected $description = 'Create resource';

Expand Down Expand Up @@ -42,11 +42,7 @@ public function handle(): int
->remove('resource', false)
->value();

$stubsPath->prependDir(
$this->getDirectory('Resources'),
)->prependNamespace(
moonshineConfig()->getNamespace('Resources'),
);
$stubsPath = $this->qualifyStubsDir($stubsPath, 'Resources');

$this->makeDir($stubsPath->dir);

Expand Down Expand Up @@ -95,7 +91,7 @@ public function handle(): int
'--without-register' => true,
]);

$pageNamespace = moonshineConfig()->getNamespace("\Pages\\$name\\$name");
$pageNamespace = $this->getNamespace("\Pages\\$name\\$name");

$replace += [
'{indexPage}' => "{$name}IndexPage",
Expand Down
2 changes: 1 addition & 1 deletion src/Laravel/src/Commands/MakeTypeCastCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#[AsCommand(name: 'moonshine:type-cast')]
class MakeTypeCastCommand extends MoonShineCommand
{
protected $signature = 'moonshine:type-cast {className?}';
protected $signature = 'moonshine:type-cast {className?} {--base-dir=} {--base-namespace=}';

protected $description = 'Create type cast class';

Expand Down
54 changes: 43 additions & 11 deletions src/Laravel/src/Commands/MoonShineCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,14 @@ abstract class MoonShineCommand extends Command
{
protected string $stubsDir = __DIR__ . '/../../stubs';

protected function getDirectory(string $path = ''): string
protected function getDirectory(string $path = '', ?string $base = null): string
{
return moonshineConfig()->getDir($path);
return moonshineConfig()->getDir($path, $base);
}

protected function getNamespace(string $path = '', ?string $base = null): string
{
return moonshineConfig()->getNamespace($path, $base);
}

protected function getRelativePath(string $path): string
Expand Down Expand Up @@ -159,16 +164,10 @@ protected function fastCreateFromStub(string $stub, string $dir): void
{
$className = $this->argument('className') ?? text(
'Class name',
required: true
required: true,
);

$stubsPath = new StubsPath($className, 'php');

$stubsPath->prependDir(
$this->getDirectory($dir)
)->prependNamespace(
moonshineConfig()->getNamespace($dir)
);
$stubsPath = $this->qualifyStubsDir(new StubsPath($className, 'php'), $dir);

$this->makeDir($stubsPath->dir);

Expand All @@ -180,10 +179,43 @@ protected function fastCreateFromStub(string $stub, string $dir): void
$this->wasCreatedInfo($stubsPath);
}

protected function qualifyStubsDir(StubsPath $stubsPath, string $dir, ?string $namespace = null): StubsPath
{
$baseDir = $this->hasOption('base-dir') ? $this->option('base-dir') : null;
$baseNamespace = $this->hasOption('base-namespace') ? $this->option('base-namespace') : null;

$toNamespace = static fn (string $str): string => str($str)
->trim('\\')
->trim('/')
->replace('/', '\\')
->explode('\\')
->map(static fn (string $segment): string => ucfirst($segment))
->implode('\\');

if ($baseDir !== null && $baseNamespace === null) {
$baseNamespace = $toNamespace($baseDir);
}

if ($namespace === null) {
$namespace = $toNamespace($dir);
}

$baseDir = $baseDir ? trim($baseDir, '/') : $baseDir;
$baseNamespace = $baseNamespace ? $toNamespace($baseNamespace) : $baseNamespace;

return $stubsPath->prependDir(
$this->getDirectory($dir, $baseDir),
)->prependNamespace(
$this->getNamespace($namespace, $baseNamespace),
);
}

protected function wasCreatedInfo(StubsPath $stubsPath): void
{
$path = $this->getRelativePath($stubsPath->getPath());

outro(
"$stubsPath->name was created: " . $this->getRelativePath($stubsPath->getPath()),
"$stubsPath->name was created: $path",
);
}
}
8 changes: 4 additions & 4 deletions src/Laravel/src/Commands/PublishCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ private function publishSystemForm(string $className, string $configKey): void

$this->replaceInConfig(
$configKey,
moonshineConfig()->getNamespace('\Forms\\' . $className) . "::class",
$this->getNamespace('\Forms\\' . $className) . "::class",
$className
);
}
Expand Down Expand Up @@ -203,7 +203,7 @@ private function publishSystemPage(string $className, string $configKey): void

$this->replaceInConfig(
$configKey,
moonshineConfig()->getNamespace('\Pages\\' . $className) . "::class",
$this->getNamespace('\Pages\\' . $className) . "::class",
$className
);
}
Expand All @@ -214,8 +214,8 @@ private function publishSystemPage(string $className, string $configKey): void
private function copySystemClass(string $name, string $dir): array
{
$classPath = "src/$dir/$name.php";
$fullClassPath = moonshineConfig()->getDir("/$dir/$name.php");
$targetNamespace = moonshineConfig()->getNamespace("\\$dir");
$fullClassPath = $this->getDirectory("/$dir/$name.php");
$targetNamespace = $this->getNamespace("\\$dir");

(new Filesystem())->put(
$fullClassPath,
Expand Down
12 changes: 8 additions & 4 deletions src/Laravel/src/DependencyInjection/MoonShineConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,18 @@ public function dir(string $dir, string $namespace): self
->set('namespace', $namespace);
}

public function getDir(string $path = ''): string
public function getDir(string $path = '', ?string $base = null): string
{
return $this->get('dir') . '/' . trim($path, '/');
$base ??= $this->get('dir');

return $base . '/' . trim($path, '/');
}

public function getNamespace(string $path = ''): string
public function getNamespace(string $path = '', ?string $base = null): string
{
return $this->get('namespace') . '\\' . trim($path, '\\');
$base ??= $this->get('namespace');

return $base . '\\' . trim($path, '\\');
}

/**
Expand Down

0 comments on commit c393f50

Please sign in to comment.