| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library descriptor.pattern; | 5 library descriptor.pattern; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:io'; | 8 import 'dart:io'; |
| 9 | 9 |
| 10 import 'package:path/path.dart' as path; | 10 import 'package:path/path.dart' as path; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 Future validateNow([String parent]) { | 48 Future validateNow([String parent]) { |
| 49 if (parent == null) parent = defaultRoot; | 49 if (parent == null) parent = defaultRoot; |
| 50 // TODO(nweiz): make sure this works with symlinks. | 50 // TODO(nweiz): make sure this works with symlinks. |
| 51 var matchingEntries = new Directory(parent).listSync() | 51 var matchingEntries = new Directory(parent).listSync() |
| 52 .map((entry) => entry is File ? entry.fullPathSync() : entry.path) | 52 .map((entry) => entry is File ? entry.fullPathSync() : entry.path) |
| 53 .where((entry) => fullMatch(path.basename(entry), pattern)) | 53 .where((entry) => fullMatch(path.basename(entry), pattern)) |
| 54 .toList(); | 54 .toList(); |
| 55 matchingEntries.sort(); | 55 matchingEntries.sort(); |
| 56 | 56 |
| 57 if (matchingEntries.isEmpty) { | 57 if (matchingEntries.isEmpty) { |
| 58 throw "No entry found in '$parent' matching ${_patternDescription}."; | 58 fail("No entry found in '$parent' matching ${_patternDescription}."); |
| 59 } | 59 } |
| 60 | 60 |
| 61 return Future.wait(matchingEntries.map((entry) { | 61 return Future.wait(matchingEntries.map((entry) { |
| 62 var descriptor = _fn(path.basename(entry)); | 62 var descriptor = _fn(path.basename(entry)); |
| 63 return descriptor.validateNow(parent).then((_) { | 63 return descriptor.validateNow(parent).then((_) { |
| 64 return new Pair(null, descriptor.describe()); | 64 return new Pair(null, descriptor.describe()); |
| 65 }).catchError((error) { | 65 }).catchError((error) { |
| 66 return new Pair(error.toString(), descriptor.describe()); | 66 return new Pair(error.toString(), descriptor.describe()); |
| 67 }); | 67 }); |
| 68 })).then((results) { | 68 })).then((results) { |
| 69 var matches = results.where((result) => result.first == null).toList(); | 69 var matches = results.where((result) => result.first == null).toList(); |
| 70 // If exactly one entry matching [pattern] validated, we're happy. | 70 // If exactly one entry matching [pattern] validated, we're happy. |
| 71 if (matches.length == 1) return; | 71 if (matches.length == 1) return; |
| 72 | 72 |
| 73 // If more than one entry matching [pattern] validated, that's bad. | 73 // If more than one entry matching [pattern] validated, that's bad. |
| 74 if (matches.length > 1) { | 74 if (matches.length > 1) { |
| 75 var resultString = matches.map((result) { | 75 var resultString = matches.map((result) { |
| 76 return prefixLines(result.last, firstPrefix: '* ', prefix: ' '); | 76 return prefixLines(result.last, firstPrefix: '* ', prefix: ' '); |
| 77 }).join('\n'); | 77 }).join('\n'); |
| 78 | 78 |
| 79 throw "Multiple valid entries found in '$parent' matching " | 79 fail("Multiple valid entries found in '$parent' matching " |
| 80 "$_patternDescription:\n" | 80 "$_patternDescription:\n" |
| 81 "$resultString"; | 81 "$resultString"); |
| 82 } | 82 } |
| 83 | 83 |
| 84 // If no entries matching [pattern] validated, that's also bad. | 84 // If no entries matching [pattern] validated, that's also bad. |
| 85 var resultString = results.map((result) { | 85 var resultString = results.map((result) { |
| 86 return prefixLines( | 86 return prefixLines( |
| 87 "Caught error\n" | 87 "Caught error\n" |
| 88 "${prefixLines(result.first)}\n" | 88 "${prefixLines(result.first)}\n" |
| 89 "while validating\n" | 89 "while validating\n" |
| 90 "${prefixLines(result.last)}", | 90 "${prefixLines(result.last)}", |
| 91 firstPrefix: '* ', prefix: ' '); | 91 firstPrefix: '* ', prefix: ' '); |
| 92 }).join('\n'); | 92 }).join('\n'); |
| 93 | 93 |
| 94 throw "No valid entries found in '$parent' matching " | 94 fail("No valid entries found in '$parent' matching " |
| 95 "$_patternDescription:\n" | 95 "$_patternDescription:\n" |
| 96 "$resultString"; | 96 "$resultString"); |
| 97 }); | 97 }); |
| 98 } | 98 } |
| 99 | 99 |
| 100 String describe() => "entry matching $_patternDescription"; | 100 String describe() => "entry matching $_patternDescription"; |
| 101 | 101 |
| 102 String get _patternDescription { | 102 String get _patternDescription { |
| 103 if (pattern is String) return "'$pattern'"; | 103 if (pattern is String) return "'$pattern'"; |
| 104 if (pattern is! RegExp) return '$pattern'; | 104 if (pattern is! RegExp) return '$pattern'; |
| 105 | 105 |
| 106 var regExp = pattern as RegExp; | 106 var regExp = pattern as RegExp; |
| 107 var flags = new StringBuffer(); | 107 var flags = new StringBuffer(); |
| 108 if (!regExp.isCaseSensitive) flags.write('i'); | 108 if (!regExp.isCaseSensitive) flags.write('i'); |
| 109 if (regExp.isMultiLine) flags.write('m'); | 109 if (regExp.isMultiLine) flags.write('m'); |
| 110 return '/${regExp.pattern}/$flags'; | 110 return '/${regExp.pattern}/$flags'; |
| 111 } | 111 } |
| 112 | 112 |
| 113 Future create([String parent]) => new Future.error( | 113 Future create([String parent]) => new Future.error( |
| 114 new UnsupportedError("Pattern descriptors don't support create().")); | 114 new UnsupportedError("Pattern descriptors don't support create().")); |
| 115 } | 115 } |
| OLD | NEW |