Projet

Général

Profil

Paste
Télécharger (4,3 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / libraries / libraries.drush.inc @ ebcc4118

1
<?php
2

    
3
/**
4
 * @file
5
 * Drush integration for Libraries API.
6
 */
7

    
8
/**
9
 * Implements hook_drush_command().
10
 */
11
function libraries_drush_command() {
12
  $items['libraries-list'] = array(
13
    'callback' => 'libraries_drush_list',
14
    'description' => dt('Lists registered library information.'),
15
    'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
16
  );
17
  /**$items['libraries-download'] = array(
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
    ),
23
  );*/
24
  return $items;
25
}
26

    
27
/**
28
 * Implements hook_drush_help().
29
 */
30
function 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

    
38
See libraries-list for a list of registered libraries.');
39
  }
40
}
41

    
42
/**
43
 * Lists registered library information.
44
 */
45
function libraries_drush_list() {
46
  $libraries = array();
47
  foreach (libraries_info() as $name => $info) {
48
    $libraries[$name] = libraries_detect($name);
49
  }
50
  ksort($libraries);
51

    
52
  if (empty($libraries)) {
53
    drush_print('There are no registered libraries.');
54
  }
55

    
56
  else {
57
    $rows = array();
58
    // drush_print_table() automatically treats the first row as the header, if
59
    // $header is TRUE.
60
    $rows[] = array(dt('Name'), dt('Status'), dt('Version'), dt('Variants'), dt('Dependencies'));
61
    foreach ($libraries as $name => $library) {
62
      $status = ($library['installed'] ? dt('OK') : drupal_ucfirst($library['error']));
63
      $version = (($library['installed'] && !empty($library['version'])) ? $library['version'] : '-');
64

    
65
      // Only list installed variants.
66
      $variants = array();
67
      foreach ($library['variants'] as $variant_name => $variant) {
68
        if ($variant['installed']) {
69
          $variants[] = $variant_name;
70
        }
71
      }
72
      $variants = (empty($variants) ? '-' : implode(', ', $variants));
73

    
74
      $dependencies = (!empty($library['dependencies']) ? implode(', ', $library['dependencies']) : '-');
75

    
76
      $rows[] = array($name, $status, $version, $variants, $dependencies);
77
    }
78
    // Make the possible values for the 'Status' column and the 'Version' header
79
    // wrap nicely.
80
    $widths = array(0, 12, 7, 0, 0);
81
    drush_print_table($rows, TRUE, $widths);
82
  }
83
}
84

    
85
/**
86
 * Downloads a library.
87
 *
88
 * @param $name
89
 *   The internal name of the library to download.
90
 */
91
function libraries_drush_download($name) {
92
  return;
93

    
94
  // @todo Looks wonky?
95
  if (!drush_shell_exec('type unzip')) {
96
    return drush_set_error(dt('Missing dependency: unzip. Install it before using this command.'));
97
  }
98

    
99
  // @todo Simply use current drush site.
100
  $args = func_get_args();
101
  if ($args[0]) {
102
    $path = $args[0];
103
  }
104
  else {
105
    $path = 'sites/all/libraries';
106
  }
107

    
108
  // Create the path if it does not exist.
109
  if (!is_dir($path)) {
110
    drush_op('mkdir', $path);
111
    drush_log(dt('Directory @path was created', array('@path' => $path)), 'notice');
112
  }
113

    
114
  // Set the directory to the download location.
115
  $olddir = getcwd();
116
  chdir($path);
117

    
118
  $filename = basename(COLORBOX_DOWNLOAD_URI);
119
  $dirname = basename(COLORBOX_DOWNLOAD_URI, '.zip');
120

    
121
  // Remove any existing Colorbox plugin directory
122
  if (is_dir($dirname)) {
123
    drush_log(dt('A existing Colorbox plugin was overwritten at @path', array('@path' => $path)), 'notice');
124
  }
125
  // Remove any existing Colorbox plugin zip archive
126
  if (is_file($filename)) {
127
    drush_op('unlink', $filename);
128
  }
129

    
130
  // Download the zip archive
131
  if (!drush_shell_exec('wget '. COLORBOX_DOWNLOAD_URI)) {
132
    drush_shell_exec('curl -O '. COLORBOX_DOWNLOAD_URI);
133
  }
134

    
135
  if (is_file($filename)) {
136
    // Decompress the zip archive
137
    drush_shell_exec('unzip -qq -o '. $filename);
138
    // Remove the zip archive
139
    drush_op('unlink', $filename);
140
  }
141

    
142
  // Set working directory back to the previous working directory.
143
  chdir($olddir);
144

    
145
  if (is_dir($path .'/'. $dirname)) {
146
    drush_log(dt('Colorbox plugin has been downloaded to @path', array('@path' => $path)), 'success');
147
  }
148
  else {
149
    drush_log(dt('Drush was unable to download the Colorbox plugin to @path', array('@path' => $path)), 'error');
150
  }
151
}