uuid_features.drush.inc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * @file
  4. * uuid_features module drush integration.
  5. */
  6. /**
  7. * Implements hook_drush_command().
  8. *
  9. * @See drush_parse_command() for a list of recognized keys.
  10. *
  11. * @return
  12. * An associative array describing your command(s).
  13. */
  14. function uuid_features_drush_command() {
  15. $items = array();
  16. $items['uuid-features-update-files'] = array(
  17. 'callback' => 'uuid_features_command_update_files',
  18. 'description' => "Update files for features modules that use the uuid_file component.",
  19. 'arguments' => array(
  20. 'feature' => 'Feature name to export.',
  21. ),
  22. 'drupal dependencies' => array('features', 'uuid', 'uuid_features', 'filefield'),
  23. 'aliases' => array('ufuf'),
  24. );
  25. return $items;
  26. }
  27. /**
  28. * Implements hook_drush_help().
  29. */
  30. function uuid_features_drush_help($section) {
  31. switch ($section) {
  32. case 'drush:features':
  33. return dt("List all the available features for your site.");
  34. }
  35. }
  36. /**
  37. * Update files for features modules that use the uuid_file component.
  38. */
  39. function uuid_features_command_update_files($feature = NULL) {
  40. if ($args = func_get_args()) {
  41. foreach ($args as $module) {
  42. if (($feature = feature_load($module, TRUE)) && module_exists($module)) {
  43. if (!empty($feature->info['features']['uuid_file'])) {
  44. $files = $feature->info['features']['uuid_file'];
  45. $dest = drupal_get_path('module', $module) . '/uuid_file';
  46. file_prepare_directory($dest, FILE_CREATE_DIRECTORY);
  47. foreach ($files as $uuid) {
  48. _uuid_features_drush_update_file($module, $uuid);
  49. }
  50. }
  51. }
  52. else if ($feature) {
  53. _features_drush_set_error($module, 'FEATURES_FEATURE_NOT_ENABLED');
  54. }
  55. else {
  56. _features_drush_set_error($module);
  57. }
  58. }
  59. }
  60. else {
  61. // By default just show contexts that are available.
  62. $rows = array(array(dt('Available features')));
  63. foreach (features_get_features(NULL, TRUE) as $name => $info) {
  64. $rows[] = array($name);
  65. }
  66. drush_print_table($rows, TRUE);
  67. }
  68. }
  69. /**
  70. * Copy the file identified by $uuid into the feature specified by $module.
  71. *
  72. * @param string $module
  73. * @param string $uuid
  74. */
  75. function _uuid_features_drush_update_file($module, $uuid) {
  76. $fid = uuid_get_serial_id('files', 'fid', $uuid);
  77. if (empty($fid) || !($file = field_file_load($fid))) {
  78. drush_set_error('UUID_FILE_NOT_FOUND', dt('The file specified by %uuid was not found.', array('%uuid' => $uuid)));
  79. }
  80. $root = drush_get_option(array('r', 'root'), drush_locate_root());
  81. if ($root) {
  82. $directory = drupal_get_path('module', $module) . '/uuid_file';
  83. if (!is_dir($directory)) {
  84. drush_op('mkdir', $directory);
  85. }
  86. $source = $file['filepath'];
  87. $file_parts = explode('.', $file['filepath']);
  88. $extension = array_pop($file_parts);
  89. $destination = $directory . '/' . $uuid . '.' . $extension;
  90. drush_op('copy', $source, $destination);
  91. drush_log(dt("Updated file: !uuid - !filename", array('!uuid' => $uuid, '!filename' => basename($destination))), 'ok');
  92. }
  93. else {
  94. drush_die(dt('Couldn\'t locate site root'));
  95. }
  96. }