| 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.file; | 5 library descriptor.file; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:io'; | 8 import 'dart:io'; |
| 9 | 9 |
| 10 import '../../../../../pkg/pathos/lib/path.dart' as path; | 10 import '../../../../../pkg/pathos/lib/path.dart' as path; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 Future validate([String parent]) => schedule(() => validateNow(parent), | 36 Future validate([String parent]) => schedule(() => validateNow(parent), |
| 37 'validating directory:\n${describe()}'); | 37 'validating directory:\n${describe()}'); |
| 38 | 38 |
| 39 Future validateNow([String parent]) { | 39 Future validateNow([String parent]) { |
| 40 if (parent == null) parent = defaultRoot; | 40 if (parent == null) parent = defaultRoot; |
| 41 var fullPath = path.join(parent, name); | 41 var fullPath = path.join(parent, name); |
| 42 if (!new Directory(fullPath).existsSync()) { | 42 if (!new Directory(fullPath).existsSync()) { |
| 43 throw "Directory not found: '$fullPath'."; | 43 throw "Directory not found: '$fullPath'."; |
| 44 } | 44 } |
| 45 | 45 |
| 46 return Future.wait( | 46 return Future.wait(contents.map((entry) { |
| 47 contents.map((entry) => entry.validateNow(fullPath)).toList()); | 47 return new Future.of(() => entry.validateNow(fullPath)) |
| 48 .then((_) => null) |
| 49 .catchError((e) => e.error); |
| 50 })).then((results) { |
| 51 var errors = results.where((e) => e != null); |
| 52 if (errors.isEmpty) return; |
| 53 throw _DirectoryValidationError.merge(errors); |
| 54 }); |
| 48 } | 55 } |
| 49 | 56 |
| 50 Stream<List<int>> load(String pathToLoad) { | 57 Stream<List<int>> load(String pathToLoad) { |
| 51 return futureStream(new Future.immediate(null).then((_) { | 58 return futureStream(new Future.immediate(null).then((_) { |
| 52 if (_path.isAbsolute(pathToLoad)) { | 59 if (_path.isAbsolute(pathToLoad)) { |
| 53 throw "Can't load absolute path '$pathToLoad'."; | 60 throw "Can't load absolute path '$pathToLoad'."; |
| 54 } | 61 } |
| 55 | 62 |
| 56 var split = _path.split(_path.normalize(pathToLoad)); | 63 var split = _path.split(_path.normalize(pathToLoad)); |
| 57 if (split.isEmpty || split.first == '.' || split.first == '..') { | 64 if (split.isEmpty || split.first == '.' || split.first == '..') { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 .replaceFirst('| ', '|-- '); | 96 .replaceFirst('| ', '|-- '); |
| 90 buffer.writeln(entryString); | 97 buffer.writeln(entryString); |
| 91 } | 98 } |
| 92 | 99 |
| 93 var lastEntryString = prefixLines(contents.last.describe(), prefix: ' ') | 100 var lastEntryString = prefixLines(contents.last.describe(), prefix: ' ') |
| 94 .replaceFirst(' ', "'-- "); | 101 .replaceFirst(' ', "'-- "); |
| 95 buffer.write(lastEntryString); | 102 buffer.write(lastEntryString); |
| 96 return buffer.toString(); | 103 return buffer.toString(); |
| 97 } | 104 } |
| 98 } | 105 } |
| 106 |
| 107 /// A class for formatting errors thrown by [DirectoryDescriptor]. |
| 108 class _DirectoryValidationError { |
| 109 final Collection<String> errors; |
| 110 |
| 111 /// Flatten nested [_DirectoryValidationError]s in [errors] to create a single |
| 112 /// list of errors. |
| 113 static _DirectoryValidationError merge(Iterable errors) { |
| 114 return new _DirectoryValidationError(errors.expand((error) { |
| 115 if (error is _DirectoryValidationError) return error.errors; |
| 116 return [error]; |
| 117 })); |
| 118 } |
| 119 |
| 120 _DirectoryValidationError(Iterable errors) |
| 121 : errors = errors.map((e) => e.toString()).toList(); |
| 122 |
| 123 String toString() { |
| 124 if (errors.length == 1) return errors.single; |
| 125 return errors.map((e) => prefixLines(e, prefix: ' ', firstPrefix: '* ')) |
| 126 .join('\n'); |
| 127 } |
| 128 } |
| OLD | NEW |