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

Add the ability to specify a custom directory for assets. #66

Merged
merged 11 commits into from
Jun 7, 2024
Merged
6 changes: 6 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
"homepage": "https://github.com/fxpio/foxy",
"type": "composer-plugin",
"license": "MIT",
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/terabytesoftw"
}
],
"require": {
"ext-ctype": "*",
"ext-mbstring": "*",
Expand Down
129 changes: 67 additions & 62 deletions composer.lock

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions resources/doc/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -369,3 +369,18 @@ If you do not deactivate any packages, you can use a simple array.
}
}
```

### Specify a custom directory for `package.json` file

You can define the custom directory for `package.json` file with the option `root-package-json-dir` [`string`, default: `null`].

**Example:**
```json
{
"config": {
"foxy": {
"root-package-json-dir": "module/theme"
}
}
}
```
9 changes: 9 additions & 0 deletions src/Asset/AbstractAssetManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,15 @@ public function run(): int
return 0;
}

$rootPackageDir = $this->config->get('root-package-json-dir');

if (is_string($rootPackageDir) && !empty($rootPackageDir)) {
if (!is_dir($rootPackageDir)) {
throw new RuntimeException(sprintf('The root package directory "%s" doesn\'t exist.', $rootPackageDir));
}
chdir($rootPackageDir);
}

$updatable = $this->isUpdatable();
$info = sprintf('<info>%s %s dependencies</info>', $updatable ? 'Updating' : 'Installing', $this->getName());
$this->io->write($info);
Expand Down
19 changes: 18 additions & 1 deletion src/Util/AssetUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,27 @@ public static function getPath(
): string|null {
$path = null;


if (self::isAsset($package, $configPackages)) {
$composerJsonPath = null;
$installPath = $installationManager->getInstallPath($package);

if ($installPath !== null) {
if (null !== $installPath) {
$composerJsonPath = $installPath . '/composer.json';
}

if (null !== $composerJsonPath && \file_exists($composerJsonPath)) {
/** @var array[] $composerJson */
$composerJson = \json_decode(\file_get_contents($composerJsonPath), true);
$rootPackageDir = $composerJson['config']['foxy']['root-package-json-dir'] ?? null;

if (null !== $installPath && \is_string($rootPackageDir)) {
$installPath .= '/' . $rootPackageDir;
}
}


if (null !== $installPath) {
$filename = $installPath . '/' . $assetManager->getPackageName();
$path = \file_exists($filename) ? \str_replace('\\', '/', \realpath($filename)) : null;
}
Expand Down
26 changes: 26 additions & 0 deletions tests/Asset/AssetManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,32 @@ public function testRunForInstallCommand(int $expectedRes, string $action): void
$this->assertSame('ASSET MANAGER OUTPUT', $this->executor->getLastOutput());
}

public function testSpecifyCustomDirectoryFromPackageJson(): void
{
$this->config = new Config(
[],
['run-asset-manager' => true, 'root-package-json-dir' => $this->cwd],
);
$this->manager = $this->getManager();

$this->assertSame($this->cwd, $this->config->get('root-package-json-dir'));
$this->assertSame(0, $this->getManager()->run());
}

public function testSpecifyCustomDirectoryFromPackageJsonException(): void
{
$this->expectException(\Foxy\Exception\RuntimeException::class);
$this->expectExceptionMessage('The root package directory "path/to/invalid" doesn\'t exist.');

$this->config = new Config(
[],
['run-asset-manager' => true, 'root-package-json-dir' => 'path/to/invalid'],
);
$this->manager = $this->getManager();

$this->assertSame(0, $this->getManager()->run());
}

abstract protected function getManager(): AssetManagerInterface;

abstract protected function getValidName(): string;
Expand Down
1 change: 1 addition & 0 deletions tests/Fixtures/package/global/composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"config": {
"foxy": {
"root-package-json-dir": "theme",
"global-composer-foo": 70,
"global-composer-bar": 70
}
Expand Down
2 changes: 2 additions & 0 deletions tests/Fixtures/package/global/theme/foo/bar/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
28 changes: 28 additions & 0 deletions tests/Util/AssetUtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Composer\Package\PackageInterface;
use Composer\Semver\Constraint\Constraint;
use Foxy\Asset\AbstractAssetManager;
use Foxy\Asset\AssetManagerInterface;
use Foxy\Util\AssetUtil;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Component\Filesystem\Filesystem;
Expand Down Expand Up @@ -340,4 +341,31 @@ public function testFormatPackage(

$this->assertEquals($expected, $res);
}

public function testGetPathWithRootPackageDir(): void
{
$installationManager = $this->createMock(InstallationManager::class);
$installationManager
->expects($this->once())
->method('getInstallPath')
->willReturn('tests/Fixtures/package/global');

$assetManager = $this->createMock(AssetManagerInterface::class);
$assetManager->expects($this->once())->method('getPackageName')->willReturn('foo/bar');

$package = $this->createMock(PackageInterface::class);
$package->expects($this->once())->method('getName')->willReturn('foo/bar');
$package->expects($this->once())->method('getRequires')->willReturn([]);
$package->expects($this->once())->method('getDevRequires')->willReturn([]);

$configPackages = [
'/^foo\/bar$/' => true,
];

$expectedPath = 'tests/Fixtures/package/global/theme/foo/bar';

$res = AssetUtil::getPath($installationManager, $assetManager, $package, $configPackages);

$this->assertStringContainsString($expectedPath, $res);
}
}