timefield.drush.inc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * @file
  4. * drush integration for timefield.
  5. */
  6. /**
  7. * The timefield plugin URI.
  8. */
  9. define('TIMEFIELD_DOWNLOAD_URI', 'http://fgelinas.com/code/timepicker/releases/jquery-ui-timepicker-0.2.9.zip');
  10. /**
  11. * Implements hook_drush_command().
  12. */
  13. function timefield_drush_command() {
  14. $items = array();
  15. $items['timefield-plugin'] = array(
  16. 'callback' => 'drush_timefield_plugin',
  17. 'description' => dt("Downloads the jQuery UI Timepicker plugin."),
  18. 'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
  19. 'arguments' => array(
  20. 'path' => dt('Optional. A path where to install the timefield plugin. If omitted Drush will use the default location.'),
  21. ),
  22. 'aliases' => array('timepicker-plugin'),
  23. );
  24. return $items;
  25. }
  26. /**
  27. * Implements hook_drush_help().
  28. */
  29. function timefield_drush_help($section) {
  30. switch ($section) {
  31. case 'drush:timefield-plugin':
  32. return dt("Downloads the jQuery UI Timepicker plugin, default location is sites/all/libraries.");
  33. }
  34. }
  35. /**
  36. * Command to download the timefield plugin.
  37. */
  38. function drush_timefield_plugin() {
  39. if (!drush_shell_exec('type unzip')) {
  40. return drush_set_error(dt('Missing dependency: unzip. Install it before using this command.'));
  41. }
  42. $args = func_get_args();
  43. if (!empty($args[0])) {
  44. $path = $args[0];
  45. }
  46. else {
  47. $path = 'sites/all/libraries';
  48. }
  49. // Create the path if it does not exist.
  50. if (!is_dir($path)) {
  51. drush_op('mkdir', $path);
  52. drush_log(dt('Directory @path was created', array('@path' => $path)), 'notice');
  53. }
  54. // Set the directory to the download location.
  55. $olddir = getcwd();
  56. chdir($path);
  57. $filename = basename(TIMEFIELD_DOWNLOAD_URI);
  58. $dirname = 'jquery.timepicker';
  59. // Remove any existing timepicker plugin directory.
  60. if (is_dir($dirname)) {
  61. drush_log(dt('An existing timepicker plugin was overwritten at @path', array('@path' => $path)), 'notice');
  62. }
  63. // Remove any existing timefield plugin zip archive.
  64. if (is_file($filename)) {
  65. drush_op('unlink', $filename);
  66. }
  67. // Download the zip archive.
  68. if (!drush_shell_exec('wget ' . TIMEFIELD_DOWNLOAD_URI)) {
  69. drush_shell_exec('curl -O ' . TIMEFIELD_DOWNLOAD_URI);
  70. }
  71. if (is_file($filename)) {
  72. // Decompress the zip archive.
  73. drush_shell_exec('unzip -qq -o ' . $filename . ' -d ' . $dirname);
  74. // Remove the zip archive.
  75. drush_op('unlink', $filename);
  76. }
  77. // Set working directory back to the previous working directory.
  78. chdir($olddir);
  79. if (is_dir($path . '/' . $dirname)) {
  80. drush_log(dt('Timepicker plugin has been downloaded to @path', array('@path' => $path)), 'success');
  81. }
  82. else {
  83. drush_log(dt('Drush was unable to download the Timepicker plugin to @path', array('@path' => $path)), 'error');
  84. }
  85. }