Skip to content

Commit

Permalink
[7.x] Add Orchestra\Testbench\transform_realpath_to_relative functi…
Browse files Browse the repository at this point in the history
…on (#276)

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

---------

Signed-off-by: Mior Muhammad Zaki <[email protected]>
  • Loading branch information
crynobone authored Dec 12, 2024
1 parent cdcc614 commit 86dbc7b
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 83 deletions.
35 changes: 8 additions & 27 deletions src/Foundation/Console/Actions/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,27 @@

namespace Orchestra\Testbench\Foundation\Console\Actions;

use function Orchestra\Testbench\package_path;
use function Orchestra\Testbench\transform_realpath_to_relative;

/**
* @api
*/
abstract class Action
{
/**
* Working path for the action.
*
* @var string|null
*/
public $workingPath;

/**
* Normalise file location.
*
* @param string $path
* @return string
*
* @deprecated
*
* @codeCoverageIgnore
*/
protected function pathLocation(string $path): string
{
$packagePath = package_path();

if (! \is_null($this->workingPath)) {
$path = str_replace(rtrim($this->workingPath, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR, '', $path);

$prefix = match ($this->workingPath) {
base_path() => '@laravel',
$packagePath => '.',
default => '@'
};

return implode('/', [$prefix, ltrim($path, '/')]);
}

if (str_starts_with($path, $packagePath)) {
return \sprintf('./%s', ltrim(str_replace($packagePath, '', $path), '/'));
}

return $path;
return transform_realpath_to_relative(
$path, property_exists($this, 'workingPath') ? $this->workingPath : null
);
}
}
12 changes: 6 additions & 6 deletions src/Foundation/Console/Actions/DeleteDirectories.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\LazyCollection;

use function Orchestra\Testbench\transform_realpath_to_relative;

/**
* @api
*/
Expand All @@ -21,10 +23,8 @@ class DeleteDirectories extends Action
public function __construct(
public Filesystem $filesystem,
public ?ComponentsFactory $components = null,
?string $workingPath = null
) {
$this->workingPath = $workingPath;
}
public ?string $workingPath = null
) {}

/**
* Handle the action.
Expand All @@ -38,7 +38,7 @@ public function handle(iterable $directories): void
->each(function ($directory) {
if (! $this->filesystem->isDirectory($directory)) {
$this->components?->twoColumnDetail(
\sprintf('Directory [%s] doesn\'t exists', $this->pathLocation($directory)),
\sprintf('Directory [%s] doesn\'t exists', transform_realpath_to_relative($directory, $this->workingPath)),
'<fg=yellow;options=bold>SKIPPED</>'
);

Expand All @@ -48,7 +48,7 @@ public function handle(iterable $directories): void
$this->filesystem->deleteDirectory($directory);

$this->components?->task(
\sprintf('Directory [%s] has been deleted', $this->pathLocation($directory))
\sprintf('Directory [%s] has been deleted', transform_realpath_to_relative($directory, $this->workingPath))
);
});
}
Expand Down
12 changes: 6 additions & 6 deletions src/Foundation/Console/Actions/DeleteFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\LazyCollection;

use function Orchestra\Testbench\transform_realpath_to_relative;

/**
* @api
*/
Expand All @@ -21,10 +23,8 @@ class DeleteFiles extends Action
public function __construct(
public Filesystem $filesystem,
public ?ComponentsFactory $components = null,
?string $workingPath = null
) {
$this->workingPath = $workingPath;
}
public ?string $workingPath = null
) {}

