colorbox.drush.inc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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/1.x.zip');
  10. define('COLORBOX_DOWNLOAD_PREFIX', 'colorbox-');
  11. /**
  12. * Implementation of hook_drush_command().
  13. *
  14. * In this hook, you specify which commands your
  15. * drush module makes available, what it does and
  16. * description.
  17. *
  18. * Notice how this structure closely resembles how
  19. * you define menu hooks.
  20. *
  21. * See `drush topic docs-commands` for a list of recognized keys.
  22. *
  23. * @return array
  24. * An associative array describing your command(s).
  25. */
  26. function colorbox_drush_command() {
  27. $items = array();
  28. // the key in the $items array is the name of the command.
  29. $items['colorbox-plugin'] = array(
  30. 'callback' => 'drush_colorbox_plugin',
  31. 'description' => dt('Download and install the Colorbox plugin.'),
  32. 'bootstrap' => DRUSH_BOOTSTRAP_DRUSH, // No bootstrap.
  33. 'arguments' => array(
  34. 'path' => dt('Optional. A path where to install the Colorbox plugin. If omitted Drush will use the default location.'),
  35. ),
  36. 'aliases' => array('colorboxplugin'),
  37. );
  38. return $items;
  39. }
  40. /**
  41. * Implementation of hook_drush_help().
  42. *
  43. * This function is called whenever a drush user calls
  44. * 'drush help <name-of-your-command>'
  45. *
  46. * @param string $section
  47. * A string with the help section (prepend with 'drush:')
  48. *
  49. * @return string
  50. * A string with the help text for your command.
  51. */
  52. function colorbox_drush_help($section) {
  53. switch ($section) {
  54. case 'drush:colorbox-plugin':
  55. return dt('Download and install the Colorbox plugin from jacklmoore.com/colorbox, default location is sites/all/libraries.');
  56. }
  57. }
  58. /**
  59. * Implements drush_MODULE_pre_pm_enable().
  60. */
  61. function drush_colorbox_pre_pm_enable() {
  62. $modules = drush_get_context('PM_ENABLE_MODULES');
  63. if (in_array('colorbox', $modules) && !drush_get_option('skip')) {
  64. drush_colorbox_plugin();
  65. }
  66. }
  67. /**
  68. * Command to download the Colorbox plugin.
  69. */
  70. function drush_colorbox_plugin() {
  71. $args = func_get_args();
  72. if (!empty($args[0])) {
  73. $path = $args[0];
  74. }
  75. else {
  76. $path = 'sites/all/libraries';
  77. }
  78. // Create the path if it does not exist.
  79. if (!is_dir($path)) {
  80. drush_op('mkdir', $path);
  81. drush_log(dt('Directory @path was created', array('@path' => $path)), 'notice');
  82. }
  83. // Set the directory to the download location.
  84. $olddir = getcwd();
  85. chdir($path);
  86. // Download the zip archive
  87. if ($filepath = drush_download_file(COLORBOX_DOWNLOAD_URI)) {
  88. $filename = basename($filepath);
  89. $dirname = COLORBOX_DOWNLOAD_PREFIX . basename($filepath, '.zip');
  90. // Remove any existing Colorbox plugin directory
  91. if (is_dir($dirname) || is_dir('colorbox')) {
  92. drush_delete_dir($dirname, TRUE);
  93. drush_delete_dir('colorbox', TRUE);
  94. drush_log(dt('A existing Colorbox plugin was deleted from @path', array('@path' => $path)), 'notice');
  95. }
  96. // Decompress the zip archive
  97. drush_tarball_extract($filename);
  98. // Change the directory name to "colorbox" if needed.
  99. if ($dirname != 'colorbox') {
  100. drush_move_dir($dirname, 'colorbox', TRUE);
  101. $dirname = 'colorbox';
  102. }
  103. }
  104. if (is_dir($dirname)) {
  105. drush_log(dt('Colorbox plugin has been installed in @path', array('@path' => $path)), 'success');
  106. }
  107. else {
  108. drush_log(dt('Drush was unable to install the Colorbox plugin to @path', array('@path' => $path)), 'error');
  109. }
  110. // Set working directory back to the previous working directory.
  111. chdir($olddir);
  112. }