Skip to content

Commit b6deb40

Browse files
committed
Allow to define how many path components to strip per patch
1 parent 91c2326 commit b6deb40

File tree

8 files changed

+103
-13
lines changed

8 files changed

+103
-13
lines changed

README.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ This plugin can automatically apply patches to any dependency of your project.
77

88
One of the most distinguishing features is that it can apply patches from special composer packages of type `patchset`.
99
This is quite convenient as you can store all your patches in one repository and apply them automatically on all
10-
systems including developer's machines in a very predictable way.
10+
systems including developer's machines in a very predictable way. Also you can easily distribute your patchset to the
11+
whole community via packagist.
1112

1213
It's (kind-of) an alternative to two other great plugins (differences will become apparent once you read __Features__):
1314

@@ -73,6 +74,16 @@ the actions necessary to get there.
7374
purpose. I can hardly imagine a legit use case when it would be desirable that installing package X will automatically
7475
patch some other package Y in your project without explicitly being advertised as a patchset.
7576

77+
## Usage
78+
79+
### Create *patchset* composer package
80+
81+
### Define patches in your root package
82+
83+
### Options
84+
85+
#### Strip defined number of components when applying patch (`-pX`)
86+
7687
## Running tests
7788

7889
Just start `vendor/bin/phpunit`.
@@ -83,7 +94,3 @@ vendor/bin/phpunit --debug
8394

8495
It's nice to also add the `--testdox` switch then.
8596

86-
87-
### Notes to myself
88-
89-
- Tests for skipping package aliases (BTW Maybe use localrepo->getCanonicalPackages)

src/Patch.php

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,25 +43,33 @@ class Patch
4343
*/
4444
private $filename;
4545

46+
/**
47+
* @var int
48+
*/
49+
private $stripPathComponents = 1;
50+
4651
/**
4752
* @param $sourcePackage
4853
* @param string $targetPackage
4954
* @param string $versionConstraint
5055
* @param string $filename
5156
* @param string $description
57+
* @param int $stripPathComponents
5258
*/
5359
public function __construct(
5460
$sourcePackage,
5561
$targetPackage,
5662
$versionConstraint,
5763
$filename,
58-
$description
64+
$description,
65+
$stripPathComponents = 1
5966
) {
6067
$this->sourcePackage = $sourcePackage;
6168
$this->targetPackage = $targetPackage;
6269
$this->versionConstraint = $versionConstraint;
6370
$this->filename = $filename;
6471
$this->description = $description;
72+
$this->stripPathComponents = $stripPathComponents;
6573
}
6674

6775
/**
@@ -73,16 +81,18 @@ public function __construct(
7381
public static function createFromConfig($sourcePackage, $targetPackage, array $config)
7482
{
7583
$config = array_merge([
76-
'version_constraint' => '*',
84+
'version-constraint' => '*',
7785
'description' => null,
86+
'strip-path-components' => 1
7887
], $config);
7988

8089
return new static(
8190
$sourcePackage,
8291
$targetPackage,
83-
$config['version_constraint'],
92+
$config['version-constraint'],
8493
$config['filename'],
85-
$config['description']
94+
$config['description'],
95+
$config['strip-path-components']
8696
);
8797
}
8898

@@ -94,7 +104,8 @@ public static function createFromArray(array $data)
94104
$data['target_package'],
95105
$data['version_constraint'],
96106
$data['filename'],
97-
$data['description']
107+
$data['description'],
108+
$data['strip_path_components']
98109
);
99110
}
100111

@@ -155,6 +166,15 @@ public function getDescription()
155166
return $this->description;
156167
}
157168

169+
/**
170+
* @return int
171+
*/
172+
public function getStripPathComponents()
173+
{
174+
return $this->stripPathComponents;
175+
}
176+
177+
158178
public function toArray()
159179
{
160180
return [
@@ -163,6 +183,7 @@ public function toArray()
163183
'version_constraint' => $this->versionConstraint,
164184
'filename' => $this->filename,
165185
'description' => $this->description,
186+
'strip_path_components' => $this->stripPathComponents
166187
];
167188
}
168189
}

src/PatchApplicator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public function applyPatch(Patch $patch, PackageInterface $sourcePackage, Packag
106106
$targetDirectory = $this->pathResolver->getPackageInstallPath($targetPackage);
107107
$patchFilename = $this->pathResolver->getPatchSourceFilePath($sourcePackage, $patch);
108108

109-
if (!$this->executePatchCommand($targetDirectory, $patchFilename, 1)) {
109+
if (!$this->executePatchCommand($targetDirectory, $patchFilename, $patch->getStripPathComponents())) {
110110
throw new \RuntimeException('Could not apply patch: ' . $this->cmdErr);
111111
}
112112

tests/Functional/CoreFeatureTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,4 +226,23 @@ public function testRootCanPatchItself()
226226
$installRun = $project->runComposerCommand('install');
227227
$this->assertThatComposerRunHasAppliedPatches($installRun, self::ROOT_PACKAGE_APPLICATIONS);
228228
}
229+
230+
public function testArbitraryNumberOfPathComponentsCanBeStripped()
231+
{
232+
$project = $this->getSandbox()->createProjectSandBox('test/project-template', 'dev-master', [
233+
'require' => [
234+
'test/patchset' => '2.0',
235+
'test/package-a' => 'dev-master',
236+
]
237+
]);
238+
239+
$installRun = $project->runComposerCommand('install', '-vvv');
240+
241+
$this->assertThatComposerRunHasAppliedPatches($installRun,
242+
array_merge_recursive(
243+
self::PACKAGEA_PATCH1_APPLICATIONS,
244+
self::PACKAGEA_PATCH2_APPLICATIONS
245+
)
246+
);
247+
}
229248
}

tests/Functional/Fixtures/packages/patchset-extra/patches/package-a-patch-2.diff

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
diff --git a/src/test.php b/src/test.php
2-
index 6cb2e72..255f36b 100644
31
--- a/src/test.php
42
+++ b/src/test.php
53
@@ -1,6 +1,9 @@
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "test/patchset",
3+
"type": "patchset",
4+
"version": "2.0",
5+
"require": {
6+
"creativestyle/composer-plugin-patchset": "dev-master"
7+
},
8+
"extra": {
9+
"patchset": {
10+
"test/package-a": [
11+
{
12+
"description": "Patch in echo in the middle",
13+
"filename": "patches/package-a-patch-1-p0.diff",
14+
"strip-path-components": 0
15+
},
16+
{
17+
"description": "Patch in echo at the end",
18+
"filename": "patches/package-a-patch-2-p2.diff",
19+
"strip-path-components": 2
20+
}
21+
]
22+
}
23+
}
24+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
--- src/test.php
2+
+++ src/test.php
3+
@@ -1,5 +1,6 @@
4+
<?php
5+
6+
echo 'first-echo';
7+
+echo 'patched-in-echo';
8+
echo 'last-echo';
9+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--- test/package-a/src/test.php
2+
+++ test/package-a/src/test.php
3+
@@ -1,6 +1,9 @@
4+
<?php
5+
6+
+echo 'layered-patch-line-1';
7+
echo 'first-echo';
8+
echo 'patched-in-echo';
9+
+echo 'layered-patch-line-2';
10+
echo 'last-echo';
11+
+echo 'layered-patch-line-3';
12+

0 commit comments

Comments
 (0)