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