| 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.utils; | 5 library descriptor.utils; |
| 6 | 6 |
| 7 import 'dart:io'; | 7 import 'dart:io'; |
| 8 | 8 |
| 9 import 'package:pathos/path.dart' as path; | 9 import 'package:pathos/path.dart' as path; |
| 10 | 10 |
| 11 /// Returns a single filesystem entry within [parent] whose name matches | 11 /// Returns a single filesystem entry within [parent] whose name matches |
| 12 /// [pattern]. If [pattern] is a string, looks for an exact match; otherwise, | 12 /// [pattern]. If [pattern] is a string, looks for an exact match; otherwise, |
| 13 /// looks for an entry that contains [pattern]. | 13 /// looks for an entry that contains [pattern]. |
| 14 /// | 14 /// |
| 15 /// If there are no entries in [parent] matching [pattern], or more than one, | 15 /// If there are no entries in [parent] matching [pattern], or more than one, |
| 16 /// this will throw an exception. | 16 /// this will throw an exception. |
| 17 /// | 17 /// |
| 18 /// [type] is used for error reporting. It should be capitalized. | 18 /// [type] is used for error reporting. It should be capitalized. |
| 19 String entryMatchingPattern(String type, String parent, Pattern pattern) { | 19 String entryMatchingPattern(String type, String parent, Pattern pattern) { |
| 20 if (pattern is String) { | 20 if (pattern is String) { |
| 21 var fullPath = path.join(parent, pattern); | 21 var fullPath = path.join(parent, pattern); |
| 22 if (new File(fullPath).existsSync() || new Directory(fullPath).existsSync())
{ | 22 if (new File(fullPath).existsSync() || new Directory(fullPath).existsSync())
{ |
| 23 return fullPath; | 23 return fullPath; |
| 24 } | 24 } |
| 25 throw "$type not found: '$fullPath'."; | 25 throw "$type not found: '$fullPath'."; |
| 26 } | 26 } |
| 27 | 27 |
| 28 var matchingEntries = new Directory(parent).listSync() | 28 var matchingEntries = new Directory(parent).listSync() |
| 29 .map((entry) => entry is File ? entry.fullPathSync() : entry.path) | 29 .map((entry) => entry is File ? entry.fullPathSync() : entry.path) |
| 30 .where((entry) => entry.contains(pattern)) | 30 .where((entry) => path.basename(entry).contains(pattern)) |
| 31 .toList(); | 31 .toList(); |
| 32 matchingEntries.sort(); | 32 matchingEntries.sort(); |
| 33 | 33 |
| 34 if (matchingEntries.length == 0) { | 34 if (matchingEntries.length == 0) { |
| 35 throw "No entry found in '$parent' matching ${describePattern(pattern)}."; | 35 throw "No entry found in '$parent' matching ${describePattern(pattern)}."; |
| 36 } else if (matchingEntries.length > 1) { | 36 } else if (matchingEntries.length > 1) { |
| 37 throw "Multiple entries found in '$parent' matching " | 37 throw "Multiple entries found in '$parent' matching " |
| 38 "${describePattern(pattern)}:\n" | 38 "${describePattern(pattern)}:\n" |
| 39 "${matchingEntries.map((entry) => '* $entry').join('\n')}"; | 39 "${matchingEntries.map((entry) => '* $entry').join('\n')}"; |
| 40 } else { | 40 } else { |
| 41 return matchingEntries.first; | 41 return matchingEntries.first; |
| 42 } | 42 } |
| 43 } | 43 } |
| 44 | 44 |
| 45 /// Returns a human-readable description of [pattern]. | 45 /// Returns a human-readable description of [pattern]. |
| 46 String describePattern(Pattern pattern) { | 46 String describePattern(Pattern pattern) { |
| 47 if (pattern is String) return "'$pattern'"; | 47 if (pattern is String) return "'$pattern'"; |
| 48 if (pattern is! RegExp) return '$pattern'; | 48 if (pattern is! RegExp) return '$pattern'; |
| 49 | 49 |
| 50 var flags = new StringBuffer(); | 50 var flags = new StringBuffer(); |
| 51 if (!pattern.isCaseSensitive) flags.add('i'); | 51 if (!pattern.isCaseSensitive) flags.add('i'); |
| 52 if (pattern.isMultiLine) flags.add('m'); | 52 if (pattern.isMultiLine) flags.add('m'); |
| 53 return '/${pattern.pattern}/$flags'; | 53 return '/${pattern.pattern}/$flags'; |
| 54 } | 54 } |
| OLD | NEW |