composer_check.drush.inc 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. use Symfony\Component\Yaml\Yaml;
  3. function composer_check_drush_command() {
  4. $cmds['composer-check'] = [
  5. 'aliases' => ['cc'],
  6. 'description' => 'Lists the packages requested in composer.json and the matching locked version.',
  7. 'arguments' => [
  8. 'composer.lock' => 'The path to the lock file. Defaults to the one in drupal root.',
  9. ],
  10. 'options' => [
  11. 'all' => [
  12. 'description' => 'List all locked packages, even those not requested',
  13. 'required' => FALSE,
  14. ],
  15. 'yaml' => 'Produce YAML output instead of a table',
  16. ],
  17. ];
  18. return $cmds;
  19. }
  20. function drush_composer_check($lockPath = NULL) {
  21. if (empty($lockPath)) {
  22. $lockPath = DRUPAL_ROOT . '/composer.lock';
  23. }
  24. if (!is_file($lockPath) && is_readable($lockPath)) {
  25. drush_die("Cannot read lock file");
  26. }
  27. $jsonPath = dirname($lockPath) . '/composer.json';
  28. if (!is_file($jsonPath) && is_readable($jsonPath)) {
  29. drush_die("Cannot read json file");
  30. }
  31. $json = json_decode(file_get_contents($jsonPath), TRUE);
  32. $jsonPackages = $json['require'] ?? [];
  33. $jsonDevPackages = $json['require-dev'] ?? [];
  34. $lock = json_decode(file_get_contents($lockPath), TRUE);
  35. $lockPackages = $lock['packages'];
  36. $lockDevPackages = $lock['packages-dev'];
  37. $all = !!drush_get_option('all');
  38. $packages = ['dev' => [], 'run' => []];
  39. foreach ($jsonPackages as $package => $requirement) {
  40. if ($all || !empty($requirement)) {
  41. $packages['run'][$package]['requirement'] = $requirement;
  42. }
  43. }
  44. foreach ($jsonDevPackages as $package => $requirement) {
  45. if ($all || !empty($requirement)) {
  46. $packages['dev'][$package] = $requirement;
  47. }
  48. }
  49. foreach ($lockPackages as $packageInfo) {
  50. $package = $packageInfo['name'];
  51. if ($all || !empty($packages['run'][$package])) {
  52. $version = $packageInfo['version'];
  53. $packages['run'][$package]['version'] = $version;
  54. }
  55. }
  56. foreach ($lockDevPackages as $packageInfo) {
  57. $package = $packageInfo['name'];
  58. if ($all || !empty($packages['dev'][$package])) {
  59. $version = $packageInfo['version'];
  60. $packages['dev'][$package]['version'] = $version;
  61. }
  62. }
  63. ksort($packages['dev']);
  64. ksort($packages['run']);
  65. if (drush_get_option('yaml')) {
  66. echo Yaml::dump($packages, 3);
  67. }
  68. else {
  69. _composer_check_output_human($packages);
  70. }
  71. }
  72. function _composer_check_output_human($packages) {
  73. $header = ['Name', 'Kind', 'Requirement', 'Version'];
  74. $rows = [$header];
  75. foreach ($packages as $kind => $kindPackages) {
  76. foreach ($kindPackages as $package => $info) {
  77. $rows["$package/$kind"] = [
  78. $package,
  79. $kind,
  80. $info['requirement'] ?? '',
  81. $info['version'] ?? ''
  82. ];
  83. }
  84. }
  85. ksort($rows);
  86. drush_print_table($rows, FALSE);
  87. }