/**
* Handle the action.
Expand All @@ -39,7 +39,7 @@ public function handle(iterable $files): void
->each(function ($file) {
if (! $this->filesystem->exists($file)) {
$this->components?->twoColumnDetail(
\sprintf('File [%s] doesn\'t exists', $this->pathLocation($file)),
\sprintf('File [%s] doesn\'t exists', transform_realpath_to_relative($file, $this->workingPath)),
'<fg=yellow;options=bold>SKIPPED</>'
);

Expand All @@ -49,7 +49,7 @@ public function handle(iterable $files): void
$this->filesystem->delete($file);

$this->components?->task(
\sprintf('File [%s] has been deleted', $this->pathLocation($file))
\sprintf('File [%s] has been deleted', transform_realpath_to_relative($file, $this->workingPath))
);
});
}
Expand Down
11 changes: 5 additions & 6 deletions src/Foundation/Console/Actions/EnsureDirectoryExists.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Support\LazyCollection;

use function Orchestra\Testbench\join_paths;
use function Orchestra\Testbench\transform_realpath_to_relative;

/**
* @api
Expand All @@ -23,10 +24,8 @@ class EnsureDirectoryExists extends Action
public function __construct(
public Filesystem $filesystem,
public ?ComponentsFactory $components = null,
?string $workingPath = null
) {
$this->workingPath = $workingPath;
}
public ?string $workingPath = null
) {}

/**
* Handle the action.
Expand All @@ -40,7 +39,7 @@ public function handle(iterable $directories): void
->each(function ($directory) {
if ($this->filesystem->isDirectory($directory)) {
$this->components?->twoColumnDetail(
\sprintf('Directory [%s] already exists', $this->pathLocation($directory)),
\sprintf('Directory [%s] already exists', transform_realpath_to_relative($directory, $this->workingPath)),
'<fg=yellow;options=bold>SKIPPED</>'
);

Expand All @@ -50,7 +49,7 @@ public function handle(iterable $directories): void
$this->filesystem->ensureDirectoryExists($directory, 0755, true);
$this->filesystem->copy((string) realpath(join_paths(__DIR__, 'stubs', '.gitkeep')), join_paths($directory, '.gitkeep'));

$this->components?->task(\sprintf('Prepare [%s] directory', $this->pathLocation($directory)));
$this->components?->task(\sprintf('Prepare [%s] directory', transform_realpath_to_relative($directory, $this->workingPath)));
});
}
}
14 changes: 7 additions & 7 deletions src/Foundation/Console/Actions/GeneratesFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Illuminate\Console\View\Components\Factory as ComponentsFactory;
use Illuminate\Filesystem\Filesystem;

use function Orchestra\Testbench\transform_realpath_to_relative;

/**
* @api
*/
Expand All @@ -22,10 +24,8 @@ public function __construct(
public Filesystem $filesystem,
public ?ComponentsFactory $components = null,
public bool $force = false,
?string $workingPath = null
) {
$this->workingPath = $workingPath;
}
public ?string $workingPath = null
) {}

/**
* Handle the action.
Expand All @@ -42,7 +42,7 @@ public function handle($from, $to): void

if (! $this->filesystem->exists($from)) {
$this->components?->twoColumnDetail(
\sprintf('Source file [%s] doesn\'t exists', $this->pathLocation($from)),
\sprintf('Source file [%s] doesn\'t exists', transform_realpath_to_relative($from, $this->workingPath)),
'<fg=yellow;options=bold>SKIPPED</>'
);

Expand All @@ -51,7 +51,7 @@ public function handle($from, $to): void

if (! $this->force && $this->filesystem->exists($to)) {
$this->components?->twoColumnDetail(
\sprintf('File [%s] already exists', $this->pathLocation($to)),
\sprintf('File [%s] already exists', transform_realpath_to_relative($to, $this->workingPath)),
'<fg=yellow;options=bold>SKIPPED</>'
);

Expand All @@ -67,7 +67,7 @@ public function handle($from, $to): void
}

$this->components?->task(
\sprintf('File [%s] generated', $this->pathLocation($to))
\sprintf('File [%s] generated', transform_realpath_to_relative($to, $this->workingPath))
);
}
}
1 change: 0 additions & 1 deletion src/Foundation/Console/CreateSqliteDbCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public function handle(Filesystem $filesystem)
filesystem: $filesystem,
components: $this->components,
force: $force,
workingPath: $workingPath,
))->handle($from, $to);

return Command::SUCCESS;
Expand Down
1 change: 0 additions & 1 deletion src/Foundation/Console/DropSqliteDbCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public function handle(Filesystem $filesystem)
(new Actions\DeleteFiles(
filesystem: $filesystem,
components: $this->components,
workingPath: $workingPath,
))->handle(
match ($this->option('all')) {
true => [...$filesystem->glob(join_paths($databasePath, '*.sqlite'))],
Expand Down
32 changes: 7 additions & 25 deletions src/Foundation/Console/VendorPublishCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Illuminate\Foundation\Console\VendorPublishCommand as Command;

use function Orchestra\Testbench\package_path;
use function Orchestra\Testbench\transform_realpath_to_relative;

/**
* @codeCoverageIgnore
Expand All @@ -15,37 +15,19 @@ class VendorPublishCommand extends Command
#[\Override]
protected function status($from, $to, $type)
{
$laravelPath = base_path();
$packagePath = package_path();

$pathLocation = function ($path) use ($laravelPath, $packagePath) {
$path = (string) realpath($path);

$format = function ($path) use ($type) {
return match (true) {
str_starts_with($path, $laravelPath) => str_replace("{$laravelPath}/", '@laravel/', $path),
str_starts_with($path, $packagePath) => str_replace("{$packagePath}/", './', $path),
default => $path,
$type === 'directory' && is_link($path) => $path,
$this->files->exists($path) => $path,
default => (string) realpath($path),
};
};

$fromLocation = $pathLocation($from);
$toLocation = $pathLocation($to);

if (
$type === 'directory' &&
$fromLocation === $toLocation &&
is_link($to)
) {
$this->components->task('Synced directory');

return;
}

$this->components->task(\sprintf(
'Copying %s [%s] to [%s]',
$type,
$fromLocation,
$toLocation,
transform_realpath_to_relative($format($from)),
transform_realpath_to_relative($format($to)),
));
}
}
30 changes: 30 additions & 0 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,36 @@ function refresh_router_lookups(Router $router): void
$router->getRoutes()->refreshNameLookups();
}

/**
* Transform realpath to alias path.
*
* @api
*
* @param string $path
* @param string|null $workingPath
* @return string
*/
function transform_realpath_to_relative(string $path, ?string $workingPath = null, string $prefix = ''): string
{
$separator = DIRECTORY_SEPARATOR;

if (! \is_null($workingPath)) {
return str_replace(rtrim($workingPath, $separator).$separator, $prefix.$separator, $path);
}

$laravelPath = base_path();
$workbenchPath = workbench_path();
$packagePath = package_path();

return match (true) {
str_starts_with($path, $laravelPath) => str_replace($laravelPath.$separator, '@laravel'.$separator, $path),
str_starts_with($path, $workbenchPath) => str_replace($workbenchPath.$separator, '@workbench'.$separator, $path),
str_starts_with($path, $packagePath) => str_replace($packagePath.$separator, '.'.$separator, $path),
! empty($prefix) => implode($separator, [$prefix, ltrim($path, $separator)]),
default => $path,
};
}

