Skip to content

Commit

Permalink
refactor: os aware paths
Browse files Browse the repository at this point in the history
  • Loading branch information
regnerisch committed Nov 30, 2023
1 parent d82f627 commit 8c35350
Show file tree
Hide file tree
Showing 15 changed files with 106 additions and 32 deletions.
4 changes: 2 additions & 2 deletions src/Actions/CopyAndRefactorDirectoryAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public function execute(string $sourcePath, string $targetPath, array $refactor

foreach ($files as $file) {
$this->copyAndRefactorFileAction->execute(
beyond_os_aware_path($sourcePath.'/'.$file->getFilename()),
beyond_os_aware_path($targetPath.'/'.$file->getFilename()),
$sourcePath.'/'.$file->getFilename(),
$targetPath.'/'.$file->getFilename(),
$refactor,
$force
);
Expand Down
10 changes: 10 additions & 0 deletions src/Actions/CopyDirectoryAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,18 @@

class CopyDirectoryAction
{
public function __construct(
private readonly NormalizePathAction $normalizePathAction
) {
}

public function execute(string $srcPath, string $targetPath, bool $force = false): void
{
[$srcPath, $targetPath] = $this->normalizePathAction->execute([
$srcPath,
$targetPath,
]);

$fs = new Filesystem();

if (! $force && $fs->exists($targetPath)) {
Expand Down
10 changes: 10 additions & 0 deletions src/Actions/CopyFileAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,18 @@

class CopyFileAction
{
public function __construct(
private readonly NormalizePathAction $normalizePathAction
) {
}

public function execute(string $srcPath, string $targetPath, bool $force = false): void
{
[$srcPath, $targetPath] = $this->normalizePathAction->execute([
$srcPath,
$targetPath,
]);

$fs = new Filesystem();

$fs->ensureDirectoryExists(
Expand Down
9 changes: 8 additions & 1 deletion src/Actions/CreateDirectoryAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

class CreateDirectoryAction
{
public function __construct(
private readonly NormalizePathAction $normalizePathAction
) {
}

/**
* @param string|array<string> $directory
*/
Expand All @@ -19,6 +24,8 @@ public function execute(string|array $directory): void
return;
}

(new Filesystem())->ensureDirectoryExists(beyond_os_aware_path(base_path()."/modules/$directory"));
(new Filesystem())->ensureDirectoryExists(
$this->normalizePathAction->execute(base_path('modules/'.$directory))
);
}
}
7 changes: 7 additions & 0 deletions src/Actions/CreateFileAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@

class CreateFileAction
{
public function __construct(
private readonly NormalizePathAction $normalizePathAction
) {
}

/**
* @param string|array<int, string>|array<string, string> $files
*/
Expand All @@ -21,6 +26,8 @@ public function execute(string|array $files): void
$contents = '';
}

$file = $this->normalizePathAction->execute($file);

$fs->ensureDirectoryExists(dirname($file));

$fs->put($file, $contents);
Expand Down
7 changes: 7 additions & 0 deletions src/Actions/DeletePathAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,15 @@

class DeletePathAction
{
public function __construct(
private readonly NormalizePathAction $normalizePathAction
) {
}

public function execute(string $path): void
{
$path = $this->normalizePathAction->execute($path);

$fs = new Filesystem();

if ($fs->isDirectory($path)) {
Expand Down
10 changes: 10 additions & 0 deletions src/Actions/MoveFileAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,18 @@

class MoveFileAction
{
public function __construct(
private readonly NormalizePathAction $normalizePathAction
) {
}

public function execute(string $sourcePath, string $targetPath, bool $force = false): void
{
[$sourcePath, $targetPath] = $this->normalizePathAction->execute([
$sourcePath,
$targetPath,
]);

$fs = new Filesystem();

$fs->ensureDirectoryExists(dirname($targetPath));
Expand Down
29 changes: 29 additions & 0 deletions src/Actions/NormalizePathAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace AkrilliA\LaravelBeyond\Actions;

use Illuminate\Support\Arr;
use Illuminate\Support\Str;

class NormalizePathAction
{
/**
* @param array<string>|string $path
* @return array<string>|string
*/
public function execute(array|string $path): array|string
{
$single = is_string($path);
$paths = Arr::wrap($path);

foreach ($paths as $k => $p) {
$paths[$k] = Str::replace('/', DIRECTORY_SEPARATOR, $p);
}

if ($single) {
return array_pop($paths);
}

return $paths;
}
}
4 changes: 2 additions & 2 deletions src/Commands/MakeModelCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function setup(NameResolver $nameResolver): void

beyond_copy_stub(
'migration.create.stub',
beyond_os_aware_path(base_path()."/modules/$module/Infrastructure/Database/Migrations/$fileName.php"),
base_path()."/modules/$module/Infrastructure/Database/Migrations/$fileName.php",
[
'{{ table }}' => $tableName,
],
Expand All @@ -49,7 +49,7 @@ public function setup(NameResolver $nameResolver): void

beyond_copy_stub(
'factory.stub',
beyond_os_aware_path(base_path()."/modules/$module/Infrastructure/factories/$fileName.php"),
base_path()."/modules/$module/Infrastructure/factories/$fileName.php",
[
'{{ namespace }}' => $namespace,
'{{ model }}' => $fileName,
Expand Down
12 changes: 6 additions & 6 deletions src/Commands/MakeModuleCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function handle(): void
$full = (bool) $this->option('full');

$this->copyDirectoryAction->execute(
beyond_os_aware_path(__DIR__.'/../../stubs/Module'),
__DIR__.'/../../stubs/Module',
beyond_modules_path($module),
$force,
);
Expand All @@ -44,11 +44,11 @@ public function handle(): void
private function moveAndRefactorModuleFiles(string $module, bool $force = false): void
{
$files = [
beyond_os_aware_path('Providers/ModuleAuthServiceProvider.stub') => beyond_os_aware_path("Providers/{$module}AuthServiceProvider.php"),
beyond_os_aware_path('Providers/ModuleEventServiceProvider.stub') => beyond_os_aware_path("Providers/{$module}EventServiceProvider.php"),
beyond_os_aware_path('Providers/ModuleRouteServiceProvider.stub') => beyond_os_aware_path("Providers/{$module}RouteServiceProvider.php"),
beyond_os_aware_path('Providers/ModuleServiceProvider.stub') => beyond_os_aware_path("Providers/{$module}ServiceProvider.php"),
beyond_os_aware_path('App/routes.stub') => beyond_os_aware_path('App/routes.php'),
'Providers/ModuleAuthServiceProvider.stub' => "Providers/{$module}AuthServiceProvider.php",
'Providers/ModuleEventServiceProvider.stub' => "Providers/{$module}EventServiceProvider.php",
'Providers/ModuleRouteServiceProvider.stub' => "Providers/{$module}RouteServiceProvider.php",
'Providers/ModuleServiceProvider.stub' => "Providers/{$module}ServiceProvider.php",
'App/routes.stub' => 'App/routes.php',
];

foreach ($files as $from => $to) {
Expand Down
2 changes: 1 addition & 1 deletion src/LaravelBeyondServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function beyondCommands(): array
$exclude = [];

$fs = new Filesystem();
$files = $fs->files(beyond_os_aware_path(__DIR__.'/Commands'));
$files = $fs->files(__DIR__.DIRECTORY_SEPARATOR.'Commands');

return array_map(
fn ($file) => 'AkrilliA\\LaravelBeyond\\Commands\\'.$file->getBasename('.php'),
Expand Down
12 changes: 5 additions & 7 deletions src/NameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,10 @@ private function init(): void
$this->directory ? '\\'.$this->directory : '',
);

$this->path = beyond_os_aware_path(
sprintf(
'%s/'.$this->command->getFileNameTemplate(),
Str::lcfirst(Str::replace('\\', '/', $this->namespace)),
$this->className,
)
$this->path = sprintf(
'%s'.DIRECTORY_SEPARATOR.$this->command->getFileNameTemplate(),
Str::lcfirst(Str::replace('\\', DIRECTORY_SEPARATOR, $this->namespace)),
$this->className,
);
}

Expand All @@ -99,6 +97,6 @@ private function setDirectoryAndClassName(string $name): void

$this->className = array_pop($parts);

$this->directory = beyond_os_aware_path(implode('/', $parts));
$this->directory = implode('\\', $parts);
}
}
17 changes: 5 additions & 12 deletions src/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use AkrilliA\LaravelBeyond\Actions\CopyAndRefactorFileAction;
use AkrilliA\LaravelBeyond\Actions\CopyFileAction;
use AkrilliA\LaravelBeyond\Actions\NormalizePathAction;
use AkrilliA\LaravelBeyond\Actions\RefactorFileAction;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Str;
Expand All @@ -16,7 +17,7 @@ function beyond_path(): string
if (! function_exists('beyond_modules_path')) {
function beyond_modules_path(string $path = ''): string
{
return base_path(beyond_os_aware_path("modules/$path"));
return base_path('modules'.DIRECTORY_SEPARATOR.$path);
}
}

Expand All @@ -33,12 +34,12 @@ function beyond_module_name(string $name): string
*/
function beyond_copy_stub(string $stub, string $path, array $refactor = [], bool $force = false): void
{
$stub = file_exists($stubPath = base_path(beyond_os_aware_path('stubs/beyond.'.$stub)))
$stub = file_exists($stubPath = base_path('stubs/beyond.'.$stub))
? $stubPath
: beyond_os_aware_path(beyond_path().'/stubs/'.$stub);
: beyond_path().'/stubs/'.$stub;

$action = new CopyAndRefactorFileAction(
new CopyFileAction(),
new CopyFileAction(new NormalizePathAction()),
new RefactorFileAction()
);

Expand Down Expand Up @@ -71,11 +72,3 @@ function ($directory) {
return $directories;
}
}

if (! function_exists('beyond_os_aware_path')) {

function beyond_os_aware_path(string $path): string
{
return Str::of($path)->replace('/', DIRECTORY_SEPARATOR)->value();
}
}
3 changes: 3 additions & 0 deletions tests/Commands/MakeModuleCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ public function testCanMakeFullModuleUsingForce(): void
$code->assertOk();
}

/**
* @return array<string>
*/
protected function getPaths(): array
{
return [
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private function removeDirectory(string $directory): void

while (false !== $file = readdir($handle)) {
if (! in_array($file, ['.', '..'], true)) {
$path = $directory.'/'.$file;
$path = $directory.DIRECTORY_SEPARATOR.$file;

if (is_dir($path)) {
$this->removeDirectory($path);
Expand Down

0 comments on commit 8c35350

Please sign in to comment.