| 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 '../../../../../pkg/pathos/lib/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 path builder to ensure that [load] uses POSIX paths. | |
| 18 final path.Builder _path = new path.Builder(style: path.Style.posix); | |
| 19 | |
| 20 /// A descriptor describing a directory containing multiple files. | |
| 21 class Directory extends descriptor.Entry { | |
| 22 /// The entries contained within this directory. | |
| 23 final Iterable<descriptor.Entry> contents; | |
| 24 | |
| 25 Directory(Pattern name, this.contents) | |
| 26 : super(name); | |
| 27 | |
| 28 Future create([String parent]) => schedule(() { | |
| 29 if (parent == null) parent = descriptor.defaultRoot; | |
| 30 var fullPath = path.join(parent, stringName); | |
| 31 return new io.Directory(fullPath).create(recursive: true).then((_) { | |
| 32 return Future.wait( | |
| 33 contents.map((entry) => entry.create(fullPath)).toList()); | |
| 34 }); | |
| 35 }, 'creating directory:\n${describe()}'); | |
| 36 | |
| 37 Future validate([String parent]) => schedule(() { | |
| 38 if (parent == null) parent = descriptor.defaultRoot; | |
| 39 var fullPath = entryMatchingPattern('Directory', parent, name); | |
| 40 return Future.wait( | |
| 41 contents.map((entry) => entry.validate(fullPath)).toList()); | |
| 42 }, 'validating directory:\n${describe()}'); | |
| 43 | |
| 44 Stream<List<int>> load(String pathToLoad) { | |
| 45 return futureStream(new Future.immediate(null).then((_) { | |
| 46 if (_path.isAbsolute(pathToLoad)) { | |
| 47 throw "Can't load absolute path '$pathToLoad'."; | |
| 48 } | |
| 49 | |
| 50 var split = _path.split(_path.normalize(pathToLoad)); | |
| 51 if (split.isEmpty || split.first == '.' || split.first == '..') { | |
| 52 throw "Can't load '$pathToLoad' from within $nameDescription."; | |
| 53 } | |
| 54 | |
| 55 var matchingEntries = contents.where((entry) => | |
| 56 entry.stringName == split.first).toList(); | |
| 57 | |
| 58 if (matchingEntries.length == 0) { | |
| 59 throw "Couldn't find an entry named '${split.first}' within " | |
| 60 "$nameDescription."; | |
| 61 } else if (matchingEntries.length > 1) { | |
| 62 throw "Found multiple entries named '${split.first}' within " | |
| 63 "$nameDescription."; | |
| 64 } else { | |
| 65 var remainingPath = split.getRange(1, split.length - 1); | |
| 66 if (remainingPath.isEmpty) { | |
| 67 return matchingEntries.first.read(); | |
| 68 } else { | |
| 69 return matchingEntries.first.load(_path.joinAll(remainingPath)); | |
| 70 } | |
| 71 } | |
| 72 })); | |
| 73 } | |
| 74 | |
| 75 Stream<List<int>> read() => errorStream("Can't read the contents of " | |
| 76 "$nameDescription: is a directory."); | |
| 77 | |
| 78 String describe() { | |
| 79 var description = name; | |
| 80 if (name is! String) description = 'directory matching $nameDescription'; | |
| 81 if (contents.isEmpty) return description; | |
| 82 | |
| 83 var buffer = new StringBuffer(); | |
| 84 buffer.writeln(description); | |
| 85 for (var entry in contents.take(contents.length - 1)) { | |
| 86 var entryString = prefixLines(entry.describe(), prefix: '| ') | |
| 87 .replaceFirst('| ', '|-- '); | |
| 88 buffer.writeln(entryString); | |
| 89 } | |
| 90 | |
| 91 var lastEntryString = prefixLines(contents.last.describe(), prefix: ' ') | |
| 92 .replaceFirst(' ', "'-- "); | |
| 93 buffer.write(lastEntryString); | |
| 94 return buffer.toString(); | |
| 95 } | |
| 96 } | |
| OLD | NEW |