123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- <?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' => ['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.',
- ],
- 'options' => [
- 'all' => [
- 'description' => 'List all locked packages, even those not requested',
- 'required' => FALSE,
- ],
- 'yaml' => 'Produce YAML output instead of a table',
- ],
- ];
- 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';
- }
- if (!is_file($lockPath) && is_readable($lockPath)) {
- drush_die("Cannot read lock file");
- }
- $jsonPath = dirname($lockPath) . '/composer.json';
- if (!is_file($jsonPath) && is_readable($jsonPath)) {
- drush_die("Cannot read json file");
- }
- $json = json_decode(file_get_contents($jsonPath), TRUE);
- $jsonPackages = $json['require'] ?? [];
- $jsonDevPackages = $json['require-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)) {
- $package = Unicode::strtolower($package);
- $packages['dev'][$package]['requirement'] = $requirement;
- }
- }
- foreach ($lockPackages as $packageInfo) {
- $package = Unicode::strtolower($packageInfo['name']);
- if ($all || !empty($packages['run'][$package])) {
- $version = $packageInfo['version'];
- $packages['run'][$package]['version'] = $version;
- }
- }
- foreach ($lockDevPackages as $packageInfo) {
- $package = Unicode::strtolower($packageInfo['name']);
- if ($all || !empty($packages['dev'][$package])) {
- $version = $packageInfo['version'];
- $packages['dev'][$package]['version'] = $version;
- }
- }
- ksort($packages['dev']);
- ksort($packages['run']);
- if (drush_get_option('yaml')) {
- echo Yaml::dump($packages, 3);
- return;
- }
- _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];
- foreach ($packages as $kind => $kindPackages) {
- foreach ($kindPackages as $package => $info) {
- $rows["$package/$kind"] = [
- $package,
- $kind,
- $info['requirement'] ?? '',
- $info['version'] ?? '',
- ];
- }
- }
- ksort($rows);
- drush_print_table($rows, FALSE);
- }
|