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:path/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 List<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(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 liftStream(new Future.immediate(null).then((_) { | |
43 if (pathToLoad == null) { | |
44 throw "Can't load the contents of $nameDescription: is a directory."; | |
45 } else if (path.isAbsolute(pathToLoad)) { | |
46 throw "Can't load absolute path '$pathToLoad'."; | |
47 } | |
48 | |
49 var split = path.split(path.normalize(pathToLoad)); | |
50 if (split.first == '.' || split.first == '..') { | |
51 throw "Can't load '$pathToLoad' from within $nameDescription."; | |
52 } | |
53 | |
54 var matchingEntries = contents.where((entry) => | |
55 entry.stringName == split.first); | |
Bob Nystrom
2013/02/22 17:58:21
You might want a .toList() here since you use .len
nweiz
2013/02/22 20:57:20
Done.
| |
56 | |
57 if (matchingEntries.length == 0) { | |
58 throw "Couldn't find an entry named '${split.first}' within " | |
59 "$nameDescription."; | |
60 } else if (matchingEntries.length > 1) { | |
61 throw "Found multiple entries named '${split.first}' within " | |
62 "$nameDescription."; | |
63 } else { | |
64 var remainingPath = split.getRange(1, split.length - 1); | |
65 if (remainingPath.isEmpty) { | |
66 return matchingEntries.first.load(); | |
67 } else { | |
68 return matchingEntries.first.load(path.joinAll(remainingPath)); | |
69 } | |
70 } | |
71 })); | |
72 } | |
73 | |
74 String describe() { | |
75 var description = name; | |
76 if (name is! String) description = 'directory matching $nameDescription'; | |
77 if (contents.isEmpty) return description; | |
78 | |
79 var buffer = new StringBuffer(); | |
80 buffer..add(description)..add('\n'); | |
Bob Nystrom
2013/02/22 17:58:21
buffer.writeln(description);
and below
nweiz
2013/02/22 20:57:20
Done.
| |
81 for (var entry in contents.take(contents.length - 1)) { | |
82 var entryString = prefixLines(entry.describe(), prefix: '| ') | |
83 .replaceFirst('| ', '|-- '); | |
84 buffer..add(entryString)..add('\n'); | |
85 } | |
86 | |
87 var lastEntryString = prefixLines(contents.last.describe(), prefix: ' ') | |
88 .replaceFirst(' ', "'-- "); | |
89 buffer.add(lastEntryString); | |
Bob Nystrom
2013/02/22 17:58:21
May want a trailing newline here.
nweiz
2013/02/22 20:57:20
Nope. This method is like toString in that it's as
| |
90 return buffer.toString(); | |
91 } | |
92 } | |
OLD | NEW |