libraries.drush.inc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 = array();
  11. $items['libraries-list'] = array(
  12. 'description' => dt('Show a list of registered libraries.'),
  13. 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
  14. 'aliases' => array('lls', 'lib-list'),
  15. );
  16. $items['libraries-download'] = array(
  17. 'description' => dt('Download library files of registered libraries.'),
  18. 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
  19. 'aliases' => array('ldl', 'lib-download'),
  20. 'arguments' => array(
  21. 'libraries' => 'A comma delimited list of library machine names.',
  22. ),
  23. 'required-arguments' => TRUE,
  24. );
  25. return $items;
  26. }
  27. /**
  28. * Implements hook_drush_cache_clear().
  29. *
  30. * @see drush_cache_clear_types()
  31. */
  32. function libraries_drush_cache_clear(array &$types) {
  33. $types['libraries'] = 'libraries_drush_invalidate_cache';
  34. }
  35. /**
  36. * Clears the library cache.
  37. */
  38. function libraries_drush_invalidate_cache() {
  39. // @see drupal_flush_all_caches()
  40. foreach (libraries_flush_caches() as $table) {
  41. cache_clear_all('*', $table, TRUE);
  42. }
  43. }
  44. /**
  45. * Command callback. Show a list of registered libraries.
  46. */
  47. function drush_libraries_list() {
  48. $libraries = libraries_detect();
  49. ksort($libraries);
  50. if (empty($libraries)) {
  51. drush_print('There are no registered libraries.');
  52. }
  53. else {
  54. module_load_include('inc', 'libraries', 'libraries.admin');
  55. $rows = array();
  56. // drush_print_table() automatically treats the first row as the header, if
  57. // $header is TRUE.
  58. $rows[] = array(
  59. dt('Name'),
  60. dt('Status'),
  61. dt('Version'),
  62. dt('Variants'),
  63. dt('Dependencies'),
  64. dt('Provider'),
  65. );
  66. foreach ($libraries as $name => $library) {
  67. // Only list installed variants.
  68. $variants = array();
  69. foreach ($library['variants'] as $variant_name => $variant) {
  70. if ($variant['installed']) {
  71. $variants[] = $variant_name;
  72. }
  73. }
  74. $rows[] = array(
  75. $name,
  76. $library['installed'] ? dt('OK') : drupal_ucfirst($library['error']),
  77. ($library['installed'] && $library['version']) ? '-' : $library['version'],
  78. $variants ? implode(', ', $variants) : '-',
  79. $library['dependencies'] ? implode(', ', $library['dependencies']) : '-',
  80. libraries_admin_get_provider($library),
  81. );
  82. }
  83. // Make the possible values for the 'Status' column and the 'Version' header
  84. // wrap nicely.
  85. $widths = array(0, 12, 7, 0, 0, 0);
  86. drush_print_table($rows, TRUE, $widths);
  87. }
  88. }
  89. /**
  90. * Command callback. Downloads a library.
  91. *
  92. * Only libraries that provide a download file URL can be downloaded.
  93. *
  94. * @see hook_libraries_info()
  95. * @see drush_pm_download()
  96. */
  97. function drush_libraries_download() {
  98. drush_command_include('pm-download');
  99. $libraries = libraries_info();
  100. // @todo Consider supporting downloading all downloadable libraries.
  101. // @todo Consider offering a selection if no library is specified.
  102. foreach (pm_parse_arguments(func_get_args(), FALSE) as $machine_name) {
  103. if (!isset($libraries[$machine_name])) {
  104. $message = dt("The !library library is not registered with Libraries API.\n", array('!library' => $machine_name));
  105. $message .= dt("Provide an info file for it or implement hook_libraries_info().\n");
  106. $message .= dt("See hook_libraries_info() for more information.\n");
  107. drush_set_error('DRUSH_LIBRARY_UKNOWN', $message);
  108. continue;
  109. }
  110. $library = $libraries[$machine_name];
  111. if (empty($library['download file url'])) {
  112. $message = dt("The !library library cannot be downloaded.\n", array('!library' => $machine_name));
  113. $message .= dt("Libraries need to specify a download file URL to support being downloaded via Drush.\n");
  114. $message .= dt("See hook_libraries_info() for more information.\n");
  115. drush_set_error('DRUSH_LIBRARY_NOT_DOWNLOADABLE', $message);
  116. continue;
  117. }
  118. $download_url = $library['download file url'];
  119. drush_log(dt('Downloading library !name ...', array('!name' => $machine_name)));
  120. // @see package_handler_download_project() in wget.inc
  121. // It cannot be used directly because it will always try to extract the
  122. // archive which fails when downloading a single file.
  123. // @todo Modify upstream to be able to use
  124. // package_handler_download_project() directly.
  125. // Prepare download path. On Windows file name cannot contain '?'.
  126. // See http://drupal.org/node/1782444
  127. $filename = str_replace('?', '_', basename($download_url));
  128. $download_path = drush_tempdir() . '/' . $filename;
  129. // Download the tarball.
  130. // Never cache the downloaded file. The downloading relies on the fact that
  131. // different versions of the library are available under the same URL as new
  132. // versions are released.
  133. $download_path = drush_download_file($download_url, $download_path, 0);
  134. if ($download_path || drush_get_context('DRUSH_SIMULATE')) {
  135. drush_log(dt('Downloading !filename was successful.', array('!filename' => $filename)));
  136. }
  137. else {
  138. drush_set_error('DRUSH_PM_DOWNLOAD_FAILED', dt('Unable to download !project to !path from !url.', array('!project' => $machine_name, '!path' => $download_path, '!url' => $download_url)));
  139. drush_log(dt('Error downloading !name', array('!name' => $machine_name)), 'error');
  140. continue;
  141. }
  142. // @todo Suport MD5 file hashing.
  143. // Extract the tarball in place and return the full path to the untarred directory.
  144. $download_base = dirname($download_path);
  145. if (drush_file_is_tarball($download_path)) {
  146. if (!$tar_file_list = drush_tarball_extract($download_path, $download_base, TRUE)) {
  147. // An error has been logged.
  148. return FALSE;
  149. }
  150. $tar_directory = drush_trim_path($tar_file_list[0]);
  151. $download_path = $download_base . '/' . $tar_directory;
  152. }
  153. else {
  154. $download_path = $download_base;
  155. }
  156. // Determine the install location for the project. User provided
  157. // --destination has preference.
  158. $destination = drush_get_option('destination');
  159. if (!empty($destination)) {
  160. if (!file_exists($destination)) {
  161. drush_mkdir($destination);
  162. }
  163. $install_location = realpath($destination);
  164. }
  165. else {
  166. /** @see _pm_download_destination_lookup() */
  167. // _pm_download_destination_lookup() pluralizes the passed type by
  168. // appending an s.
  169. // This relies on the fact that there is no library named 'contrib'.
  170. // @todo Request that this be turned into a proper API upstream.
  171. $install_location = _pm_download_destination('librarie');
  172. }
  173. // @todo Consider invoking a hook similar to
  174. // hook_drush_pm_download_destination_alter().
  175. // @todo Consider adding version-control support similar to pm-download.
  176. $install_location .= '/' . $machine_name;
  177. // Check if install location already exists.
  178. if (is_dir($install_location)) {
  179. if (drush_confirm(dt('Install location !location already exists. Do you want to overwrite it?', array('!location' => $install_location)))) {
  180. drush_delete_dir($install_location, TRUE);
  181. }
  182. else {
  183. drush_log(dt("Skip installation of !project to !dest.", array('!project' => $library['machine name'], '!dest' => $install_location)), 'warning');
  184. continue;
  185. }
  186. }
  187. // Copy the project to the install location.
  188. if (drush_op('_drush_recursive_copy', $download_path, $install_location)) {
  189. drush_log(dt("Library !project downloaded to !dest.", array('!project' => $machine_name, '!dest' => $install_location)), 'success');
  190. // @todo Consider invoking a hook similar to
  191. // hook_drush_pm_post_download().
  192. // @todo Support printing release notes.
  193. }
  194. else {
  195. // We don't `return` here in order to proceed with downloading additional projects.
  196. drush_set_error('DRUSH_PM_DOWNLOAD_FAILED', dt("Project !project could not be downloaded to !dest.", array('!project' => $machine_name, '!dest' => $install_location)));
  197. }
  198. // @todo Consider adding notify support.
  199. }
  200. }