-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit c21d484
Showing
14 changed files
with
508 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.idea/ | ||
vendor/ | ||
composer.lock | ||
.phpunit.result.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2023 Wieni | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# Composer Plugin Patchsets | ||
|
||
## Introduction | ||
|
||
The `wieni/composer-plugin-patchsets` is a PHP Composer plugin designed to address the complexities in managing and applying patches within Drupal projects. | ||
This plugin aims to centralize patch management, enabling a consistent approach to applying patches across multiple projects. | ||
|
||
## Problem Statement | ||
|
||
Current practices utilizing `cweagans/composer-patches` for patch management in projects have introduced several challenges: | ||
|
||
- Lack of uniformity in patch application across different projects leads to inconsistencies in behaviors. | ||
- Difficulty in identifying and applying the same patches when upgrading versions due to variations in patch creation among different projects. | ||
|
||
## Objective | ||
|
||
The primary goal of the `wieni/composer-plugin-patchsets` is to enable dependency patch resolution. A feature thas has [been deprecated](https://www.cweagans.net/2023/07/dependency-patch-resolution/) in `cweagans/composer-patches`. | ||
|
||
## Functionality | ||
|
||
The plugin leverages the new plugin capabilities released by `cweagans/composer-patches`, allowing it to add patches by registering [a custom Resolver](https://docs.cweagans.net/composer-patches/api/capabilities/). | ||
|
||
## Usage | ||
|
||
- Create a repository (eg `your-org/drupal-patches`) that contains a `composer.json` with patches defined [as documented by `cweagans/composer-patches`](https://docs.cweagans.net/composer-patches/usage/defining-patches/). | ||
- Require the `wieni/composer-plugin-patchsets` plugin and your new patch repository (`your-org/drupal-patches`) in your Drupal project. | ||
- Define the patch repository (`your-org/drupal-patches`) as a patch set in your `composer.json`: | ||
|
||
```json | ||
{ | ||
"extra": { | ||
"patchRepositories": [ | ||
"your-org/drupal-patches" | ||
] | ||
} | ||
} | ||
``` | ||
|
||
This configuration instructs `wieni/composer-plugin-patchsets` to apply the patches defined in the `your-org/drupal-patches` repository. | ||
|
||
## Advanced Configuration | ||
|
||
The plugin supports an advanced configuration allowing the exclusion of specific packages and patches. | ||
This next example demonstrates the exclusion of the "`#2907810`" patch for `drupal/core` and all patches for `drupal/field_group`. | ||
|
||
```json | ||
{ | ||
"extra": { | ||
"patchRepositories": [ | ||
{ | ||
"name": "your-org/drupal-patches", | ||
"excludePatches": { | ||
"drupal/core": [ | ||
"#2907810" | ||
], | ||
"drupal/field_group": ["*"] | ||
} | ||
} | ||
] | ||
} | ||
} | ||
``` | ||
|
||
## Contributing | ||
|
||
Contributions, suggestions, and issue reports are welcome. Feel free to fork this repository and submit pull requests. | ||
|
||
## License | ||
|
||
This project is licensed under the MIT License. | ||
|
||
## Acknowledgments | ||
|
||
- Acknowledgment of `cweagans/composer-patches` for its pioneering efforts in patch management within Composer. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"name": "wieni/composer-plugin-patchsets", | ||
"type": "composer-plugin", | ||
"license": "MIT", | ||
"require": { | ||
"composer-plugin-api": "^2.0", | ||
"cweagans/composer-patches": "^2.0.0-beta1" | ||
}, | ||
"require-dev": { | ||
"composer/composer": "^2.0", | ||
"phpunit/phpunit": "^10" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Wieni\\ComposerPatchSet\\": "src/" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"Tests\\": "tests/" | ||
} | ||
}, | ||
"extra": { | ||
"class": "Wieni\\ComposerPatchSet\\Composer\\Plugin\\PatchSet" | ||
}, | ||
"config": { | ||
"allow-plugins": { | ||
"cweagans/composer-patches": true | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd" | ||
bootstrap="vendor/autoload.php" | ||
colors="true" | ||
> | ||
<testsuites> | ||
<testsuite name="Unit"> | ||
<directory>tests/Unit</directory> | ||
</testsuite> | ||
</testsuites> | ||
<source> | ||
<include> | ||
<directory>src</directory> | ||
</include> | ||
</source> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace Wieni\ComposerPatchSet\Composer\Plugin; | ||
|
||
use Composer\Composer; | ||
use Composer\IO\IOInterface; | ||
use Composer\Plugin\Capable; | ||
use Composer\Plugin\PluginInterface; | ||
use cweagans\Composer\Capability\Resolver\ResolverProvider as ResolverProviderInterface; | ||
use cweagans\Composer\Capability\Patcher\PatcherProvider as PatcherProviderInterface; | ||
use Wieni\ComposerPatchSet\Patcher\PatcherProvider; | ||
use Wieni\ComposerPatchSet\Resolver\ResolverProvider; | ||
|
||
class PatchSet implements PluginInterface, Capable | ||
{ | ||
|
||
public function getCapabilities(): array | ||
{ | ||
return [ | ||
ResolverProviderInterface::class => ResolverProvider::class, | ||
PatcherProviderInterface::class => PatcherProvider::class, | ||
]; | ||
} | ||
|
||
public function activate(Composer $composer, IOInterface $io) | ||
{ | ||
} | ||
|
||
public function deactivate(Composer $composer, IOInterface $io) | ||
{ | ||
} | ||
|
||
public function uninstall(Composer $composer, IOInterface $io) | ||
{ | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace Wieni\ComposerPatchSet\PatchRepository; | ||
|
||
use cweagans\Composer\Patch; | ||
|
||
class ExcludedPatch | ||
{ | ||
|
||
public function __construct( | ||
public readonly string $package, | ||
public readonly string $url, | ||
) | ||
{ | ||
} | ||
|
||
public function matches(Patch $patch): bool | ||
{ | ||
return $patch->package === $this->package | ||
&& $this->urlOrDescriptionMatches($patch); | ||
} | ||
|
||
public function urlOrDescriptionMatches(Patch $patch): bool | ||
{ | ||
if ($this->url === '*') { | ||
return true; | ||
} | ||
|
||
return ( | ||
$patch->url === $this->url | ||
|| $patch->description === $this->url | ||
); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
<?php | ||
|
||
namespace Wieni\ComposerPatchSet\PatchRepository; | ||
|
||
use Composer\Installer\InstallationManager; | ||
use Composer\IO\IOInterface; | ||
use Composer\Package\PackageInterface; | ||
use Composer\Package\RootPackageInterface; | ||
use Composer\Repository\InstalledRepositoryInterface; | ||
use cweagans\Composer\Patch; | ||
use cweagans\Composer\Resolver\ResolverBase; | ||
|
||
class Loader | ||
{ | ||
|
||
public function __construct( | ||
private readonly string $projectRootDirectory, | ||
private readonly RootPackageInterface $rootPackage, | ||
private readonly InstalledRepositoryInterface $installedRepository, | ||
private readonly InstallationManager $installationManager, | ||
private readonly ResolverBase $resolver, | ||
private readonly IOInterface $io, | ||
) | ||
{ | ||
} | ||
|
||
public function loadPatches(PatchRepository $patchRepository): array | ||
{ | ||
$package = $this->installedRepository->findPackage($patchRepository->name, '*'); | ||
if (!$package) { | ||
throw new \Exception(sprintf('Package %s not found', $patchRepository->name)); | ||
} | ||
|
||
$patches = []; | ||
foreach ($this->getPatchesFromPatchRepository($package) as $patch) { | ||
if ($patchRepository->isExcluded($patch)) { | ||
$this->io->write(sprintf( | ||
' - <info>Excluding patch %s: %s</info>', | ||
$patch->package, | ||
$patch->description, | ||
)); | ||
continue; | ||
} | ||
|
||
$patches[] = $patch; | ||
} | ||
|
||
return $patches; | ||
} | ||
|
||
public function findPatchRepositories(): array | ||
{ | ||
$patchRepositories = []; | ||
foreach ($this->rootPackage->getExtra()['patchRepositories'] ?? [] as $patchRepositoryJson) { | ||
$patchRepositories[] = $this->createPatchRepositoryFromJson($patchRepositoryJson); | ||
} | ||
|
||
return $patchRepositories; | ||
} | ||
|
||
private function createPatchRepositoryFromJson(string|array $patchRepositoryJson): PatchRepository | ||
{ | ||
if (is_string($patchRepositoryJson)) { | ||
$patchRepositoryJson = [ | ||
'name' => $patchRepositoryJson, | ||
'excludedPatches' => [], | ||
]; | ||
} | ||
|
||
return new PatchRepository( | ||
$patchRepositoryJson['name'], | ||
$this->createExcludedPatchesFromJson($patchRepositoryJson), | ||
); | ||
} | ||
|
||
private function createExcludedPatchesFromJson(array $patchRepositoryJson): array | ||
{ | ||
$excludedPatches = []; | ||
foreach ($patchRepositoryJson['excludedPatches'] ?? [] as $package => $urls) { | ||
foreach ($urls as $url) { | ||
$excludedPatches[] = new ExcludedPatch($package, $url); | ||
} | ||
} | ||
|
||
return $excludedPatches; | ||
} | ||
|
||
/** @return Patch[] */ | ||
private function getPatchesFromPatchRepository(PackageInterface $package): array | ||
{ | ||
$packagePatches = $this->resolver->findPatchesInJson($package->getExtra()['patches'] ?? []); | ||
|
||
$allPatches = []; | ||
foreach ($packagePatches as $patches) { | ||
foreach ($patches as $patch) { | ||
$allPatches[] = $patch; | ||
} | ||
} | ||
|
||
// In case Patch url is a file path, we need to resolve it to a path | ||
// relative to the root composer.json. | ||
foreach ($allPatches as $patch) { | ||
if (!str_starts_with($patch->url, 'http')) { | ||
// /home/user/project/vendor/your-org/drupal-patches/patches/core/case_insensitive_langcode.patch | ||
$newUrl = $this->installationManager->getInstaller($package->getType())->getInstallPath($package) . '/' . trim($patch->url, '/'); | ||
// /vendor/your-org/drupal-patches/patches/core/case_insensitive_langcode.patch | ||
$newUrl = str_replace(trim($this->projectRootDirectory, '/') . '/', '', $newUrl); | ||
// vendor/your-org/drupal-patches/patches/core/case_insensitive_langcode.patch | ||
$newUrl = ltrim($newUrl, '/'); | ||
|
||
$this->io->write(sprintf( | ||
' - <info>Renaming patch path %s to %s</info>', | ||
$patch->url, | ||
$newUrl, | ||
)); | ||
$patch->url = $newUrl; | ||
} | ||
} | ||
|
||
// sort the patches by package name. | ||
usort($allPatches, function (Patch $a, Patch $b) { | ||
return strcmp($a->package, $b->package); | ||
}); | ||
return $allPatches; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace Wieni\ComposerPatchSet\PatchRepository; | ||
|
||
use cweagans\Composer\Patch; | ||
|
||
class PatchRepository | ||
{ | ||
|
||
public function __construct( | ||
public readonly string $name, | ||
/** @var ExcludedPatch[] */ | ||
public readonly array $excludedPatches = [], | ||
) | ||
{ | ||
} | ||
|
||
public function isExcluded(Patch $patch): bool | ||
{ | ||
foreach ($this->excludedPatches as $excludedPatch) { | ||
if ($excludedPatch->matches($patch)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
} |
Oops, something went wrong.