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