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

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

Issue 1400473008: Roll Observatory packages and add a roll script (Closed) Base URL: git@github.com:dart-lang/observatory_pub_packages.git@master
Patch Set: Created 5 years, 2 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
« no previous file with comments | « dart_style/pubspec.yaml ('k') | glob/lib/src/ast.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 'dart:async';
8 import 'dart:io';
9
10 import 'package:path/path.dart' as p;
11
12 import 'src/ast.dart';
13 import 'src/list_tree.dart';
14 import 'src/parser.dart';
15 import 'src/utils.dart';
16
17 /// Regular expression used to quote globs.
18 final _quoteRegExp = new RegExp(r'[*{[?\\}\],\-()]');
19
20 /// A glob for matching and listing files and directories.
21 ///
22 /// A glob matches an entire string as a path. Although the glob pattern uses
23 /// POSIX syntax, it can match against POSIX, Windows, or URL paths. The format
24 /// it expects paths to use is based on the `context` parameter to [new Glob];
25 /// it defaults to the current system's syntax.
26 ///
27 /// Paths are normalized before being matched against a glob, so for example the
28 /// glob `foo/bar` matches the path `foo/./bar`. A relative glob can match an
29 /// absolute path and vice versa; globs and paths are both interpreted as
30 /// relative to `context.current`, which defaults to the current working
31 /// directory.
32 ///
33 /// When used as a [Pattern], a glob will return either one or zero matches for
34 /// a string depending on whether the entire string matches the glob. These
35 /// matches don't currently have capture groups, although this may change in the
36 /// future.
37 class Glob implements Pattern {
38 /// The pattern used to create this glob.
39 final String pattern;
40
41 /// The context in which paths matched against this glob are interpreted.
42 final p.Context context;
43
44 /// If true, a path matches if it matches the glob itself or is recursively
45 /// contained within a directory that matches.
46 final bool recursive;
47
48 /// The parsed AST of the glob.
49 final AstNode _ast;
50
51 ListTree _listTree;
52
53 /// Whether [context]'s current directory is absolute.
54 bool get _contextIsAbsolute {
55 if (_contextIsAbsoluteCache == null) {
56 _contextIsAbsoluteCache = context.isAbsolute(context.current);
57 }
58 return _contextIsAbsoluteCache;
59 }
60 bool _contextIsAbsoluteCache;
61
62 /// Whether [pattern] could match absolute paths.
63 bool get _patternCanMatchAbsolute {
64 if (_patternCanMatchAbsoluteCache == null) {
65 _patternCanMatchAbsoluteCache = _ast.canMatchAbsolute;
66 }
67 return _patternCanMatchAbsoluteCache;
68 }
69 bool _patternCanMatchAbsoluteCache;
70
71 /// Whether [pattern] could match relative paths.
72 bool get _patternCanMatchRelative {
73 if (_patternCanMatchRelativeCache == null) {
74 _patternCanMatchRelativeCache = _ast.canMatchRelative;
75 }
76 return _patternCanMatchRelativeCache;
77 }
78 bool _patternCanMatchRelativeCache;
79
80 /// Returns [contents] with characters that are meaningful in globs
81 /// backslash-escaped.
82 static String quote(String contents) =>
83 contents.replaceAllMapped(_quoteRegExp, (match) => '\\${match[0]}');
84
85 /// Creates a new glob with [pattern].
86 ///
87 /// Paths matched against the glob are interpreted according to [context]. It
88 /// defaults to the system context.
89 ///
90 /// 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.
92 Glob(String pattern, {p.Context context, bool recursive: false})
93 : this._(
94 pattern,
95 context == null ? p.context : context,
96 recursive);
97
98 // Internal constructor used to fake local variables for [context] and [ast].
99 Glob._(String pattern, p.Context context, bool recursive)
100 : pattern = pattern,
101 context = context,
102 recursive = recursive,
103 _ast = new Parser(pattern + (recursive ? "{,/**}" : ""), context)
104 .parse();
105
106 /// Lists all [FileSystemEntity]s beneath [root] that match the glob.
107 ///
108 /// This works much like [Directory.list], but it only lists directories that
109 /// could contain entities that match the glob. It provides no guarantees
110 /// about the order of the returned entities, although it does guarantee that
111 /// only one entity with a given path will be returned.
112 ///
113 /// [root] defaults to the current working directory.
114 ///
115 /// [followLinks] works the same as for [Directory.list].
116 Stream<FileSystemEntity> list({String root, bool followLinks: true}) {
117 if (context.style != p.style) {
118 throw new StateError("Can't list glob \"$this\"; it matches "
119 "${context.style} paths, but this platform uses ${p.style} paths.");
120 }
121
122 if (_listTree == null) _listTree = new ListTree(_ast);
123 return _listTree.list(root: root, followLinks: followLinks);
124 }
125
126 /// Synchronously lists all [FileSystemEntity]s beneath [root] that match the
127 /// glob.
128 ///
129 /// This works much like [Directory.listSync], but it only lists directories
130 /// that could contain entities that match the glob. It provides no guarantees
131 /// about the order of the returned entities, although it does guarantee that
132 /// only one entity with a given path will be returned.
133 ///
134 /// [root] defaults to the current working directory.
135 ///
136 /// [followLinks] works the same as for [Directory.list].
137 List<FileSystemEntity> listSync({String root, bool followLinks: true}) {
138 if (context.style != p.style) {
139 throw new StateError("Can't list glob \"$this\"; it matches "
140 "${context.style} paths, but this platform uses ${p.style} paths.");
141 }
142
143 if (_listTree == null) _listTree = new ListTree(_ast);
144 return _listTree.listSync(root: root, followLinks: followLinks);
145 }
146
147 /// Returns whether this glob matches [path].
148 bool matches(String path) => matchAsPrefix(path) != null;
149
150 Match matchAsPrefix(String path, [int start = 0]) {
151 // Globs are like anchored RegExps in that they only match entire paths, so
152 // if the match starts anywhere after the first character it can't succeed.
153 if (start != 0) return null;
154
155 if (_patternCanMatchAbsolute &&
156 (_contextIsAbsolute || context.isAbsolute(path))) {
157 var absolutePath = context.normalize(context.absolute(path));
158 if (_ast.matches(toPosixPath(context, absolutePath))) {
159 return new GlobMatch(path, this);
160 }
161 }
162
163 if (_patternCanMatchRelative) {
164 var relativePath = context.relative(path);
165 if (_ast.matches(toPosixPath(context, relativePath))) {
166 return new GlobMatch(path, this);
167 }
168 }
169
170 return null;
171 }
172
173 Iterable<Match> allMatches(String path, [int start = 0]) {
174 var match = matchAsPrefix(path, start);
175 return match == null ? [] : [match];
176 }
177
178 String toString() => pattern;
179 }
OLDNEW
« no previous file with comments | « dart_style/pubspec.yaml ('k') | glob/lib/src/ast.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698