Skip to content

Commit e5df90e

Browse files
author
Michael Wikberg
authored
Composer 2.0 compatibility (#62)
* Update composer.json * Support both Composer versions 1 and 2 * Replace deprecated constants * Try to run on Travis with both Composer 1.x and 2.x versions * Try to run on Travis with both Composer 1.x and 2.x versions * Try to run on Travis with both Composer 1.x and 2.x versions * Add askForUpdate for composer 2.0
1 parent e97df9c commit e5df90e

File tree

3 files changed

+72
-19
lines changed

3 files changed

+72
-19
lines changed

.travis.yml

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
language: php
22

33
env:
4-
- CODECEPTION_VERSION: '^3.0'
5-
- CODECEPTION_VERSION: '^4.0'
4+
- CODECEPTION_VERSION='^3.0' COMPOSER=1
5+
- CODECEPTION_VERSION='^4.0' COMPOSER=1
6+
- CODECEPTION_VERSION='^4.0' COMPOSER=2
67

78
php:
89
- 5.6
@@ -12,8 +13,18 @@ php:
1213
- 7.3
1314
- 7.4
1415

16+
jobs:
17+
exclude:
18+
- php: 5.6
19+
env: CODECEPTION_VERSION='^4.0' COMPOSER=2
20+
- php: 7.0
21+
env: CODECEPTION_VERSION='^4.0' COMPOSER=2
22+
- php: 7.1
23+
env: CODECEPTION_VERSION='^4.0' COMPOSER=2
24+
1525
before_script:
1626
- export COMPOSER_MEMORY_LIMIT=-1
27+
- composer self-update --${COMPOSER}
1728
- composer require codeception/codeception:"$CODECEPTION_VERSION" --dev --prefer-source
1829
- if [ "^3.0" != "$CODECEPTION_VERSION" ]; then composer require codeception/module-filesystem codeception/module-cli codeception/module-asserts codeception/module-phpbrowser --dev; fi;
1930
- cp c3.php vendor/codeception/codeception/tests/data/claypit

Installer.php

Lines changed: 57 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
use Composer\DependencyResolver\Operation\UpdateOperation;
77
use Composer\EventDispatcher\EventSubscriberInterface;
88
use Composer\Installer\PackageEvent;
9+
use Composer\Installer\PackageEvents;
910
use Composer\IO\IOInterface;
1011
use Composer\Plugin\PluginInterface;
1112
use Composer\Script\Event;
1213
use Composer\Script\ScriptEvents;
14+
use Composer\Semver\Comparator;
1315

1416
class Installer implements PluginInterface, EventSubscriberInterface
1517
{
@@ -22,9 +24,20 @@ public function activate(Composer $composer, IOInterface $io)
2224
{
2325
$this->io = $io;
2426
}
25-
27+
28+
public function deactivate(Composer $composer, IOInterface $io)
29+
{
30+
}
31+
32+
public function uninstall(Composer $composer, IOInterface $io) {
33+
$this->deleteFile();
34+
}
35+
2636
protected function isOperationOnC3(PackageEvent $event)
2737
{
38+
if (static::composerV2()) {
39+
return true;
40+
}
2841
$name = '';
2942

3043
if ($event->getOperation() instanceof InstallOperation) {
@@ -40,34 +53,50 @@ protected function isOperationOnC3(PackageEvent $event)
4053

4154
public static function getSubscribedEvents()
4255
{
56+
if (static::composerV2()) {
57+
return [
58+
ScriptEvents::POST_INSTALL_CMD => [
59+
['copyC3V2', 0]
60+
],
61+
ScriptEvents::POST_UPDATE_CMD => [
62+
['askForUpdateV2', 0]
63+
],
64+
];
65+
}
4366
return [
44-
ScriptEvents::POST_PACKAGE_INSTALL => [
67+
PackageEvents::POST_PACKAGE_INSTALL => [
4568
['copyC3', 0]
4669
],
47-
ScriptEvents::POST_PACKAGE_UPDATE => [
70+
PackageEvents::POST_PACKAGE_UPDATE => [
4871
['askForUpdate', 0]
4972
],
50-
ScriptEvents::POST_PACKAGE_UNINSTALL => [
73+
PackageEvents::POST_PACKAGE_UNINSTALL => [
5174
['deleteC3', 0]
5275
]
5376
];
5477
}
5578

56-
public static function copyC3ToRoot(Event $event)
57-
{
58-
$event->getIO()->write("<warning>c3 is now a Composer Plugin and installs c3.php automatically.</warning>");
59-
$event->getIO()->write("<warning>Please remove current \"post-install-cmd\" and \"post-update-cmd\" hooks from your composer.json</warning>");
60-
}
61-
6279
public function copyC3(PackageEvent $event)
6380
{
6481
if (!$this->isOperationOnC3($event)) {
6582
return;
6683
}
84+
85+
$this->copyC3V2(null);
86+
}
87+
88+
public function copyC3V2(Event $event)
89+
{
6790
if ($this->c3NotChanged()) {
6891
$this->io->write("<comment>[codeception/c3]</comment> c3.php is already up-to-date");
6992
return;
7093
}
94+
if (file_exists(getcwd() . DIRECTORY_SEPARATOR . 'c3.php')) {
95+
$replace = $this->io->askConfirmation("<warning>c3.php has changed</warning> Do you want to replace c3.php with latest version?", false);
96+
if (!$replace) {
97+
return;
98+
}
99+
}
71100

72101
$this->io->write("<comment>[codeception/c3]</comment> Copying c3.php to the root of your project...");
73102
copy(__DIR__ . DIRECTORY_SEPARATOR . 'c3.php', getcwd() . DIRECTORY_SEPARATOR.'c3.php');
@@ -79,15 +108,17 @@ public function askForUpdate(PackageEvent $event)
79108
if (!$this->isOperationOnC3($event) || $this->c3NotChanged()) {
80109
return;
81110
}
82-
if (file_exists(getcwd() . DIRECTORY_SEPARATOR . 'c3.php')) {
83-
$replace = $this->io->askConfirmation("<warning>c3.php has changed</warning> Do you want to replace c3.php with latest version?", false);
84-
if (!$replace) {
85-
return;
86-
}
87-
}
88111
$this->copyC3($event);
89112
}
90113

114+
public function askForUpdateV2(Event $event)
115+
{
116+
if ($this->c3NotChanged()) {
117+
return;
118+
}
119+
$this->copyC3V2($event);
120+
}
121+
91122
private function c3NotChanged()
92123
{
93124
return file_exists(getcwd() . DIRECTORY_SEPARATOR . 'c3.php') &&
@@ -99,9 +130,19 @@ public function deleteC3(PackageEvent $event)
99130
if (!$this->isOperationOnC3($event)) {
100131
return;
101132
}
133+
$this->deleteFile();
134+
}
135+
136+
private function deleteFile() {
102137
if (file_exists(getcwd() . DIRECTORY_SEPARATOR . 'c3.php')) {
103138
$this->io->write("<comment>[codeception/c3]</comment> Deleting c3.php from the root of your project...");
104139
unlink(getcwd() . DIRECTORY_SEPARATOR . 'c3.php');
105140
}
106141
}
142+
143+
private static function composerV2()
144+
{
145+
return Comparator::greaterThanOrEqualTo(PluginInterface::PLUGIN_API_VERSION, '2.0.0');
146+
}
147+
107148
}

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
],
2121
"require": {
2222
"php": ">=5.4.0",
23-
"composer-plugin-api": "^1.0"
23+
"composer-plugin-api": "^1.0 || ^2.0",
24+
"composer/composer": "^1.0 || ^2.0"
2425
},
2526
"extra": {
2627
"class": "Codeception\\c3\\Installer"

0 commit comments

Comments
 (0)