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'; | 7 import 'dart:async'; |
8 import 'dart:io'; | 8 import 'dart:io'; |
9 | 9 |
10 import 'package:path/path.dart' as p; | 10 import 'package:path/path.dart' as p; |
(...skipping 27 matching lines...) Expand all Loading... | |
38 /// The pattern used to create this glob. | 38 /// The pattern used to create this glob. |
39 final String pattern; | 39 final String pattern; |
40 | 40 |
41 /// The context in which paths matched against this glob are interpreted. | 41 /// The context in which paths matched against this glob are interpreted. |
42 final p.Context context; | 42 final p.Context context; |
43 | 43 |
44 /// 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 |
45 /// contained within a directory that matches. | 45 /// contained within a directory that matches. |
46 final bool recursive; | 46 final bool recursive; |
47 | 47 |
48 /// Whether the glob matches paths case-sensitively. | |
49 bool get caseSensitive => _ast.caseSensitive; | |
50 | |
48 /// The parsed AST of the glob. | 51 /// The parsed AST of the glob. |
49 final AstNode _ast; | 52 final AstNode _ast; |
50 | 53 |
51 ListTree _listTree; | 54 ListTree _listTree; |
52 | 55 |
53 /// Whether [context]'s current directory is absolute. | 56 /// Whether [context]'s current directory is absolute. |
54 bool get _contextIsAbsolute { | 57 bool get _contextIsAbsolute { |
55 if (_contextIsAbsoluteCache == null) { | 58 if (_contextIsAbsoluteCache == null) { |
56 _contextIsAbsoluteCache = context.isAbsolute(context.current); | 59 _contextIsAbsoluteCache = context.isAbsolute(context.current); |
57 } | 60 } |
(...skipping 23 matching lines...) Expand all Loading... | |
81 /// backslash-escaped. | 84 /// backslash-escaped. |
82 static String quote(String contents) => | 85 static String quote(String contents) => |
83 contents.replaceAllMapped(_quoteRegExp, (match) => '\\${match[0]}'); | 86 contents.replaceAllMapped(_quoteRegExp, (match) => '\\${match[0]}'); |
84 | 87 |
85 /// Creates a new glob with [pattern]. | 88 /// Creates a new glob with [pattern]. |
86 /// | 89 /// |
87 /// Paths matched against the glob are interpreted according to [context]. It | 90 /// Paths matched against the glob are interpreted according to [context]. It |
88 /// defaults to the system context. | 91 /// defaults to the system context. |
89 /// | 92 /// |
90 /// If [recursive] is true, this glob will match and list not only the files | 93 /// If [recursive] is true, this glob will match and list not only the files |
91 /// and directories it explicitly lists, but anything beneath those as well. | 94 /// and directories it explicitly lists, but anything beneath those as well. |
Bob Nystrom
2015/12/02 01:01:43
"lists" -> "matches"?
nweiz
2015/12/02 01:33:01
Done.
| |
92 Glob(String pattern, {p.Context context, bool recursive: false}) | 95 /// |
93 : this._( | 96 /// If [caseSensitive] is true, this glob will match and list only files whose |
Bob Nystrom
2015/12/02 01:01:43
"will match" -> "matches", etc.
nweiz
2015/12/02 01:33:01
Done.
| |
94 pattern, | 97 /// case matches that of the characters in the glob. Otherwise, it will match |
95 context == null ? p.context : context, | 98 /// regardless of case. This defaults to `false` when [context] is Windows and |
96 recursive); | 99 /// `true` otherwise. |
100 factory Glob(String pattern, {p.Context context, bool recursive: false, | |
101 bool caseSensitive}) { | |
102 context ??= p.context; | |
103 caseSensitive ??= context.style == p.Style.windows ? false : true; | |
104 if (recursive) pattern += "{,/**}"; | |
97 | 105 |
98 // Internal constructor used to fake local variables for [context] and [ast]. | 106 var parser = new Parser(pattern, context, caseSensitive: caseSensitive); |
99 Glob._(String pattern, p.Context context, bool recursive) | 107 return new Glob._(pattern, context, parser.parse(), recursive); |
100 : pattern = pattern, | 108 } |
101 context = context, | 109 |
102 recursive = recursive, | 110 Glob._(this.pattern, this.context, this._ast, this.recursive); |
103 _ast = new Parser(pattern + (recursive ? "{,/**}" : ""), context) | |
104 .parse(); | |
105 | 111 |
106 /// Lists all [FileSystemEntity]s beneath [root] that match the glob. | 112 /// Lists all [FileSystemEntity]s beneath [root] that match the glob. |
107 /// | 113 /// |
108 /// This works much like [Directory.list], but it only lists directories that | 114 /// This works much like [Directory.list], but it only lists directories that |
109 /// could contain entities that match the glob. It provides no guarantees | 115 /// could contain entities that match the glob. It provides no guarantees |
110 /// about the order of the returned entities, although it does guarantee that | 116 /// about the order of the returned entities, although it does guarantee that |
111 /// only one entity with a given path will be returned. | 117 /// only one entity with a given path will be returned. |
112 /// | 118 /// |
113 /// [root] defaults to the current working directory. | 119 /// [root] defaults to the current working directory. |
114 /// | 120 /// |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
170 return null; | 176 return null; |
171 } | 177 } |
172 | 178 |
173 Iterable<Match> allMatches(String path, [int start = 0]) { | 179 Iterable<Match> allMatches(String path, [int start = 0]) { |
174 var match = matchAsPrefix(path, start); | 180 var match = matchAsPrefix(path, start); |
175 return match == null ? [] : [match]; | 181 return match == null ? [] : [match]; |
176 } | 182 } |
177 | 183 |
178 String toString() => pattern; | 184 String toString() => pattern; |
179 } | 185 } |
OLD | NEW |