Browse Source

Support platform locks: extensions and PHP version.

- Alias renamed to 'cck' per yched remark
Frederic G. MARAND 7 years ago
parent
commit
0146c8aa55
2 changed files with 57 additions and 15 deletions
  1. 13 4
      README.md
  2. 44 11
      composer_check.drush.inc

+ 13 - 4
README.md

@@ -1,6 +1,6 @@
 # Drupal Composer check
 
-This Drush plugin provides a `composer-check` (alias `cc`) command, which 
+This Drush plugin provides a `composer-check` (alias `cck`) command, which 
 compares the packages versions in `composer.lock` with those requested in the
 `composer.json` file of a Drupal 8 project.
 
@@ -13,18 +13,27 @@ compares the packages versions in `composer.lock` with those requested in the
 * Options:
   * `--all`: List all locked packages, even those not requested
   * `--yaml`: Produce YAML output instead of a table
-* Aliases: `cc`
+* Aliases: `cck`
+
+
+# Composer / Packagist
+
+This package is available on [Packagist] as `fgm/composer-check`, but for now the 
+`config` section of your `composer.json` will need `"secure-http": false` 
+because this repo is not yet available over HTTPS.
+
+[Packagist]: https://packagist.org/packages/fgm/composer-check
 
 
 # Examples
 
 * Default usage in a `drupal/drupal` project:
 
-      drush cc
+      drush cck
 
 * Listing all packages in a `drupal-composer/drupal-project` project, in YAML format:
 
-      drush cc --all --yaml $PWD/composer.lock
+      drush cck --all --yaml $PWD/composer.lock
 
 
 # License

+ 44 - 11
composer_check.drush.inc

@@ -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'] ?? '',
       ];
     }
   }