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 plugin option "package-types" #152

Merged
merged 2 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

## 1.3.2 under development

- no changes in this release.
- Enh #152: Add plugin option "package-types" that define package types for process, by default "library" and
"composer-plugin" (@vjik)

## 1.3.1 November 17, 2023

Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,19 @@ This can be useful when developing. If the config package is a dependency of you
and you do not need to create a merge plan file when developing your package.
For example, this is implemented in [yiisoft/yii-runner](https://github.com/yiisoft/yii-runner).

### `package-types`

The `package-types` option define package types for process by composer plugin. By default, it is "library" and
"composer-plugin". You can add custom types:

```json
"extra": {
"config-plugin-options": {
"package-types": ["library", "my-extension"]
vjik marked this conversation as resolved.
Show resolved Hide resolved
}
}
```

## Environments

The plugin supports creating additional environments added to the base configuration. This allows you to create
Expand Down
4 changes: 2 additions & 2 deletions src/Composer/PackagesListBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ final class PackagesListBuilder
{
public function __construct(
private Composer $composer,
private array $packageTypes,
) {
}

Expand Down Expand Up @@ -119,8 +120,7 @@ private function getAllPackages(): array
continue;
}

$type = $package->getType();
if ($type !== 'library' && $type !== 'composer-plugin') {
if (!in_array($package->getType(), $this->packageTypes)) {
continue;
}

Expand Down
5 changes: 4 additions & 1 deletion src/Composer/ProcessHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ public function __construct(Composer $composer)
$this->composer = $composer;
$this->rootPackageOptions = new Options($this->rootPackageExtra);
$this->paths = new ConfigPaths($rootPath, $this->rootPackageOptions->sourceDirectory());
$this->packages = (new PackagesListBuilder($this->composer))->build();
$this->packages = (new PackagesListBuilder(
$this->composer,
$this->rootPackageOptions->packageTypes()
))->build();
}

/**
Expand Down
11 changes: 11 additions & 0 deletions src/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ final class Options
public const DEFAULT_ENVIRONMENT = '/';
public const ROOT_PACKAGE_NAME = '/';
public const VENDOR_OVERRIDE_PACKAGE_NAME = '//';
public const DEFAULT_PACKAGE_TYPES = ['library', 'composer-plugin'];

private string $mergePlanFile = self::DEFAULT_MERGE_PLAN_FILE;
private bool $buildMergePlan = true;
private array $vendorOverrideLayerPackages = [];
private string $sourceDirectory = self::DEFAULT_CONFIG_DIRECTORY;
private array $packageTypes = self::DEFAULT_PACKAGE_TYPES;

public function __construct(array $extra)
{
Expand All @@ -48,6 +50,10 @@ public function __construct(array $extra)
if (isset($options['source-directory'])) {
$this->sourceDirectory = $this->normalizePath((string) $options['source-directory']);
}

if (isset($options['package-types'])) {
$this->packageTypes = (array) $options['package-types'];
}
}

public static function containsWildcard(string $file): bool
Expand Down Expand Up @@ -85,6 +91,11 @@ public function sourceDirectory(): string
return $this->sourceDirectory;
}

public function packageTypes(): array
{
return $this->packageTypes;
}

private function normalizePath(string $value): string
{
return trim(str_replace('\\', '/', $value), '/');
Expand Down
85 changes: 85 additions & 0 deletions tests/Composer/PackageFilesProcessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,4 +254,89 @@ public function testProcessAllPackages(): void
->absolutePath(),
);
}

public function testProcessCustomPackageTypes(): void
{
$process = new PackageFilesProcess(
$this->createComposerMock(packateTypes: ['custom-type', 'library']),
['test/a', 'test/custom-type']
);

$this->assertCount(3, $process->files());

$this->assertSame('params.php', $process
->files()[0]
->filename());
$this->assertSame('params.php', $process
->files()[0]
->relativePath());
$this->assertSameIgnoringSlash(
$this->getSourcePath('a/params.php'),
$process
->files()[0]
->absolutePath(),
);

$this->assertSame('web.php', $process
->files()[1]
->filename());
$this->assertSame('web.php', $process
->files()[1]
->relativePath());
$this->assertSameIgnoringSlash(
$this->getSourcePath('a/web.php'),
$process
->files()[1]
->absolutePath(),
);

$this->assertSame('params.php', $process
->files()[2]
->filename());
$this->assertSame('params.php', $process
->files()[2]
->relativePath());
$this->assertSameIgnoringSlash(
$this->getSourcePath('custom-type/params.php'),
$process
->files()[2]
->absolutePath(),
);
}

public function testProcessOnlyLibraries(): void
{
$process = new PackageFilesProcess(
$this->createComposerMock(packateTypes: ['library']),
['test/a', 'test/custom-type']
);

$this->assertCount(2, $process->files());

$this->assertSame('params.php', $process
->files()[0]
->filename());
$this->assertSame('params.php', $process
->files()[0]
->relativePath());
$this->assertSameIgnoringSlash(
$this->getSourcePath('a/params.php'),
$process
->files()[0]
->absolutePath(),
);

$this->assertSame('web.php', $process
->files()[1]
->filename());
$this->assertSame('web.php', $process
->files()[1]
->relativePath());
$this->assertSameIgnoringSlash(
$this->getSourcePath('a/web.php'),
$process
->files()[1]
->absolutePath(),
);
}
}
3 changes: 2 additions & 1 deletion tests/Composer/PackagesListBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
namespace Yiisoft\Config\Tests\Composer;

use Yiisoft\Config\Composer\PackagesListBuilder;
use Yiisoft\Config\Options;

final class PackagesListBuilderTest extends TestCase
{
public function testBuild(): void
{
$packages = (new PackagesListBuilder($this->createComposerMock()))->build();
$packages = (new PackagesListBuilder($this->createComposerMock(), Options::DEFAULT_PACKAGE_TYPES))->build();

$this->assertCount(7, $packages);
$this->assertSame('test/a', $packages['test/a']->getPrettyName());
Expand Down
10 changes: 10 additions & 0 deletions tests/Composer/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ protected function createComposerMock(
bool $buildMergePlan = true,
string $extraConfigFile = null,
?string $mergePlanFile = null,
?array $packateTypes = null,
) {
$rootPath = $this->tempDirectory;
$sourcePath = $this->sourceDirectory;
Expand All @@ -196,6 +197,7 @@ protected function createComposerMock(
'vendor-override-layer' => $vendorOverridePackage ?? 'test/over',
'build-merge-plan' => $buildMergePlan,
'merge-plan-file' => $mergePlanFile,
'package-types' => $packateTypes,
],
'config-plugin' => [
'empty' => [],
Expand Down Expand Up @@ -229,6 +231,11 @@ protected function createComposerMock(
'test/ba' => new Link("$sourcePath/ba", "$targetPath/test/ba", new Constraint('>=', '1.0.0')),
'test/c' => new Link("$sourcePath/c", "$targetPath/test/c", new Constraint('>=', '1.0.0')),
'test/custom-source' => new Link("$sourcePath/custom-source", "$targetPath/test/custom-source", new Constraint('>=', '1.0.0')),
'test/custom-type' => new Link(
"$sourcePath/custom-type",
"$targetPath/test/custom-type",
new Constraint('>=', '1.0.0')
),
'test/over' => new Link("$sourcePath/over", "$targetPath/test/over", new Constraint('>=', '1.0.0')),
'test/metapack' => new Link("$sourcePath/metapack", "$targetPath/test/metapack", new Constraint('>=', '1.0.0')),
'test/empty-group' => new Link(
Expand All @@ -248,11 +255,14 @@ protected function createComposerMock(

$metapackage = new CompletePackage('test/metapack', '1.0.0', '1.0.0');
$metapackage->setType('metapackage');
$customTypePacakge = new CompletePackage('test/custom-type', '1.0.0', '1.0.0');
$customTypePacakge->setType('custom-type');
$packages = [
new CompletePackage('test/a', '1.0.0', '1.0.0'),
new CompletePackage('test/ba', '1.0.0', '1.0.0'),
new CompletePackage('test/c', '1.0.0', '1.0.0'),
new CompletePackage('test/custom-source', '1.0.0', '1.0.0'),
$customTypePacakge,
new CompletePackage('test/d-dev-c', '1.0.0', '1.0.0'),
new CompletePackage('test/over', '1.0.0', '1.0.0'),
new Package('test/e', '1.0.0', '1.0.0'),
Expand Down
10 changes: 10 additions & 0 deletions tests/TestAsset/packages/custom-type/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "test/custom-type",
"version": "1.0.0",
"type": "custom-type",
"extra": {
"config-plugin": {
"params": "params.php"
}
}
}
5 changes: 5 additions & 0 deletions tests/TestAsset/packages/custom-type/params.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

declare(strict_types=1);

return [];
Loading