OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 library descriptor.file; | |
6 | |
7 import 'dart:async'; | |
8 import 'dart:io' as io; | |
9 | |
10 import 'package:pathos/path.dart' as path; | |
11 | |
12 import '../../descriptor.dart' as descriptor; | |
13 import '../../scheduled_test.dart'; | |
14 import '../utils.dart'; | |
15 import 'utils.dart'; | |
16 | |
17 /// A descriptor describing a directory containing multiple files. | |
18 class Directory extends descriptor.Entry { | |
19 /// The entries contained within this directory. | |
20 final Iterable<descriptor.Entry> contents; | |
21 | |
22 Directory(Pattern name, this.contents) | |
23 : super(name); | |
24 | |
25 Future create([String parent]) => schedule(() { | |
26 if (parent == null) parent = descriptor.defaultRoot; | |
27 var fullPath = path.join(parent, stringName); | |
28 return new io.Directory(fullPath).create(recursive: true).then((_) { | |
29 return Future.wait( | |
30 contents.map((entry) => entry.create(fullPath)).toList()); | |
31 }); | |
32 }, 'creating directory:\n${describe()}'); | |
33 | |
34 Future validate([String parent]) => schedule(() { | |
35 if (parent == null) parent = descriptor.defaultRoot; | |
36 var fullPath = entryMatchingPattern('Directory', parent, name); | |
37 return Future.wait( | |
38 contents.map((entry) => entry.validate(fullPath)).toList()); | |
39 }, 'validating directory:\n${describe()}'); | |
40 | |
41 Stream<List<int>> load(String pathToLoad) { | |
42 return futureStream(new Future.immediate(null).then((_) { | |
43 if (path.isAbsolute(pathToLoad)) { | |
44 throw "Can't load absolute path '$pathToLoad'."; | |
45 } | |
46 | |
47 var split = path.split(path.normalize(pathToLoad)); | |
48 if (split.isEmpty || split.first == '.' || split.first == '..') { | |
49 throw "Can't load '$pathToLoad' from within $nameDescription."; | |
50 } | |
51 | |
52 var matchingEntries = contents.where((entry) => | |
53 entry.stringName == split.first).toList(); | |
54 | |
55 if (matchingEntries.length == 0) { | |
56 throw "Couldn't find an entry named '${split.first}' within " | |
57 "$nameDescription."; | |
58 } else if (matchingEntries.length > 1) { | |
59 throw "Found multiple entries named '${split.first}' within " | |
60 "$nameDescription."; | |
61 } else { | |
62 var remainingPath = split.getRange(1, split.length - 1); | |
63 if (remainingPath.isEmpty) { | |
64 return matchingEntries.first.read(); | |
65 } else { | |
66 return matchingEntries.first.load(path.joinAll(remainingPath)); | |
67 } | |
68 } | |
69 })); | |
70 } | |
71 | |
72 Stream<List<int>> read() => errorStream("Can't load the contents of " | |
Bob Nystrom
2013/02/22 22:08:57
"load" -> "read"
nweiz
2013/02/22 23:05:21
Done.
| |
73 "$nameDescription: is a directory."); | |
74 | |
75 String describe() { | |
76 var description = name; | |
77 if (name is! String) description = 'directory matching $nameDescription'; | |
78 if (contents.isEmpty) return description; | |
79 | |
80 var buffer = new StringBuffer(); | |
81 buffer.writeln(description); | |
82 for (var entry in contents.take(contents.length - 1)) { | |
83 var entryString = prefixLines(entry.describe(), prefix: '| ') | |
84 .replaceFirst('| ', '|-- '); | |
85 buffer.writeln(entryString); | |
86 } | |
87 | |
88 var lastEntryString = prefixLines(contents.last.describe(), prefix: ' ') | |
89 .replaceFirst(' ', "'-- "); | |
90 buffer.add(lastEntryString); | |
91 return buffer.toString(); | |
92 } | |
93 } | |
OLD | NEW |