? drush_views_tag.patch
Index: drush_views.drush.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/drush_views/drush_views.drush.inc,v
retrieving revision 1.1.2.4
diff -u -p -r1.1.2.4 drush_views.drush.inc
--- drush_views.drush.inc	11 Oct 2009 17:29:39 -0000	1.1.2.4
+++ drush_views.drush.inc	8 Nov 2009 15:41:53 -0000
@@ -38,6 +38,10 @@ function drush_views_drush_help($section
      case 'drush:views delete':
        return dt("Usage: drush [options] views delete <views>\n\n"
                  ."Delete the specified views. Use 'ALL' to delete all normal/overridden views.");
+     case 'drush:views tag':
+       return dt("Usage: drush [options] views tag [--names] \n"
+                ."Usage: drush [options] views tag list <tags>\n"
+                ."Usage: drush [options] views tag export [--module=<name>] <tags>");
   }
 }
 
@@ -62,6 +66,17 @@ function drush_views_drush_command() {
     'callback' => 'drush_views_delete',
     'description' => 'Delete or revert a view.',
   );
+  $items['views tag'] = array(
+    'callback' => 'drush_views_tag',
+    'description' => 'List and export views by tag.',
+    'drupal dependencies' => array('views'),
+    'aliases' => array('vt'),
+    'examples' => array(
+      'views tag' => 'List all tags',
+      'views tag list default' => 'List views for tag "default".',
+      'views tag export your_module > your_module/includes/your_module.views_default.inc' => 'Export all views of tag "your_module" and pipe it to the modules default views file. This is similar to doing a bulk export of all views from a tag.',
+    ),
+  );
   return $items;
 }
 
@@ -258,3 +273,69 @@ function drush_views_delete() {
     }
   }
 }
+
+/**
+ * Command callbacks: views tag, views tag list and views tag export.
+ */
+function drush_views_tag() {
+  $args = func_get_args();
+  $output = '';
+  // Default to 'list' if no arguments are given. If arguments are given, the
+  // first is a command (export or list).
+  $command = count($args) ? array_shift($args) : 'list';
+
+  if (!in_array($command, array('list', 'export'))) {
+    drush_set_error(dt("Command '@command' not available. See 'drush help views tag'.", array('@command' => $command)));
+    return;
+  }
+
+  // List view tags.
+  if ($command == 'list') {
+    $tags = array();
+    foreach (views_get_all_views() as $name => $view) {
+      // Create an array with tags and their views, and filter by tags if provided.
+      if (!empty($view->tag)  && (count($args) < 1 || in_array($view->tag, $args))) {
+        $tags[$view->tag][] = $name;
+      }
+    }
+    // Display a tag's views if --names is set or if any tags were specified. 
+    if (drush_get_option(array('n', 'names')) || count($args)) {
+      foreach ($tags as $tag => $names) {
+        $output .= "$tag:\t" . implode($names, ", ") . "\n";
+      }
+    }
+    else {
+      $output .= implode(array_keys($tags), "\n");
+      $output .= "\n";
+    }
+  }
+
+  // Export views by tag.
+  elseif ($command == 'export') {
+    if (!count($args)) {
+      drush_set_error(dt('No tags specified.'));
+      return;
+    }
+
+    $views = array();
+    foreach (views_get_all_views() as $name => $view) {
+      if (!empty($view->tag) && in_array($view->tag, $args)) {
+        $views[$name] = $name;
+      }
+    }
+
+    if (!count($views)) {
+      drush_set_error(dt('No views found.'));
+      return;
+    }
+    
+    $m = drush_get_option(array('module'));
+    // Use first tag as module name if not explicitly specified.
+    $module = $m ? $m : $args[0];
+    // Use the same output that is used for Views bulk export.
+    $output .= "<?php\n";
+    $output .= module_invoke('views', 'views_exportables', 'export', $views, $module);
+  }
+  // Print to STDOUT.
+  print $output;
+}
