libraries.drush.inc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * @file
  4. * Drush integration for Libraries API.
  5. */
  6. /**
  7. * Implements hook_drush_command().
  8. */
  9. function libraries_drush_command() {
  10. $items['libraries-list'] = array(
  11. 'callback' => 'libraries_drush_list',
  12. 'description' => dt('Lists registered library information.'),
  13. 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
  14. );
  15. /**$items['libraries-download'] = array(
  16. 'callback' => 'libraries_drush_download',
  17. 'description' => dt('Downloads a registered library into the libraries directory for the active site.'),
  18. 'arguments' => array(
  19. 'name' => dt('The internal name of the registered library.'),
  20. ),
  21. );*/
  22. return $items;
  23. }
  24. /**
  25. * Implements hook_drush_help().
  26. */
  27. function libraries_drush_help($section) {
  28. switch ($section) {
  29. case 'drush:libraries-list':
  30. return dt('Lists registered library information.');
  31. case 'drush:libraries-download':
  32. return dt('Downloads a registered library into the libraries directory for the active site.
  33. See libraries-list for a list of registered libraries.');
  34. }
  35. }
  36. /**
  37. * Lists registered library information.
  38. */
  39. function libraries_drush_list() {
  40. $libraries = array();
  41. foreach (libraries_info() as $name => $info) {
  42. $libraries[$name] = libraries_detect($name);
  43. }
  44. ksort($libraries);
  45. if (empty($libraries)) {
  46. drush_print('There are no registered libraries.');
  47. }
  48. else {
  49. $rows = array();
  50. // drush_print_table() automatically treats the first row as the header, if
  51. // $header is TRUE.
  52. $rows[] = array(dt('Name'), dt('Status'), dt('Version'), dt('Variants'), dt('Dependencies'));
  53. foreach ($libraries as $name => $library) {
  54. $status = ($library['installed'] ? dt('OK') : drupal_ucfirst($library['error']));
  55. $version = (($library['installed'] && !empty($library['version'])) ? $library['version'] : '-');
  56. // Only list installed variants.
  57. $variants = array();
  58. foreach ($library['variants'] as $variant_name => $variant) {
  59. if ($variant['installed']) {
  60. $variants[] = $variant_name;
  61. }
  62. }
  63. $variants = (empty($variants) ? '-' : implode(', ', $variants));
  64. $dependencies = (!empty($library['dependencies']) ? implode(', ', $library['dependencies']) : '-');
  65. $rows[] = array($name, $status, $version, $variants, $dependencies);
  66. }
  67. // Make the possible values for the 'Status' column and the 'Version' header
  68. // wrap nicely.
  69. $widths = array(0, 12, 7, 0, 0);
  70. drush_print_table($rows, TRUE, $widths);
  71. }
  72. }
  73. /**
  74. * Downloads a library.
  75. *
  76. * @param $name
  77. * The internal name of the library to download.
  78. */
  79. function libraries_drush_download($name) {
  80. return;
  81. // @todo Looks wonky?
  82. if (!drush_shell_exec('type unzip')) {
  83. return drush_set_error(dt('Missing dependency: unzip. Install it before using this command.'));
  84. }
  85. // @todo Simply use current drush site.
  86. $args = func_get_args();
  87. if ($args[0]) {
  88. $path = $args[0];
  89. }
  90. else {
  91. $path = 'sites/all/libraries';
  92. }
  93. // Create the path if it does not exist.
  94. if (!is_dir($path)) {
  95. drush_op('mkdir', $path);
  96. drush_log(dt('Directory @path was created', array('@path' => $path)), 'notice');
  97. }
  98. // Set the directory to the download location.
  99. $olddir = getcwd();
  100. chdir($path);
  101. $filename = basename(COLORBOX_DOWNLOAD_URI);
  102. $dirname = basename(COLORBOX_DOWNLOAD_URI, '.zip');
  103. // Remove any existing Colorbox plugin directory
  104. if (is_dir($dirname)) {
  105. drush_log(dt('A existing Colorbox plugin was overwritten at @path', array('@path' => $path)), 'notice');
  106. }
  107. // Remove any existing Colorbox plugin zip archive
  108. if (is_file($filename)) {
  109. drush_op('unlink', $filename);
  110. }
  111. // Download the zip archive
  112. if (!drush_shell_exec('wget '. COLORBOX_DOWNLOAD_URI)) {
  113. drush_shell_exec('curl -O '. COLORBOX_DOWNLOAD_URI);
  114. }
  115. if (is_file($filename)) {
  116. // Decompress the zip archive
  117. drush_shell_exec('unzip -qq -o '. $filename);
  118. // Remove the zip archive
  119. drush_op('unlink', $filename);
  120. }
  121. // Set working directory back to the previous working directory.
  122. chdir($olddir);
  123. if (is_dir($path .'/'. $dirname)) {
  124. drush_log(dt('Colorbox plugin has been downloaded to @path', array('@path' => $path)), 'success');
  125. }
  126. else {
  127. drush_log(dt('Drush was unable to download the Colorbox plugin to @path', array('@path' => $path)), 'error');
  128. }
  129. }