colorbox.drush.inc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. // Download colorbox plugin only if path is writable.
  84. if (is_writable($path)) {
  85. // Set the directory to the download location.
  86. $olddir = getcwd();
  87. chdir($path);
  88. // Download the zip archive
  89. if ($filepath = drush_download_file(COLORBOX_DOWNLOAD_URI)) {
  90. $filename = basename($filepath);
  91. $dirname = COLORBOX_DOWNLOAD_PREFIX . basename($filepath, '.zip');
  92. // Remove any existing Colorbox plugin directory.
  93. if (is_dir($dirname) || is_dir('colorbox')) {
  94. drush_delete_dir($dirname, TRUE);
  95. drush_delete_dir('colorbox', TRUE);
  96. drush_log(dt('A existing Colorbox plugin was deleted from @path', array('@path' => $path)), 'notice');
  97. }
  98. // Decompress the zip archive
  99. drush_tarball_extract($filename);
  100. // Change the directory name to "colorbox" if needed.
  101. if ($dirname != 'colorbox') {
  102. drush_move_dir($dirname, 'colorbox', TRUE);
  103. $dirname = 'colorbox';
  104. }
  105. }
  106. if (is_dir($dirname)) {
  107. drush_log(dt('Colorbox plugin has been installed in @path', array('@path' => $path)), 'success');
  108. }
  109. else {
  110. drush_log(dt('Drush was unable to install the Colorbox plugin to @path', array('@path' => $path)), 'error');
  111. }
  112. // Set working directory back to the previous working directory.
  113. chdir($olddir);
  114. }
  115. else {
  116. drush_log(dt('Drush was unable to install the Colorbox plugin because @path is not writable. If you enable the colorbox module before you install the plugin library, you may find that colorbox does not work until you reinstall the colorbox module.', array('@path' => $path)), 'warning');
  117. }
  118. }