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

Dependency patch resolution #547

Merged
merged 7 commits into from
Feb 14, 2024
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
Next Next commit
Draft of simplified dependency patch resolution
  • Loading branch information
cweagans committed Feb 14, 2024
commit 4c645c5f1848f0cc06cf22607be30b1c26e4a740
1 change: 1 addition & 0 deletions src/Capability/Resolver/CoreResolverProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public function getResolvers(): array
return [
new RootComposer($this->composer, $this->io, $this->plugin),
new PatchesFile($this->composer, $this->io, $this->plugin),
new Dependencies($this->composer, $this->io, $this->plugin),
];
}
}
6 changes: 5 additions & 1 deletion src/Plugin/Patches.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,11 @@ public function activate(Composer $composer, IOInterface $io): void
'patches-file' => [
'type' => 'string',
'default' => 'patches.json',
]
],
"ignore-dependency-patches" => [
'type' => 'list',
'default' => [],
],
];
$this->configure($this->composer->getPackage()->getExtra(), 'composer-patches');
}
Expand Down
54 changes: 54 additions & 0 deletions src/Resolver/Dependencies.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

/**
* @file
* Contains \cweagans\Composer\Resolvers\Dependencies.
*/

namespace cweagans\Composer\Resolver;

use cweagans\Composer\Patch;
use cweagans\Composer\PatchCollection;

class Dependencies extends ResolverBase
{
/**
* {@inheritDoc}
*/
public function resolve(PatchCollection $collection): void
{
$locker = $this->composer->getLocker();
if (!$locker->isLocked()) {
$this->io->write(' - <info>Composer lock file does not exist.</info>');
$this->io->write(' - <info>Patches defined in dependencies will not be resolved.</info>');
return;
}

$this->io->write(' - <info>Resolving patches from dependencies.</info>');

$ignored_dependencies = $this->plugin->getConfig('ignore-dependency-patches');

$lockdata = $locker->getLockData();
foreach ($lockdata['packages'] as $p) {
// If we're supposed to skip gathering patches from a dependency, do that.
if (in_array($p['name'], $ignored_dependencies)) {
continue;
}

// Find patches in the composer.json for dependencies.
if (!isset($p['extra']) || !isset($p['extra']['patches'])) {
continue;
}
foreach ($this->findPatchesInJson($p['extra']['patches']) as $package => $patches) {
foreach ($patches as $patch) {
$patch->extra['provenance'] = "dependency:" . $package;

/** @var Patch $patch */
$collection->addPatch($patch);
}
}

// TODO: Also find patches in a configured patches.json for the dependency.
}
}
}
1 change: 1 addition & 0 deletions src/Resolver/PatchesFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function resolve(PatchCollection $collection): void
foreach ($this->findPatchesInJson($patches_file) as $package => $patches) {
foreach ($patches as $patch) {
/** @var Patch $patch */
$patch->extra['provenance'] = "patches-file:" . $patches_file;
$collection->addPatch($patch);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/Resolver/RootComposer.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function resolve(PatchCollection $collection): void
foreach ($this->findPatchesInJson($extra['patches']) as $package => $patches) {
foreach ($patches as $patch) {
/** @var Patch $patch */
$patch->extra['provenance'] = "root";
$collection->addPatch($patch);
}
}
Expand Down
23 changes: 10 additions & 13 deletions tests/_data/dep-test-package/composer.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
{
"name": "cweagans/dep-test-package",
"description": "Project for use in cweagans/composer-patches acceptance tests.",
"type": "project",
"license": "BSD-2-Clause",
"require": {
"drupal/drupal": "8.2.0"
},
"extra": {
"patches": {
"drupal/drupal": {
"Add a startup config for the PHP web server": "https://www.drupal.org/files/issues/add_a_startup-1543858-58.patch"
}
}
"name": "cweagans/dep-test-package",
"description": "Project for use in cweagans/composer-patches acceptance tests.",
"type": "project",
"license": "BSD-3-Clause",
"extra": {
"patches": {
"cweagans/composer-patches-testrepo": {
"Add a file": "https://patch-diff.githubusercontent.com/raw/cweagans/composer-patches-testrepo/pull/1.patch"
}
}
}
}
26 changes: 26 additions & 0 deletions tests/_data/fixtures/dependency-patches/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "cweagans/composer-patches-test-project",
"description": "Project for use in cweagans/composer-patches acceptance tests.",
"type": "project",
"license": "BSD-3-Clause",
"repositories": [
{
"type": "path",
"url": "../../../../"
},
{
"type": "path",
"url": "../../dep-test-package"
}
],
"require": {
"cweagans/composer-patches": "*@dev",
"cweagans/composer-patches-testrepo": "~1.0",
"cweagans/dep-test-package": "*@dev"
},
"config": {
"allow-plugins": {
"cweagans/composer-patches": true
}
}
}