libraries.drush.inc 9.3 KB

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