/**
* Transform relative path.
*
Expand Down
43 changes: 43 additions & 0 deletions tests/Helpers/TransformRealpathToRelativeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Orchestra\Testbench\Tests\Helpers;

use Orchestra\Testbench\TestCase;

use function Orchestra\Testbench\default_skeleton_path;
use function Orchestra\Testbench\join_paths;
use function Orchestra\Testbench\package_path;
use function Orchestra\Testbench\transform_realpath_to_relative;

class TransformRealpathToRelativeTest extends TestCase
{
/** @test */
public function it_can_use_transform_realpath_to_relative()
{
$this->assertSame('Testbench.php', transform_realpath_to_relative('Testbench.php'));

$this->assertSame(
join_paths('.', 'src', 'TestCase.php'),
transform_realpath_to_relative(package_path('src', 'TestCase.php'))
);

$this->assertSame(
join_paths('@laravel', 'composer.json'),
transform_realpath_to_relative(default_skeleton_path('composer.json'))
);

$this->assertSame(
join_paths('@workbench', 'app', 'Providers', 'WorkbenchServiceProvider.php'),
transform_realpath_to_relative(package_path('workbench', 'app', 'Providers', 'WorkbenchServiceProvider.php'))
);
}

/** @test */
public function it_can_use_transform_realpath_to_relative_using_custom_working_path()
{
$this->assertSame(
join_paths('@tests', 'Helpers', 'TransformRealpathToRelativeTest.php'),
transform_realpath_to_relative(__FILE__, package_path('tests'), '@tests')
);
}
}
5 changes: 1 addition & 4 deletions tests/Helpers/TransformRelativePathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ class TransformRelativePathTest extends TestCase
/** @test */
public function it_can_use_transform_relative_path()
{
$this->assertSame(
realpath(__DIR__.DIRECTORY_SEPARATOR.'TransformRelativePathTest.php'),
transform_relative_path('./TransformRelativePathTest.php', realpath(__DIR__))
);
$this->assertSame(__FILE__, transform_relative_path('./TransformRelativePathTest.php', __DIR__));
}
}

0 comments on commit 86dbc7b

Please sign in to comment.