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

Unified 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, 4 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 side-by-side diff with in-line comments
Download patch
Index: pkg/glob/lib/glob.dart
diff --git a/pkg/glob/lib/glob.dart b/pkg/glob/lib/glob.dart
new file mode 100644
index 0000000000000000000000000000000000000000..10d69c04eb23e9466ac75b048a261997c1030443
--- /dev/null
+++ b/pkg/glob/lib/glob.dart
@@ -0,0 +1,138 @@
+// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+library glob;
+
+import 'package:path/path.dart' as p;
+
+import 'src/ast.dart';
+import 'src/parser.dart';
+import 'src/utils.dart';
+
+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
+
+// TODO(nweiz): Add [list] and [listSync] methods.
+/// A glob for matching and listing files and directories.
+///
+/// A glob matches an entire string as a path. Although the glob pattern uses
+/// 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.
+/// path syntax it uses is based on the `context` parameter to [new Glob]; it
+/// defaults to the current system's syntax.
+///
+/// Paths are normalized before being matched against a glob, so for example the
+/// glob `foo/bar` matches the path `foo/./bar`. A relative glob can match an
+/// absolute path and vice versa; globs and paths are both interpreted as
+/// relative to `context.current`, which defaults to the current working
+/// directory.
+///
+/// When used as a [Pattern], a glob will return either one or zero matches for
+/// a string depending on whether the entire string matches the glob. These
+/// matches don't currently have capture groups, although this may change in the
+/// future.
+class Glob implements Pattern {
+ /// The pattern used to create this glob.
+ final String pattern;
+
+ /// The context in which paths matched against this glob are interpreted.
+ final p.Context context;
+
+ /// 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.
+ final bool recursive;
+
+ /// The parsed AST of the glob.
+ final AstNode _ast;
+
+ /// Whether [context]'s current directory is absolute.
+ bool get _contextIsAbsolute {
+ 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.
+ _contextIsAbsoluteCache = context.isAbsolute(context.current);
+ }
+ return _contextIsAbsoluteCache;
+ }
+ bool _contextIsAbsoluteCache;
+
+ /// Whether [pattern] could match absolute paths.
+ 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.
+ if (_patternCouldBeAbsoluteCache == null) {
+ _patternCouldBeAbsoluteCache = _ast.couldBeAbsolute;
+ }
+ return _patternCouldBeAbsoluteCache;
+ }
+ bool _patternCouldBeAbsoluteCache;
+
+ /// Whether [pattern] could match relative paths.
+ bool get _patternCouldBeRelative {
+ if (_patternCouldBeRelativeCache == null) {
+ _patternCouldBeRelativeCache = _ast.couldBeRelative;
+ }
+ return _patternCouldBeRelativeCache;
+ }
+ bool _patternCouldBeRelativeCache;
+
+ /// Returns [contents] with characters that are meaningful in globs
+ /// backslash-escaped.
+ static String quote(String contents) =>
+ contents.replaceAllMapped(_quoteRegExp, (match) => '\\${match[0]}');
+
+ /// Creates a new glob with [pattern].
+ ///
+ /// Paths matched against the glob are interpreted according to [context]. It
+ /// defaults to the system context.
+ ///
+ /// If [recursive] is true, this glob will match and list not only the files
+ /// and directories it explicitly lists, but anything beneath those as well.
+ Glob(String pattern, {p.Context context, bool recursive: false})
+ : this._(
+ pattern,
+ context == null ? p.context : context,
+ recursive);
+
+ // Internal constructor used to fake local variables for [context] and [ast].
+ 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
+ : pattern = pattern,
+ context = context,
+ recursive = recursive,
+ _ast = new Parser(pattern + (recursive ? "{,/**}" : ""), context)
+ .parse();
+
+ /// Returns whether this glob matches [path].
+ bool matches(String path) => matchAsPrefix(path) != null;
+
+ Match matchAsPrefix(String path, [int start = 0]) {
+ // Globs are like anchored RegExps in that they only match entire paths, so
+ // if the match starts anywhere after the first character it can't succeed.
+ if (start != 0) return null;
+
+ if (_patternCouldBeAbsolute &&
+ (_contextIsAbsolute || context.isAbsolute(path))) {
+ var absolutePath = context.normalize(context.absolute(path));
+ if (_ast.matches(_toPosixPath(absolutePath))) {
+ return new GlobMatch(path, this);
+ }
+ }
+
+ if (_patternCouldBeRelative) {
+ var relativePath = context.relative(path);
+ if (_ast.matches(_toPosixPath(relativePath))) {
+ return new GlobMatch(path, this);
+ }
+ }
+
+ return null;
+ }
+
+ /// Returns [path] converted to the POSIX format that globs match against.
+ String _toPosixPath(String path) {
+ if (context.style == p.Style.windows) return path.replaceAll('\\', '/');
+ if (context.style == p.Style.url) return Uri.decodeFull(path);
+ return path;
+ }
+
+ Iterable<Match> allMatches(String path, [int start = 0]) {
+ var match = matchAsPrefix(path, start);
+ return match == null ? [] : [match];
+ }
+
+ String toString() => pattern;
+}

Powered by Google App Engine
This is Rietveld 408576698