composer_check.drush.inc 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. use Symfony\Component\Yaml\Yaml;
  3. function composer_check_drush_command() {
  4. $cmds['lock'] = [
  5. 'arguments' => [
  6. 'lock file' => 'The path to the lock file. Defaults to the one in drupal root.',
  7. ],
  8. ];
  9. return $cmds;
  10. }
  11. function drush_composer_check_lock($path = NULL) {
  12. if (empty($path)) {
  13. $path = DRUPAL_ROOT . '/composer.lock';
  14. }
  15. if (!is_file($path) && is_readable($path)) {
  16. drush_die("Cannot read lock file");
  17. }
  18. $jsonPath = dirname($path) . '/composer.json';
  19. if (!is_file($jsonPath) && is_readable($jsonPath)) {
  20. drush_die("Cannot read json file");
  21. }
  22. $json = json_decode(file_get_contents($jsonPath), TRUE);
  23. $jsonPackages = $json['require'] ?? [];
  24. $jsonDevPackages = $json['require-dev'] ?? [];
  25. $lock = json_decode(file_get_contents($path), TRUE);
  26. $lockPackages = $lock['packages'];
  27. $lockDevPackages = $lock['packages-dev'];
  28. $packages = ['dev' => [], 'run' => []];
  29. foreach ($jsonPackages as $package => $requirement) {
  30. $packages['run'][$package]['requirement'] = $requirement;
  31. }
  32. foreach ($jsonDevPackages as $package => $requirement) {
  33. $packages['dev'][$package] = $requirement;
  34. }
  35. foreach ($lockPackages as $packageInfo) {
  36. $package = $packageInfo['name'];
  37. $version = $packageInfo['version'];
  38. $packages['run'][$package]['version'] = $version;
  39. }
  40. foreach ($lockDevPackages as $packageInfo) {
  41. $package = $packageInfo['name'];
  42. $version = $packageInfo['version'];
  43. $packages['dev'][$package]['version'] = $version;
  44. }
  45. ksort($packages['dev']);
  46. ksort($packages['run']);
  47. if (drush_get_option('pipe')) {
  48. drush_print(Yaml::dump($packages));
  49. }
  50. else {
  51. _composer_check_output_human($packages);
  52. }
  53. }
  54. function _composer_check_output_human($packages) {
  55. $header = ['Kind', 'Name', 'Requirement', 'Version'];
  56. $rows = [$header];
  57. foreach ($packages as $kind => $kindPackages) {
  58. foreach ($kindPackages as $package => $info) {
  59. $rows["$package/$kind"] = [
  60. $package,
  61. $kind,
  62. $info['requirement'] ?? '',
  63. $info['version'] ?? ''
  64. ];
  65. }
  66. }
  67. ksort($rows);
  68. drush_print_table($rows, FALSE);
  69. }