Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Replace '/' with DIRECTORY_SEPARATOR in the File generation Concerns #103

Merged
merged 3 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"));
});
Loading