colorbox.drush.inc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * @file
  4. * Drush integration for colorbox.
  5. */
  6. /**
  7. * The Colorbox plugin URI.
  8. */
  9. define('COLORBOX_DOWNLOAD_URI', 'https://github.com/jackmoore/colorbox/archive/master.zip');
  10. define('COLORBOX_DOWNLOAD_PREFIX', 'colorbox-');
  11. /**
  12. * Implements hook_drush_command().
  13. */
  14. function colorbox_drush_command() {
  15. $items = array();
  16. // The key in the $items array is the name of the command.
  17. $items['colorbox-plugin'] = array(
  18. 'callback' => 'drush_colorbox_plugin',
  19. 'description' => dt('Download and install the Colorbox plugin.'),
  20. // No bootstrap.
  21. 'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
  22. 'arguments' => array(
  23. 'path' => dt('Optional. A path where to install the Colorbox plugin. If omitted Drush will use the default location.'),
  24. ),
  25. 'aliases' => array('colorboxplugin'),
  26. );
  27. return $items;
  28. }
  29. /**
  30. * Implements hook_drush_help().
  31. *
  32. * This function is called whenever a drush user calls
  33. * 'drush help <name-of-your-command>'
  34. */
  35. function colorbox_drush_help($section) {
  36. switch ($section) {
  37. case 'drush:colorbox-plugin':
  38. return dt('Download and install the Colorbox plugin from jacklmoore.com/colorbox, default location is the libraries directory.');
  39. }
  40. }
  41. /**
  42. * Command to download the Colorbox plugin.
  43. */
  44. function drush_colorbox_plugin() {
  45. $args = func_get_args();
  46. if (!empty($args[0])) {
  47. $path = $args[0];
  48. }
  49. else {
  50. $path = 'libraries';
  51. }
  52. // Create the path if it does not exist.
  53. if (!is_dir($path)) {
  54. drush_op('mkdir', $path);
  55. drush_log(dt('Directory @path was created', array('@path' => $path)), 'notice');
  56. }
  57. // Set the directory to the download location.
  58. $olddir = getcwd();
  59. chdir($path);
  60. // Download the zip archive.
  61. if ($filepath = drush_download_file(COLORBOX_DOWNLOAD_URI)) {
  62. $filename = basename($filepath);
  63. $dirname = COLORBOX_DOWNLOAD_PREFIX . basename($filepath, '.zip');
  64. // Remove any existing Colorbox plugin directory.
  65. if (is_dir($dirname) || is_dir('colorbox')) {
  66. drush_delete_dir($dirname, TRUE);
  67. drush_delete_dir('colorbox', TRUE);
  68. drush_log(dt('A existing Colorbox plugin was deleted from @path', array('@path' => $path)), 'notice');
  69. }
  70. // Decompress the zip archive.
  71. drush_tarball_extract($filename);
  72. // Change the directory name to "colorbox" if needed.
  73. if ($dirname != 'colorbox') {
  74. drush_move_dir($dirname, 'colorbox', TRUE);
  75. $dirname = 'colorbox';
  76. }
  77. }
  78. if (is_dir($dirname)) {
  79. drush_log(dt('Colorbox plugin has been installed in @path', array('@path' => $path)), 'success');
  80. }
  81. else {
  82. drush_log(dt('Drush was unable to install the Colorbox plugin to @path', array('@path' => $path)), 'error');
  83. }
  84. // Set working directory back to the previous working directory.
  85. chdir($olddir);
  86. }