OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013, 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 descriptor.utils; | |
6 | |
7 import 'dart:io'; | |
8 | |
9 import 'package:path/path.dart' as path; | |
10 | |
11 /// Returns a single filesystem entry within [parent] whose name matches | |
12 /// [pattern]. If [pattern] is a string, looks for an exact match; otherwise, | |
13 /// looks for an entry that contains [pattern]. | |
14 /// | |
15 /// If there are no entries in [parent] matching [pattern], or more than one, | |
16 /// this will throw an exception. | |
17 String entryMatchingPattern(String parent, Pattern pattern) { | |
18 if (pattern is String) { | |
19 var path = path.join(parent, pattern); | |
20 if (new File(path).existsSync() || new Directory(path).existsSync()) { | |
21 return path; | |
22 } | |
23 throw "Entry not found: '$path'."; | |
24 } | |
25 | |
26 var matchingEntries = new Directory(parent).listSync() | |
Bob Nystrom
2013/02/22 17:58:21
Yay listSync()!
| |
27 .map((entry) => entry is File ? entry.fullPathSync() : entry.path) | |
28 .where((entry) => entry.contains(pattern)) | |
29 .toList(); | |
30 | |
31 if (matchingEntries.length == 0) { | |
32 throw "No entry found in '$parent' matching ${describePattern(pattern)}."; | |
33 } else if (matchingEntries.length > 1) { | |
34 throw "Multiple entries found in '$parent' matching " | |
35 "${describePattern(pattern)}:\n" | |
36 "${matchingEntries.map((entry) => '* $entry').join('\n')}"; | |
37 } else { | |
38 return matchingEntries.first; | |
39 } | |
40 } | |
41 | |
42 /// Returns a human-readable description of [pattern]. | |
43 String describePattern(Pattern pattern) { | |
44 if (pattern is String) return "'$pattern'"; | |
Bob Nystrom
2013/02/22 17:58:21
Do you care about escaping quotes here?
nweiz
2013/02/22 20:57:20
I'd *like* to escape enough stuff that it's a vali
| |
45 if (pattern is! RegExp) return '$pattern'; | |
46 | |
47 var flags = new StringBuffer(); | |
48 if (!pattern.isCaseSensitive) flags.add('i'); | |
49 if (pattern.isMultiLine) flags.add('m'); | |
50 return '/${pattern.pattern}/$flags'; | |
51 } | |
OLD | NEW |