5 * Drush integration for Libraries API.
8 use Drupal\Component\Utility\Unicode;
11 * Implements hook_drush_command().
13 function libraries_drush_command() {
14 $items['libraries-list'] = array(
15 'callback' => 'libraries_drush_list',
16 'description' => dt('Lists registered library information.'),
17 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
19 /**$items['libraries-download'] = array(
20 'callback' => 'libraries_drush_download',
21 'description' => dt('Downloads a registered library into the libraries directory for the active site.'),
23 'name' => dt('The internal name of the registered library.'),
30 * Implements hook_drush_help().
32 function libraries_drush_help($section) {
34 case 'drush:libraries-list':
35 return dt('Lists registered library information.');
37 case 'drush:libraries-download':
38 return dt('Downloads a registered library into the libraries directory for the active site.
40 See libraries-list for a list of registered libraries.');
45 * Implements hook_drush_cache_clear().
47 * @see drush_cache_clear_types()
49 function libraries_drush_cache_clear(&$types) {
50 $types['libraries'] = 'libraries_drush_invalidate_cache';
54 * Clears the library cache.
56 function libraries_drush_invalidate_cache() {
57 \Drupal::cache('libraries')->deleteAll();
61 * Lists registered library information.
63 function libraries_drush_list() {
65 foreach (libraries_info() as $name => $info) {
66 $libraries[$name] = libraries_detect($name);
70 if (empty($libraries)) {
71 drush_print('There are no registered libraries.');
76 // drush_print_table() automatically treats the first row as the header, if
78 $rows[] = array(dt('Name'), dt('Status'), dt('Version'), dt('Variants'), dt('Dependencies'));
79 foreach ($libraries as $name => $library) {
80 $status = ($library['installed'] ? dt('OK') : Unicode::ucfirst($library['error']));
81 $version = (($library['installed'] && !empty($library['version'])) ? $library['version'] : '-');
83 // Only list installed variants.
85 foreach ($library['variants'] as $variant_name => $variant) {
86 if ($variant['installed']) {
87 $variants[] = $variant_name;
90 $variants = (empty($variants) ? '-' : implode(', ', $variants));
92 $dependencies = (!empty($library['dependencies']) ? implode(', ', $library['dependencies']) : '-');
94 $rows[] = array($name, $status, $version, $variants, $dependencies);
96 // Make the possible values for the 'Status' column and the 'Version' header
98 $widths = array(0, 12, 7, 0, 0);
99 drush_print_table($rows, TRUE, $widths);
104 * Downloads a library.
107 * The internal name of the library to download.
109 function libraries_drush_download($name) {
112 // @todo Looks wonky?
113 if (!drush_shell_exec('type unzip')) {
114 return drush_set_error(dt('Missing dependency: unzip. Install it before using this command.'));
117 // @todo Simply use current drush site.
118 $args = func_get_args();
123 $path = 'sites/all/libraries';
126 // Create the path if it does not exist.
127 if (!is_dir($path)) {
128 drush_op('mkdir', $path);
129 drush_log(dt('Directory @path was created', array('@path' => $path)), 'notice');
132 // Set the directory to the download location.
136 $filename = basename(COLORBOX_DOWNLOAD_URI);
137 $dirname = basename(COLORBOX_DOWNLOAD_URI, '.zip');
139 // Remove any existing Colorbox plugin directory
140 if (is_dir($dirname)) {
141 drush_log(dt('A existing Colorbox plugin was overwritten at @path', array('@path' => $path)), 'notice');
143 // Remove any existing Colorbox plugin zip archive
144 if (is_file($filename)) {
145 drush_op('unlink', $filename);
148 // Download the zip archive
149 if (!drush_shell_exec('wget '. COLORBOX_DOWNLOAD_URI)) {
150 drush_shell_exec('curl -O '. COLORBOX_DOWNLOAD_URI);
153 if (is_file($filename)) {
154 // Decompress the zip archive
155 drush_shell_exec('unzip -qq -o '. $filename);
156 // Remove the zip archive
157 drush_op('unlink', $filename);
160 // Set working directory back to the previous working directory.
163 if (is_dir($path .'/'. $dirname)) {
164 drush_log(dt('Colorbox plugin has been downloaded to @path', array('@path' => $path)), 'success');
167 drush_log(dt('Drush was unable to download the Colorbox plugin to @path', array('@path' => $path)), 'error');