|
@@ -1,10 +1,19 @@
|
|
|
<?php
|
|
|
|
|
|
+/**
|
|
|
+ * @file
|
|
|
+ * A Drush plugin to compare composer.json and composer.lock.
|
|
|
+ */
|
|
|
+
|
|
|
+use Drupal\Component\Utility\Unicode;
|
|
|
use Symfony\Component\Yaml\Yaml;
|
|
|
|
|
|
+/**
|
|
|
+ * Implements hook_drush_command().
|
|
|
+ */
|
|
|
function composer_check_drush_command() {
|
|
|
$cmds['composer-check'] = [
|
|
|
- 'aliases' => ['cc'],
|
|
|
+ 'aliases' => ['cck'],
|
|
|
'description' => 'Lists the packages requested in composer.json and the matching locked version.',
|
|
|
'arguments' => [
|
|
|
'composer.lock' => 'The path to the lock file. Defaults to the one in drupal root.',
|
|
@@ -21,6 +30,12 @@ function composer_check_drush_command() {
|
|
|
return $cmds;
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+ * Command callback for composer-check.
|
|
|
+ *
|
|
|
+ * @param null|string $lockPath
|
|
|
+ * Optional. The path to a composer.lock file.
|
|
|
+ */
|
|
|
function drush_composer_check($lockPath = NULL) {
|
|
|
if (empty($lockPath)) {
|
|
|
$lockPath = DRUPAL_ROOT . '/composer.lock';
|
|
@@ -38,31 +53,43 @@ function drush_composer_check($lockPath = NULL) {
|
|
|
$jsonPackages = $json['require'] ?? [];
|
|
|
$jsonDevPackages = $json['require-dev'] ?? [];
|
|
|
|
|
|
- $lock = json_decode(file_get_contents($lockPath), TRUE);
|
|
|
- $lockPackages = $lock['packages'];
|
|
|
- $lockDevPackages = $lock['packages-dev'];
|
|
|
+ $lockFile = json_decode(file_get_contents($lockPath), TRUE);
|
|
|
+ $lockPackages = $lockFile['packages'];
|
|
|
+ $lockDevPackages = $lockFile['packages-dev'];
|
|
|
+
|
|
|
+ $lockPlatform = $lockFile['platform'];
|
|
|
+ array_walk($lockPlatform, function (&$requirement, $component) {
|
|
|
+ $requirement = [
|
|
|
+ 'name' => $component,
|
|
|
+ 'version' => $requirement,
|
|
|
+ ];
|
|
|
+ });
|
|
|
+ $lockPackages = array_merge($lockPackages, $lockPlatform);
|
|
|
+ $lockDevPackages = array_merge($lockDevPackages, $lockPlatform);
|
|
|
|
|
|
$all = !!drush_get_option('all');
|
|
|
$packages = ['dev' => [], 'run' => []];
|
|
|
foreach ($jsonPackages as $package => $requirement) {
|
|
|
if ($all || !empty($requirement)) {
|
|
|
+ $package = Unicode::strtolower($package);
|
|
|
$packages['run'][$package]['requirement'] = $requirement;
|
|
|
}
|
|
|
}
|
|
|
foreach ($jsonDevPackages as $package => $requirement) {
|
|
|
if ($all || !empty($requirement)) {
|
|
|
- $packages['dev'][$package] = $requirement;
|
|
|
+ $package = Unicode::strtolower($package);
|
|
|
+ $packages['dev'][$package]['requirement'] = $requirement;
|
|
|
}
|
|
|
}
|
|
|
foreach ($lockPackages as $packageInfo) {
|
|
|
- $package = $packageInfo['name'];
|
|
|
+ $package = Unicode::strtolower($packageInfo['name']);
|
|
|
if ($all || !empty($packages['run'][$package])) {
|
|
|
$version = $packageInfo['version'];
|
|
|
$packages['run'][$package]['version'] = $version;
|
|
|
}
|
|
|
}
|
|
|
foreach ($lockDevPackages as $packageInfo) {
|
|
|
- $package = $packageInfo['name'];
|
|
|
+ $package = Unicode::strtolower($packageInfo['name']);
|
|
|
if ($all || !empty($packages['dev'][$package])) {
|
|
|
$version = $packageInfo['version'];
|
|
|
$packages['dev'][$package]['version'] = $version;
|
|
@@ -73,12 +100,18 @@ function drush_composer_check($lockPath = NULL) {
|
|
|
|
|
|
if (drush_get_option('yaml')) {
|
|
|
echo Yaml::dump($packages, 3);
|
|
|
+ return;
|
|
|
}
|
|
|
- else {
|
|
|
- _composer_check_output_human($packages);
|
|
|
- }
|
|
|
+
|
|
|
+ _composer_check_output_human($packages);
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+ * Display a package comparison as a text table.
|
|
|
+ *
|
|
|
+ * @param array $packages
|
|
|
+ * A package comparison array.
|
|
|
+ */
|
|
|
function _composer_check_output_human($packages) {
|
|
|
$header = ['Name', 'Kind', 'Requirement', 'Version'];
|
|
|
$rows = [$header];
|
|
@@ -88,7 +121,7 @@ function _composer_check_output_human($packages) {
|
|
|
$package,
|
|
|
$kind,
|
|
|
$info['requirement'] ?? '',
|
|
|
- $info['version'] ?? ''
|
|
|
+ $info['version'] ?? '',
|
|
|
];
|
|
|
}
|
|
|
}
|