OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library glob; | 5 library glob; |
6 | 6 |
| 7 import 'dart:async'; |
| 8 import 'dart:io'; |
| 9 |
7 import 'package:path/path.dart' as p; | 10 import 'package:path/path.dart' as p; |
8 | 11 |
9 import 'src/ast.dart'; | 12 import 'src/ast.dart'; |
| 13 import 'src/list_tree.dart'; |
10 import 'src/parser.dart'; | 14 import 'src/parser.dart'; |
11 import 'src/utils.dart'; | 15 import 'src/utils.dart'; |
12 | 16 |
13 /// Regular expression used to quote globs. | 17 /// Regular expression used to quote globs. |
14 final _quoteRegExp = new RegExp(r'[*{[?\\}\],\-()]'); | 18 final _quoteRegExp = new RegExp(r'[*{[?\\}\],\-()]'); |
15 | 19 |
16 // TODO(nweiz): Add [list] and [listSync] methods. | |
17 /// A glob for matching and listing files and directories. | 20 /// A glob for matching and listing files and directories. |
18 /// | 21 /// |
19 /// A glob matches an entire string as a path. Although the glob pattern uses | 22 /// A glob matches an entire string as a path. Although the glob pattern uses |
20 /// POSIX syntax, it can match against POSIX, Windows, or URL paths. The format | 23 /// POSIX syntax, it can match against POSIX, Windows, or URL paths. The format |
21 /// it expects paths to use is based on the `context` parameter to [new Glob]; | 24 /// it expects paths to use is based on the `context` parameter to [new Glob]; |
22 /// it defaults to the current system's syntax. | 25 /// it defaults to the current system's syntax. |
23 /// | 26 /// |
24 /// Paths are normalized before being matched against a glob, so for example the | 27 /// Paths are normalized before being matched against a glob, so for example the |
25 /// glob `foo/bar` matches the path `foo/./bar`. A relative glob can match an | 28 /// glob `foo/bar` matches the path `foo/./bar`. A relative glob can match an |
26 /// absolute path and vice versa; globs and paths are both interpreted as | 29 /// absolute path and vice versa; globs and paths are both interpreted as |
(...skipping 11 matching lines...) Expand all Loading... |
38 /// The context in which paths matched against this glob are interpreted. | 41 /// The context in which paths matched against this glob are interpreted. |
39 final p.Context context; | 42 final p.Context context; |
40 | 43 |
41 /// If true, a path matches if it matches the glob itself or is recursively | 44 /// If true, a path matches if it matches the glob itself or is recursively |
42 /// contained within a directory that matches. | 45 /// contained within a directory that matches. |
43 final bool recursive; | 46 final bool recursive; |
44 | 47 |
45 /// The parsed AST of the glob. | 48 /// The parsed AST of the glob. |
46 final AstNode _ast; | 49 final AstNode _ast; |
47 | 50 |
| 51 ListTree _listTree; |
| 52 |
48 /// Whether [context]'s current directory is absolute. | 53 /// Whether [context]'s current directory is absolute. |
49 bool get _contextIsAbsolute { | 54 bool get _contextIsAbsolute { |
50 if (_contextIsAbsoluteCache == null) { | 55 if (_contextIsAbsoluteCache == null) { |
51 _contextIsAbsoluteCache = context.isAbsolute(context.current); | 56 _contextIsAbsoluteCache = context.isAbsolute(context.current); |
52 } | 57 } |
53 return _contextIsAbsoluteCache; | 58 return _contextIsAbsoluteCache; |
54 } | 59 } |
55 bool _contextIsAbsoluteCache; | 60 bool _contextIsAbsoluteCache; |
56 | 61 |
57 /// Whether [pattern] could match absolute paths. | 62 /// Whether [pattern] could match absolute paths. |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 recursive); | 96 recursive); |
92 | 97 |
93 // Internal constructor used to fake local variables for [context] and [ast]. | 98 // Internal constructor used to fake local variables for [context] and [ast]. |
94 Glob._(String pattern, p.Context context, bool recursive) | 99 Glob._(String pattern, p.Context context, bool recursive) |
95 : pattern = pattern, | 100 : pattern = pattern, |
96 context = context, | 101 context = context, |
97 recursive = recursive, | 102 recursive = recursive, |
98 _ast = new Parser(pattern + (recursive ? "{,/**}" : ""), context) | 103 _ast = new Parser(pattern + (recursive ? "{,/**}" : ""), context) |
99 .parse(); | 104 .parse(); |
100 | 105 |
| 106 /// Lists all [FileSystemEntity]s beneath [root] that match the glob. |
| 107 /// |
| 108 /// This works much like [Directory.list], but it only lists directories that |
| 109 /// could contain entities that match the glob. It provides no guarantees |
| 110 /// about the order of the returned entities, although it does guarantee that |
| 111 /// only one entity with a given path will be returned. |
| 112 /// |
| 113 /// [root] defaults to the current working directory. |
| 114 /// |
| 115 /// [followLinks] works the same as for [Directory.list]. |
| 116 Stream<FileSystemEntity> list({String root, bool followLinks: true}) { |
| 117 if (context.style != p.style) { |
| 118 throw new StateError("Can't list glob \"$this\"; it matches " |
| 119 "${context.style} paths, but this platform uses ${p.style} paths."); |
| 120 } |
| 121 |
| 122 if (_listTree == null) _listTree = new ListTree(_ast); |
| 123 return _listTree.list(root: root, followLinks: followLinks); |
| 124 } |
| 125 |
| 126 /// Synchronously lists all [FileSystemEntity]s beneath [root] that match the |
| 127 /// glob. |
| 128 /// |
| 129 /// This works much like [Directory.listSync], but it only lists directories |
| 130 /// that could contain entities that match the glob. It provides no guarantees |
| 131 /// about the order of the returned entities, although it does guarantee that |
| 132 /// only one entity with a given path will be returned. |
| 133 /// |
| 134 /// [root] defaults to the current working directory. |
| 135 /// |
| 136 /// [followLinks] works the same as for [Directory.list]. |
| 137 List<FileSystemEntity> listSync({String root, bool followLinks: true}) { |
| 138 if (context.style != p.style) { |
| 139 throw new StateError("Can't list glob \"$this\"; it matches " |
| 140 "${context.style} paths, but this platform uses ${p.style} paths."); |
| 141 } |
| 142 |
| 143 if (_listTree == null) _listTree = new ListTree(_ast); |
| 144 return _listTree.listSync(root: root, followLinks: followLinks); |
| 145 } |
| 146 |
101 /// Returns whether this glob matches [path]. | 147 /// Returns whether this glob matches [path]. |
102 bool matches(String path) => matchAsPrefix(path) != null; | 148 bool matches(String path) => matchAsPrefix(path) != null; |
103 | 149 |
104 Match matchAsPrefix(String path, [int start = 0]) { | 150 Match matchAsPrefix(String path, [int start = 0]) { |
105 // Globs are like anchored RegExps in that they only match entire paths, so | 151 // Globs are like anchored RegExps in that they only match entire paths, so |
106 // if the match starts anywhere after the first character it can't succeed. | 152 // if the match starts anywhere after the first character it can't succeed. |
107 if (start != 0) return null; | 153 if (start != 0) return null; |
108 | 154 |
109 if (_patternCanMatchAbsolute && | 155 if (_patternCanMatchAbsolute && |
110 (_contextIsAbsolute || context.isAbsolute(path))) { | 156 (_contextIsAbsolute || context.isAbsolute(path))) { |
(...skipping 20 matching lines...) Expand all Loading... |
131 return path; | 177 return path; |
132 } | 178 } |
133 | 179 |
134 Iterable<Match> allMatches(String path, [int start = 0]) { | 180 Iterable<Match> allMatches(String path, [int start = 0]) { |
135 var match = matchAsPrefix(path, start); | 181 var match = matchAsPrefix(path, start); |
136 return match == null ? [] : [match]; | 182 return match == null ? [] : [match]; |
137 } | 183 } |
138 | 184 |
139 String toString() => pattern; | 185 String toString() => pattern; |
140 } | 186 } |
OLD | NEW |