['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); }