Skip to content

Commit 2f7941b

Browse files
pfrenssenwebflo
authored andcommitted
Check the Composer version before downloading the dependencies (#170)
1 parent 6bc31af commit 2f7941b

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

composer.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@
4444
},
4545
"scripts": {
4646
"drupal-scaffold": "DrupalComposer\\DrupalScaffold\\Plugin::scaffold",
47+
"pre-install-cmd": [
48+
"DrupalProject\\composer\\ScriptHandler::checkComposerVersion"
49+
],
50+
"pre-update-cmd": [
51+
"DrupalProject\\composer\\ScriptHandler::checkComposerVersion"
52+
],
4753
"post-install-cmd": [
4854
"DrupalProject\\composer\\ScriptHandler::createRequiredFiles"
4955
],

scripts/composer/ScriptHandler.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace DrupalProject\composer;
99

1010
use Composer\Script\Event;
11+
use Composer\Semver\Comparator;
1112
use Symfony\Component\Filesystem\Filesystem;
1213

1314
class ScriptHandler {
@@ -57,4 +58,35 @@ public static function createRequiredFiles(Event $event) {
5758
}
5859
}
5960

61+
/**
62+
* Checks if the installed version of Composer is compatible.
63+
*
64+
* Composer 1.0.0 and higher consider a `composer install` without having a
65+
* lock file present as equal to `composer update`. We do not ship with a lock
66+
* file to avoid merge conflicts downstream, meaning that if a project is
67+
* installed with an older version of Composer the scaffolding of Drupal will
68+
* not be triggered. We check this here instead of in drupal-scaffold to be
69+
* able to give immediate feedback to the end user, rather than failing the
70+
* installation after going through the lengthy process of compiling and
71+
* downloading the Composer dependencies.
72+
*
73+
* @see https://github.com/composer/composer/pull/5035
74+
*/
75+
public static function checkComposerVersion(Event $event) {
76+
$composer = $event->getComposer();
77+
$io = $event->getIO();
78+
79+
$version = $composer::VERSION;
80+
81+
// If Composer is installed through git we have no easy way to determine if
82+
// it is new enough, just display a warning.
83+
if ($version === '@package_version@') {
84+
$io->writeError('<warning>You are running a development version of Composer. If you experience problems, please update Composer to the latest stable version.</warning>');
85+
}
86+
elseif (Comparator::lessThan($version, '1.0.0')) {
87+
$io->writeError('<error>Drupal-project requires Composer version 1.0.0 or higher. Please update your Composer before continuing</error>.');
88+
exit(1);
89+
}
90+
}
91+
6092
}

0 commit comments

Comments
 (0)