diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 627198d..1ce7539 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -7,7 +7,7 @@ jobs: runs-on: ${{ matrix.os }} strategy: matrix: - os: [ubuntu-latest] + os: [ubuntu-latest, windows-latest] php: ['8.1', '8.2'] steps: @@ -18,6 +18,7 @@ jobs: uses: shivammathur/setup-php@v2 with: php-version: ${{ matrix.php }} + extensions: fileinfo tools: composer:v2 coverage: none diff --git a/composer.json b/composer.json index 50bbe0f..b2c0665 100644 --- a/composer.json +++ b/composer.json @@ -28,6 +28,7 @@ ], "require": { "php": "^8.1", + "ext-fileinfo": "^8.1", "illuminate/support": "^10.0", "illuminate/console": "^10.0", "illuminate/filesystem": "^10.0" diff --git a/src/Actions/CopyAndRefactorDirectoryAction.php b/src/Actions/CopyAndRefactorDirectoryAction.php index 63a8833..20bcbd0 100644 --- a/src/Actions/CopyAndRefactorDirectoryAction.php +++ b/src/Actions/CopyAndRefactorDirectoryAction.php @@ -21,8 +21,8 @@ public function execute(string $sourcePath, string $targetPath, array $refactor foreach ($files as $file) { $this->copyAndRefactorFileAction->execute( - $sourcePath.'/'.$file->getFilename(), - $targetPath.'/'.$file->getFilename(), + beyond_os_aware_path($sourcePath.'/'.$file->getFilename()), + beyond_os_aware_path($targetPath.'/'.$file->getFilename()), $refactor, $force ); diff --git a/src/Actions/CreateDirectoryAction.php b/src/Actions/CreateDirectoryAction.php index 00e5a24..95bac23 100644 --- a/src/Actions/CreateDirectoryAction.php +++ b/src/Actions/CreateDirectoryAction.php @@ -19,6 +19,6 @@ public function execute(string|array $directory): void return; } - (new Filesystem())->ensureDirectoryExists(base_path()."/modules/$directory"); + (new Filesystem())->ensureDirectoryExists(beyond_os_aware_path(base_path()."/modules/$directory")); } } diff --git a/src/Commands/Abstracts/BaseCommand.php b/src/Commands/Abstracts/BaseCommand.php index e7a7c0a..118ddc8 100644 --- a/src/Commands/Abstracts/BaseCommand.php +++ b/src/Commands/Abstracts/BaseCommand.php @@ -97,6 +97,6 @@ public function handle(): void public function getTypeName(): string { - return Str::afterLast($this->getType(), '/'); + return Str::afterLast($this->getType(), DIRECTORY_SEPARATOR); } } diff --git a/src/Commands/MakeMigrationCommand.php b/src/Commands/MakeMigrationCommand.php index 6a3f95f..7791344 100644 --- a/src/Commands/MakeMigrationCommand.php +++ b/src/Commands/MakeMigrationCommand.php @@ -22,7 +22,7 @@ protected function getStub(): string public function getType(): string { - return 'Database/Migration'; + return beyond_os_aware_path('Database/Migration'); } public function getFileNameTemplate(): string diff --git a/src/Commands/MakeModelCommand.php b/src/Commands/MakeModelCommand.php index 2f6b00f..54ccc2d 100644 --- a/src/Commands/MakeModelCommand.php +++ b/src/Commands/MakeModelCommand.php @@ -35,7 +35,7 @@ public function setup(NameResolver $nameResolver): void beyond_copy_stub( 'migration.create.stub', - base_path()."/modules/$module/Infrastructure/Database/Migrations/$fileName.php", + beyond_os_aware_path(base_path()."/modules/$module/Infrastructure/Database/Migrations/$fileName.php"), [ '{{ table }}' => $tableName, ], @@ -48,7 +48,7 @@ public function setup(NameResolver $nameResolver): void beyond_copy_stub( 'factory.stub', - base_path()."/modules/$module/Infrastructure/factories/$fileName.php", + beyond_os_aware_path(base_path()."/modules/$module/Infrastructure/factories/$fileName.php"), [ '{{ namespace }}' => $namespace, '{{ model }}' => $fileName, diff --git a/src/Commands/MakeModuleCommand.php b/src/Commands/MakeModuleCommand.php index ac93fc0..f342ac3 100644 --- a/src/Commands/MakeModuleCommand.php +++ b/src/Commands/MakeModuleCommand.php @@ -29,7 +29,7 @@ public function handle(): void $full = (bool) $this->option('full'); $this->copyDirectoryAction->execute( - __DIR__.'/../../stubs/Module', + beyond_os_aware_path(__DIR__.'/../../stubs/Module'), beyond_modules_path($module), $force, ); @@ -44,17 +44,17 @@ public function handle(): void private function moveAndRefactorModuleFiles(string $module, bool $force = false): void { $files = [ - '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', + 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'), ]; foreach ($files as $from => $to) { $this->moveAndRefactorFileAction->execute( - beyond_modules_path("{$module}/{$from}"), - beyond_modules_path("{$module}/{$to}"), + beyond_modules_path("$module/$from"), + beyond_modules_path("$module/$to"), [ '{{ module }}' => $module, ], diff --git a/src/LaravelBeyondServiceProvider.php b/src/LaravelBeyondServiceProvider.php index 7f45e19..80001b6 100644 --- a/src/LaravelBeyondServiceProvider.php +++ b/src/LaravelBeyondServiceProvider.php @@ -22,7 +22,7 @@ public function beyondCommands(): array $exclude = []; $fs = new Filesystem(); - $files = $fs->files(__DIR__.'/Commands'); + $files = $fs->files(beyond_os_aware_path(__DIR__.'/Commands')); return array_map( fn ($file) => 'AkrilliA\\LaravelBeyond\\Commands\\'.$file->getBasename('.php'), diff --git a/src/NameResolver.php b/src/NameResolver.php index 32ed690..0f6d490 100644 --- a/src/NameResolver.php +++ b/src/NameResolver.php @@ -48,7 +48,7 @@ public function getPath(): string public function getCommandNameArgument(): string { - return $this->module.'.'.$this->className; + return $this->module . '.' . $this->className; } private function init(): void @@ -59,7 +59,7 @@ private function init(): void if (1 === $numParts) { $this->module = $this->command->choice( - 'On which module should we create your '.Str::studly($this->command->getTypeName()).'?', + 'On which module should we create your ' . Str::studly($this->command->getTypeName()) . '?', $modules, attempts: 2 ); @@ -67,7 +67,7 @@ private function init(): void $this->setDirectoryAndClassName($parts[0]); } elseif (2 === $numParts) { $module = Str::of($parts[0])->ucfirst()->value(); - if (! in_array($module, $modules, true)) { + if (!in_array($module, $modules, true)) { throw new ModuleDoesNotExistsException($module); } @@ -78,16 +78,18 @@ private function init(): void } $this->namespace = sprintf( - $this->command->getNamespaceTemplate().'%s', + $this->command->getNamespaceTemplate() . '%s', $this->module, Str::pluralStudly($this->command->getType()), - $this->directory ? '\\'.$this->directory : '', + $this->directory ? '\\' . $this->directory : '', ); - $this->path = sprintf( - '%s/'.$this->command->getFileNameTemplate(), - Str::lcfirst(Str::replace('\\', '/', $this->namespace)), - $this->className, + $this->path = beyond_os_aware_path( + sprintf( + '%s/' . $this->command->getFileNameTemplate(), + Str::lcfirst(Str::replace('\\', '/', $this->namespace)), + $this->className, + ) ); } @@ -97,6 +99,6 @@ private function setDirectoryAndClassName(string $name): void $this->className = array_pop($parts); - $this->directory = implode('/', $parts); + $this->directory = beyond_os_aware_path(implode('/', $parts)); } } diff --git a/src/helper.php b/src/helper.php index f211ada..c89bdd3 100644 --- a/src/helper.php +++ b/src/helper.php @@ -16,7 +16,7 @@ function beyond_path(): string if (! function_exists('beyond_modules_path')) { function beyond_modules_path(string $path = ''): string { - return base_path("modules/$path"); + return base_path(beyond_os_aware_path("modules/$path")); } } @@ -33,9 +33,9 @@ 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('stubs/beyond.'.$stub)) + $stub = file_exists($stubPath = base_path(beyond_os_aware_path('stubs/beyond.'.$stub))) ? $stubPath - : beyond_path().'/stubs/'.$stub; + : beyond_os_aware_path(beyond_path().'/stubs/'.$stub); $action = new CopyAndRefactorFileAction( new CopyFileAction(), @@ -63,7 +63,7 @@ function beyond_get_choices(string $path): array $directories = array_map( function ($directory) { - return last(explode('/', $directory)); + return last(explode(DIRECTORY_SEPARATOR, $directory)); }, $fs->directories($path) ); @@ -71,3 +71,13 @@ function ($directory) { return $directories; } } + +if (! function_exists('beyond_os_aware_path')) { + /** + * @return string + */ + function beyond_os_aware_path(string $path): string + { + return Str::of($path)->replace('/', DIRECTORY_SEPARATOR)->value(); + } +}