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

Side by Side Diff: pkg/glob/lib/glob.dart

Issue 506993004: Create a glob package. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 final _quoteRegExp = new RegExp(r'[*{[?\\}\],\-]');
Bob Nystrom 2014/08/27 00:42:04 Doc comment. Also, escape "()".
nweiz 2014/08/27 01:36:32 Done, although I'm ambivalent about rigorously doc
Bob Nystrom 2014/08/27 22:16:44 Never in my life have I found myself thinking, "yo
nweiz 2014/09/02 19:48:15 https://github.com/dart-lang/bleeding_edge/commit/
Bob Nystrom 2014/09/02 20:23:50 The person reviewing that code didn't seem to mind
14
15 // TODO(nweiz): Add [list] and [listSync] methods.
16 /// A glob for matching and listing files and directories.
17 ///
18 /// A glob matches an entire string as a path. Although the glob pattern uses
19 /// POSIX path syntax, it can match against POSIX, Windows, or URL paths. Which
Bob Nystrom 2014/08/27 00:42:04 This is confusing. Maybe: 'Although the glob patt
nweiz 2014/08/27 01:36:32 I think that's more confusing than my phrasing, es
Bob Nystrom 2014/08/27 22:16:44 How about then: "Although the glob pattern uses PO
nweiz 2014/09/02 19:48:15 Done.
20 /// path syntax it uses is based on the `context` parameter to [new Glob]; it
21 /// defaults to the current system's syntax.
22 ///
23 /// Paths are normalized before being matched against a glob, so for example the
24 /// glob `foo/bar` matches the path `foo/./bar`. A relative glob can match an
25 /// absolute path and vice versa; globs and paths are both interpreted as
26 /// relative to `context.current`, which defaults to the current working
27 /// directory.
28 ///
29 /// When used as a [Pattern], a glob will return either one or zero matches for
30 /// a string depending on whether the entire string matches the glob. These
31 /// matches don't currently have capture groups, although this may change in the
32 /// future.
33 class Glob implements Pattern {
34 /// The pattern used to create this glob.
35 final String pattern;
36
37 /// The context in which paths matched against this glob are interpreted.
38 final p.Context context;
39
40 /// Whether this glob recursively matches and lists entities.
Bob Nystrom 2014/08/27 00:42:04 It took me a while to figure out what this meant i
nweiz 2014/08/27 01:36:33 Done.
41 final bool recursive;
42
43 /// The parsed AST of the glob.
44 final AstNode _ast;
45
46 /// Whether [context]'s current directory is absolute.
47 bool get _contextIsAbsolute {
48 if (_contextIsAbsoluteCache == null) {
Bob Nystrom 2014/08/27 00:42:04 Why is this cached?
nweiz 2014/08/27 01:36:32 I want to minimize the amount of work that has to
Bob Nystrom 2014/08/27 22:16:44 That makes sense.
49 _contextIsAbsoluteCache = context.isAbsolute(context.current);
50 }
51 return _contextIsAbsoluteCache;
52 }
53 bool _contextIsAbsoluteCache;
54
55 /// Whether [pattern] could match absolute paths.
56 bool get _patternCouldBeAbsolute {
Bob Nystrom 2014/08/27 00:42:04 "could be" reads confusingly to me. I don't think
nweiz 2014/08/27 01:36:33 Done.
57 if (_patternCouldBeAbsoluteCache == null) {
58 _patternCouldBeAbsoluteCache = _ast.couldBeAbsolute;
59 }
60 return _patternCouldBeAbsoluteCache;
61 }
62 bool _patternCouldBeAbsoluteCache;
63
64 /// Whether [pattern] could match relative paths.
65 bool get _patternCouldBeRelative {
66 if (_patternCouldBeRelativeCache == null) {
67 _patternCouldBeRelativeCache = _ast.couldBeRelative;
68 }
69 return _patternCouldBeRelativeCache;
70 }
71 bool _patternCouldBeRelativeCache;
72
73 /// Returns [contents] with characters that are meaningful in globs
74 /// backslash-escaped.
75 static String quote(String contents) =>
76 contents.replaceAllMapped(_quoteRegExp, (match) => '\\${match[0]}');
77
78 /// Creates a new glob with [pattern].
79 ///
80 /// Paths matched against the glob are interpreted according to [context]. It
81 /// defaults to the system context.
82 ///
83 /// If [recursive] is true, this glob will match and list not only the files
84 /// and directories it explicitly lists, but anything beneath those as well.
85 Glob(String pattern, {p.Context context, bool recursive: false})
86 : this._(
87 pattern,
88 context == null ? p.context : context,
89 recursive);
90
91 // Internal constructor used to fake local variables for [context] and [ast].
92 Glob._(String pattern, p.Context context, bool recursive)
Bob Nystrom 2014/08/27 00:42:04 It might be simpler to make this take the ast too
nweiz 2014/08/27 01:36:32 I don't like making things factory constructors wh
93 : pattern = pattern,
94 context = context,
95 recursive = recursive,
96 _ast = new Parser(pattern + (recursive ? "{,/**}" : ""), context)
97 .parse();
98
99 /// Returns whether this glob matches [path].
100 bool matches(String path) => matchAsPrefix(path) != null;
101
102 Match matchAsPrefix(String path, [int start = 0]) {
103 // Globs are like anchored RegExps in that they only match entire paths, so
104 // if the match starts anywhere after the first character it can't succeed.
105 if (start != 0) return null;
106
107 if (_patternCouldBeAbsolute &&
108 (_contextIsAbsolute || context.isAbsolute(path))) {
109 var absolutePath = context.normalize(context.absolute(path));
110 if (_ast.matches(_toPosixPath(absolutePath))) {
111 return new GlobMatch(path, this);
112 }
113 }
114
115 if (_patternCouldBeRelative) {
116 var relativePath = context.relative(path);
117 if (_ast.matches(_toPosixPath(relativePath))) {
118 return new GlobMatch(path, this);
119 }
120 }
121
122 return null;
123 }
124
125 /// Returns [path] converted to the POSIX format that globs match against.
126 String _toPosixPath(String path) {
127 if (context.style == p.Style.windows) return path.replaceAll('\\', '/');
128 if (context.style == p.Style.url) return Uri.decodeFull(path);
129 return path;
130 }
131
132 Iterable<Match> allMatches(String path, [int start = 0]) {
133 var match = matchAsPrefix(path, start);
134 return match == null ? [] : [match];
135 }
136
137 String toString() => pattern;
138 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698