OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library glob; |
| 6 |
| 7 import 'package:path/path.dart' as p; |
| 8 |
| 9 import 'src/ast.dart'; |
| 10 import 'src/parser.dart'; |
| 11 import 'src/utils.dart'; |
| 12 |
| 13 /// Regular expression used to quote globs. |
| 14 final _quoteRegExp = new RegExp(r'[*{[?\\}\],\-()]'); |
| 15 |
| 16 // TODO(nweiz): Add [list] and [listSync] methods. |
| 17 /// A glob for matching and listing files and directories. |
| 18 /// |
| 19 /// 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 |
| 21 /// it expects paths to use is based on the `context` parameter to [new Glob]; |
| 22 /// it defaults to the current system's syntax. |
| 23 /// |
| 24 /// 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 |
| 26 /// absolute path and vice versa; globs and paths are both interpreted as |
| 27 /// relative to `context.current`, which defaults to the current working |
| 28 /// directory. |
| 29 /// |
| 30 /// When used as a [Pattern], a glob will return either one or zero matches for |
| 31 /// a string depending on whether the entire string matches the glob. These |
| 32 /// matches don't currently have capture groups, although this may change in the |
| 33 /// future. |
| 34 class Glob implements Pattern { |
| 35 /// The pattern used to create this glob. |
| 36 final String pattern; |
| 37 |
| 38 /// The context in which paths matched against this glob are interpreted. |
| 39 final p.Context context; |
| 40 |
| 41 /// If true, a path matches if it matches the glob itself or is recursively |
| 42 /// contained within a directory that matches. |
| 43 final bool recursive; |
| 44 |
| 45 /// The parsed AST of the glob. |
| 46 final AstNode _ast; |
| 47 |
| 48 /// Whether [context]'s current directory is absolute. |
| 49 bool get _contextIsAbsolute { |
| 50 if (_contextIsAbsoluteCache == null) { |
| 51 _contextIsAbsoluteCache = context.isAbsolute(context.current); |
| 52 } |
| 53 return _contextIsAbsoluteCache; |
| 54 } |
| 55 bool _contextIsAbsoluteCache; |
| 56 |
| 57 /// Whether [pattern] could match absolute paths. |
| 58 bool get _patternCanMatchAbsolute { |
| 59 if (_patternCanMatchAbsoluteCache == null) { |
| 60 _patternCanMatchAbsoluteCache = _ast.canMatchAbsolute; |
| 61 } |
| 62 return _patternCanMatchAbsoluteCache; |
| 63 } |
| 64 bool _patternCanMatchAbsoluteCache; |
| 65 |
| 66 /// Whether [pattern] could match relative paths. |
| 67 bool get _patternCanMatchRelative { |
| 68 if (_patternCanMatchRelativeCache == null) { |
| 69 _patternCanMatchRelativeCache = _ast.canMatchRelative; |
| 70 } |
| 71 return _patternCanMatchRelativeCache; |
| 72 } |
| 73 bool _patternCanMatchRelativeCache; |
| 74 |
| 75 /// Returns [contents] with characters that are meaningful in globs |
| 76 /// backslash-escaped. |
| 77 static String quote(String contents) => |
| 78 contents.replaceAllMapped(_quoteRegExp, (match) => '\\${match[0]}'); |
| 79 |
| 80 /// Creates a new glob with [pattern]. |
| 81 /// |
| 82 /// Paths matched against the glob are interpreted according to [context]. It |
| 83 /// defaults to the system context. |
| 84 /// |
| 85 /// If [recursive] is true, this glob will match and list not only the files |
| 86 /// and directories it explicitly lists, but anything beneath those as well. |
| 87 Glob(String pattern, {p.Context context, bool recursive: false}) |
| 88 : this._( |
| 89 pattern, |
| 90 context == null ? p.context : context, |
| 91 recursive); |
| 92 |
| 93 // Internal constructor used to fake local variables for [context] and [ast]. |
| 94 Glob._(String pattern, p.Context context, bool recursive) |
| 95 : pattern = pattern, |
| 96 context = context, |
| 97 recursive = recursive, |
| 98 _ast = new Parser(pattern + (recursive ? "{,/**}" : ""), context) |
| 99 .parse(); |
| 100 |
| 101 /// Returns whether this glob matches [path]. |
| 102 bool matches(String path) => matchAsPrefix(path) != null; |
| 103 |
| 104 Match matchAsPrefix(String path, [int start = 0]) { |
| 105 // 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. |
| 107 if (start != 0) return null; |
| 108 |
| 109 if (_patternCanMatchAbsolute && |
| 110 (_contextIsAbsolute || context.isAbsolute(path))) { |
| 111 var absolutePath = context.normalize(context.absolute(path)); |
| 112 if (_ast.matches(_toPosixPath(absolutePath))) { |
| 113 return new GlobMatch(path, this); |
| 114 } |
| 115 } |
| 116 |
| 117 if (_patternCanMatchRelative) { |
| 118 var relativePath = context.relative(path); |
| 119 if (_ast.matches(_toPosixPath(relativePath))) { |
| 120 return new GlobMatch(path, this); |
| 121 } |
| 122 } |
| 123 |
| 124 return null; |
| 125 } |
| 126 |
| 127 /// Returns [path] converted to the POSIX format that globs match against. |
| 128 String _toPosixPath(String path) { |
| 129 if (context.style == p.Style.windows) return path.replaceAll('\\', '/'); |
| 130 if (context.style == p.Style.url) return Uri.decodeFull(path); |
| 131 return path; |
| 132 } |
| 133 |
| 134 Iterable<Match> allMatches(String path, [int start = 0]) { |
| 135 var match = matchAsPrefix(path, start); |
| 136 return match == null ? [] : [match]; |
| 137 } |
| 138 |
| 139 String toString() => pattern; |
| 140 } |
OLD | NEW |