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 "Can't load absolute path '$pathToLoad'."; |
Bob Nystrom
2013/06/04 15:45:53
While you're at it, make this an ArgumentError.
nweiz
2013/06/04 18:26:45
Done.
| |
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 "Can't load '$pathToLoad' from within '$name'."; |
Bob Nystrom
2013/06/04 15:45:53
And this.
| |
68 } | 68 } |
69 | 69 |
70 var matchingEntries = contents.where((entry) => | 70 var requiresReadable = split.length == 1; |
71 entry.name == split.first).toList(); | 71 var matchingEntries = contents.where((entry) { |
72 return entry.name == split.first && (requiresReadable ? | |
73 entry is ReadableDescriptor : | |
74 entry is LoadableDescriptor); | |
Bob Nystrom
2013/06/04 15:45:53
Is DirectoryDescriptor the only thing that impleme
nweiz
2013/06/04 18:26:45
I want to support directory-like descriptors that
| |
75 }).toList(); | |
72 | 76 |
77 var adjective = requiresReadable ? 'readable' : 'loadable'; | |
73 if (matchingEntries.length == 0) { | 78 if (matchingEntries.length == 0) { |
74 throw "Couldn't find an entry named '${split.first}' within '$name'."; | 79 throw "Couldn't find a $adjective entry named '${split.first}' within " |
80 "'$name'."; | |
75 } else if (matchingEntries.length > 1) { | 81 } else if (matchingEntries.length > 1) { |
76 throw "Found multiple entries named '${split.first}' within '$name'."; | 82 throw "Found multiple $adjective entries named '${split.first}' within " |
83 "'$name'."; | |
77 } else { | 84 } else { |
78 var remainingPath = split.sublist(1); | 85 var remainingPath = split.sublist(1); |
79 if (remainingPath.isEmpty) { | 86 if (remainingPath.isEmpty) { |
80 return matchingEntries.first.read(); | 87 return matchingEntries.first.read(); |
81 } else { | 88 } else { |
82 return matchingEntries.first.load(_path.joinAll(remainingPath)); | 89 return matchingEntries.first.load(_path.joinAll(remainingPath)); |
83 } | 90 } |
84 } | 91 } |
85 })); | 92 })); |
86 } | 93 } |
87 | 94 |
88 Stream<List<int>> read() => errorStream("Can't read the contents of '$name': " | |
89 "is a directory."); | |
90 | |
91 String describe() { | 95 String describe() { |
92 if (contents.isEmpty) return name; | 96 if (contents.isEmpty) return name; |
93 | 97 |
94 var buffer = new StringBuffer(); | 98 var buffer = new StringBuffer(); |
95 buffer.writeln(name); | 99 buffer.writeln(name); |
96 for (var entry in contents.take(contents.length - 1)) { | 100 for (var entry in contents.take(contents.length - 1)) { |
97 var entryString = prefixLines(entry.describe(), prefix: '| ') | 101 var entryString = prefixLines(entry.describe(), prefix: '| ') |
98 .replaceFirst('| ', '|-- '); | 102 .replaceFirst('| ', '|-- '); |
99 buffer.writeln(entryString); | 103 buffer.writeln(entryString); |
100 } | 104 } |
(...skipping 20 matching lines...) Expand all Loading... | |
121 | 125 |
122 _DirectoryValidationError(Iterable errors) | 126 _DirectoryValidationError(Iterable errors) |
123 : errors = errors.map((e) => e.toString()).toList(); | 127 : errors = errors.map((e) => e.toString()).toList(); |
124 | 128 |
125 String toString() { | 129 String toString() { |
126 if (errors.length == 1) return errors.single; | 130 if (errors.length == 1) return errors.single; |
127 return errors.map((e) => prefixLines(e, prefix: ' ', firstPrefix: '* ')) | 131 return errors.map((e) => prefixLines(e, prefix: ' ', firstPrefix: '* ')) |
128 .join('\n'); | 132 .join('\n'); |
129 } | 133 } |
130 } | 134 } |
OLD | NEW |