12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- <?php
- use Symfony\Component\Yaml\Yaml;
- function composer_check_drush_command() {
- $cmds['composer-check'] = [
- 'aliases' => ['cc'],
- '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;
- }
- 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'] ?? [];
- $lock = json_decode(file_get_contents($lockPath), TRUE);
- $lockPackages = $lock['packages'];
- $lockDevPackages = $lock['packages-dev'];
- $all = !!drush_get_option('all');
- $packages = ['dev' => [], 'run' => []];
- foreach ($jsonPackages as $package => $requirement) {
- if ($all || !empty($requirement)) {
- $packages['run'][$package]['requirement'] = $requirement;
- }
- }
- foreach ($jsonDevPackages as $package => $requirement) {
- if ($all || !empty($requirement)) {
- $packages['dev'][$package] = $requirement;
- }
- }
- foreach ($lockPackages as $packageInfo) {
- $package = $packageInfo['name'];
- if ($all || !empty($packages['run'][$package])) {
- $version = $packageInfo['version'];
- $packages['run'][$package]['version'] = $version;
- }
- }
- foreach ($lockDevPackages as $packageInfo) {
- $package = $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);
- }
- else {
- _composer_check_output_human($packages);
- }
- }
- 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);
- }
|