Skip to content

Commit

Permalink
Refactor: Replace '/' with DIRECTORY_SEPARATOR in the File generation…
Browse files Browse the repository at this point in the history
… Concerns
  • Loading branch information
coolsam726 committed Apr 24, 2024
1 parent 87f9147 commit 0d7da67
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
12 changes: 10 additions & 2 deletions src/Concerns/CanManipulateFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
namespace Coolsam\Modules\Concerns;

use Coolsam\Modules\Facades\FilamentModules;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Filesystem\Filesystem;
use Nwidart\Modules\Module;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

use function Laravel\Prompts\confirm;

Expand Down Expand Up @@ -34,12 +37,17 @@ protected function checkForCollision(array $paths): bool

/**
* @param array<string, string> $replacements
*
* @throws FileNotFoundException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
protected function copyStubToApp(string $stub, string $targetPath, array $replacements = []): void
{
$D = DIRECTORY_SEPARATOR;
$filesystem = app(Filesystem::class);

$stubPath = $this->getDefaultStubPath() . "/{$stub}.stub";
$stubPath = $this->getDefaultStubPath() . "{$D}{$stub}.stub";

$stub = str($filesystem->get($stubPath));

Expand Down Expand Up @@ -72,7 +80,7 @@ protected function writeFile(string $path, string $contents): void

protected function getDefaultStubPath(): string
{
return $this->getModule()->appPath('Commands/stubs');
return $this->getModule()->appPath('Commands' . DIRECTORY_SEPARATOR . 'stubs');
}

protected function getModule(): Module
Expand Down
4 changes: 2 additions & 2 deletions src/Concerns/GeneratesModularFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ protected function getArguments(): array

protected function resolveStubPath($stub): string
{
return FilamentModules::packagePath('Commands/' . trim($stub, DIRECTORY_SEPARATOR));
return FilamentModules::packagePath('Commands' . DIRECTORY_SEPARATOR . trim($stub, DIRECTORY_SEPARATOR));
}

public function getModule(): Module
Expand All @@ -43,7 +43,7 @@ protected function getPath($name): string
{
$name = Str::replaceFirst($this->rootNamespace(), 'app', $name);

return $this->getModule()->getExtraPath(str_replace('\\', '/', $name) . '.php');
return $this->getModule()->getExtraPath(str_replace('\\', DIRECTORY_SEPARATOR, $name) . '.php');
}

protected function possibleModels()
Expand Down
5 changes: 4 additions & 1 deletion tests/Unit/GeneratesModularFilesConcernTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@ public function getRelativeNamespace(): string

public function getStub()
{
$d = DIRECTORY_SEPARATOR;

return $this->resolveStubPath('stubs/filament-plugin.stub');
}
};
});

test('can generate the correct stubs path', function () {
// include the GeneratesModularFiles trait
$d = DIRECTORY_SEPARATOR;
expect($this->trait->getStub())
->toEqual(realpath(__DIR__.'/../../src/Commands/stubs/filament-plugin.stub'));
->toEqual(realpath(__DIR__."{$d}..{$d}..{$d}src{$d}Commands{$d}stubs{$d}filament-plugin.stub"));
});

0 comments on commit 0d7da67

Please sign in to comment.