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.directory; | 5 library descriptor.directory; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:io'; | 8 import 'dart:io'; |
9 | 9 |
10 import 'package:pathos/path.dart' as path; | 10 import 'package:pathos/path.dart' as path; |
11 | 11 |
12 import '../../descriptor.dart'; | 12 import '../../descriptor.dart'; |
13 import '../../scheduled_test.dart'; | 13 import '../../scheduled_test.dart'; |
14 import '../utils.dart'; | 14 import '../utils.dart'; |
15 | 15 |
16 /// A path builder to ensure that [load] uses POSIX paths. | 16 /// A path builder to ensure that [load] uses POSIX paths. |
17 final path.Builder _path = new path.Builder(style: path.Style.posix); | 17 final path.Builder _path = new path.Builder(style: path.Style.posix); |
18 | 18 |
19 /// A descriptor describing a directory containing multiple files. | 19 /// A descriptor describing a directory containing multiple files. |
20 class DirectoryDescriptor extends Descriptor { | 20 class DirectoryDescriptor extends Descriptor implements LoadableDescriptor { |
21 /// The entries contained within this directory. This is intentionally | 21 /// The entries contained within this directory. This is intentionally |
22 /// mutable. | 22 /// mutable. |
23 final List<Descriptor> contents; | 23 final List<Descriptor> contents; |
24 | 24 |
25 DirectoryDescriptor(String name, Iterable<Descriptor> contents) | 25 DirectoryDescriptor(String name, Iterable<Descriptor> contents) |
26 : super(name), | 26 : super(name), |
27 contents = contents.toList(); | 27 contents = contents.toList(); |
28 | 28 |
29 Future create([String parent]) => schedule(() { | 29 Future create([String parent]) => schedule(() { |
30 if (parent == null) parent = defaultRoot; | 30 if (parent == null) parent = defaultRoot; |
(...skipping 21 matching lines...) Expand all Loading... |
52 })).then((results) { | 52 })).then((results) { |
53 var errors = results.where((e) => e != null); | 53 var errors = results.where((e) => e != null); |
54 if (errors.isEmpty) return; | 54 if (errors.isEmpty) return; |
55 throw _DirectoryValidationError.merge(errors); | 55 throw _DirectoryValidationError.merge(errors); |
56 }); | 56 }); |
57 } | 57 } |
58 | 58 |
59 Stream<List<int>> load(String pathToLoad) { | 59 Stream<List<int>> load(String pathToLoad) { |
60 return futureStream(new Future.value().then((_) { | 60 return futureStream(new Future.value().then((_) { |
61 if (_path.isAbsolute(pathToLoad)) { | 61 if (_path.isAbsolute(pathToLoad)) { |
62 throw "Can't load absolute path '$pathToLoad'."; | 62 throw new ArgumentError("Can't load absolute path '$pathToLoad'."); |
63 } | 63 } |
64 | 64 |
65 var split = _path.split(_path.normalize(pathToLoad)); | 65 var split = _path.split(_path.normalize(pathToLoad)); |
66 if (split.isEmpty || split.first == '.' || split.first == '..') { | 66 if (split.isEmpty || split.first == '.' || split.first == '..') { |
67 throw "Can't load '$pathToLoad' from within '$name'."; | 67 throw new ArgumentError("Can't load '$pathToLoad' from within " |
| 68 "'$name'."); |
68 } | 69 } |
69 | 70 |
70 var matchingEntries = contents.where((entry) => | 71 var requiresReadable = split.length == 1; |
71 entry.name == split.first).toList(); | 72 var matchingEntries = contents.where((entry) { |
| 73 return entry.name == split.first && (requiresReadable ? |
| 74 entry is ReadableDescriptor : |
| 75 entry is LoadableDescriptor); |
| 76 }).toList(); |
72 | 77 |
| 78 var adjective = requiresReadable ? 'readable' : 'loadable'; |
73 if (matchingEntries.length == 0) { | 79 if (matchingEntries.length == 0) { |
74 throw "Couldn't find an entry named '${split.first}' within '$name'."; | 80 throw "Couldn't find a $adjective entry named '${split.first}' within " |
| 81 "'$name'."; |
75 } else if (matchingEntries.length > 1) { | 82 } else if (matchingEntries.length > 1) { |
76 throw "Found multiple entries named '${split.first}' within '$name'."; | 83 throw "Found multiple $adjective entries named '${split.first}' within " |
| 84 "'$name'."; |
77 } else { | 85 } else { |
78 var remainingPath = split.sublist(1); | 86 var remainingPath = split.sublist(1); |
79 if (remainingPath.isEmpty) { | 87 if (remainingPath.isEmpty) { |
80 return matchingEntries.first.read(); | 88 return matchingEntries.first.read(); |
81 } else { | 89 } else { |
82 return matchingEntries.first.load(_path.joinAll(remainingPath)); | 90 return matchingEntries.first.load(_path.joinAll(remainingPath)); |
83 } | 91 } |
84 } | 92 } |
85 })); | 93 })); |
86 } | 94 } |
87 | 95 |
88 Stream<List<int>> read() => errorStream("Can't read the contents of '$name': " | |
89 "is a directory."); | |
90 | |
91 String describe() { | 96 String describe() { |
92 if (contents.isEmpty) return name; | 97 if (contents.isEmpty) return name; |
93 | 98 |
94 var buffer = new StringBuffer(); | 99 var buffer = new StringBuffer(); |
95 buffer.writeln(name); | 100 buffer.writeln(name); |
96 for (var entry in contents.take(contents.length - 1)) { | 101 for (var entry in contents.take(contents.length - 1)) { |
97 var entryString = prefixLines(entry.describe(), prefix: '| ') | 102 var entryString = prefixLines(entry.describe(), prefix: '| ') |
98 .replaceFirst('| ', '|-- '); | 103 .replaceFirst('| ', '|-- '); |
99 buffer.writeln(entryString); | 104 buffer.writeln(entryString); |
100 } | 105 } |
(...skipping 20 matching lines...) Expand all Loading... |
121 | 126 |
122 _DirectoryValidationError(Iterable errors) | 127 _DirectoryValidationError(Iterable errors) |
123 : errors = errors.map((e) => e.toString()).toList(); | 128 : errors = errors.map((e) => e.toString()).toList(); |
124 | 129 |
125 String toString() { | 130 String toString() { |
126 if (errors.length == 1) return errors.single; | 131 if (errors.length == 1) return errors.single; |
127 return errors.map((e) => prefixLines(e, prefix: ' ', firstPrefix: '* ')) | 132 return errors.map((e) => prefixLines(e, prefix: ' ', firstPrefix: '* ')) |
128 .join('\n'); | 133 .join('\n'); |
129 } | 134 } |
130 } | 135 } |
OLD | NEW |