Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(509)

Unified Diff: pkg/glob/lib/glob.dart

Issue 549633002: Add support for listing to the glob package. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Code review changes Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | pkg/glob/lib/src/ast.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/glob/lib/glob.dart
diff --git a/pkg/glob/lib/glob.dart b/pkg/glob/lib/glob.dart
index 1c7efda55b4bcff17a487d5e1d85f01d995d97e6..f80faa1647b15c2bfb6848e0b2819bcb9b4a7281 100644
--- a/pkg/glob/lib/glob.dart
+++ b/pkg/glob/lib/glob.dart
@@ -4,16 +4,19 @@
library glob;
+import 'dart:async';
+import 'dart:io';
+
import 'package:path/path.dart' as p;
import 'src/ast.dart';
+import 'src/list_tree.dart';
import 'src/parser.dart';
import 'src/utils.dart';
/// Regular expression used to quote globs.
final _quoteRegExp = new RegExp(r'[*{[?\\}\],\-()]');
-// TODO(nweiz): Add [list] and [listSync] methods.
/// A glob for matching and listing files and directories.
///
/// A glob matches an entire string as a path. Although the glob pattern uses
@@ -45,6 +48,8 @@ class Glob implements Pattern {
/// The parsed AST of the glob.
final AstNode _ast;
+ ListTree _listTree;
+
/// Whether [context]'s current directory is absolute.
bool get _contextIsAbsolute {
if (_contextIsAbsoluteCache == null) {
@@ -98,6 +103,47 @@ class Glob implements Pattern {
_ast = new Parser(pattern + (recursive ? "{,/**}" : ""), context)
.parse();
+ /// Lists all [FileSystemEntity]s beneath [root] that match the glob.
+ ///
+ /// This works much like [Directory.list], but it only lists directories that
+ /// could contain entities that match the glob. It provides no guarantees
+ /// about the order of the returned entities, although it does guarantee that
+ /// only one entity with a given path will be returned.
+ ///
+ /// [root] defaults to the current working directory.
+ ///
+ /// [followLinks] works the same as for [Directory.list].
+ Stream<FileSystemEntity> list({String root, bool followLinks: true}) {
+ if (context.style != p.style) {
+ throw new StateError("Can't list glob \"$this\"; it matches "
+ "${context.style} paths, but this platform uses ${p.style} paths.");
+ }
+
+ if (_listTree == null) _listTree = new ListTree(_ast);
+ return _listTree.list(root: root, followLinks: followLinks);
+ }
+
+ /// Synchronously lists all [FileSystemEntity]s beneath [root] that match the
+ /// glob.
+ ///
+ /// This works much like [Directory.listSync], but it only lists directories
+ /// that could contain entities that match the glob. It provides no guarantees
+ /// about the order of the returned entities, although it does guarantee that
+ /// only one entity with a given path will be returned.
+ ///
+ /// [root] defaults to the current working directory.
+ ///
+ /// [followLinks] works the same as for [Directory.list].
+ List<FileSystemEntity> listSync({String root, bool followLinks: true}) {
+ if (context.style != p.style) {
+ throw new StateError("Can't list glob \"$this\"; it matches "
+ "${context.style} paths, but this platform uses ${p.style} paths.");
+ }
+
+ if (_listTree == null) _listTree = new ListTree(_ast);
+ return _listTree.listSync(root: root, followLinks: followLinks);
+ }
+
/// Returns whether this glob matches [path].
bool matches(String path) => matchAsPrefix(path) != null;
« no previous file with comments | « no previous file | pkg/glob/lib/src/ast.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698