Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(184)

Side by Side Diff: pkg/scheduled_test/lib/src/descriptor/directory_descriptor.dart

Issue 62753005: Refactor pkg/path. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: code review Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « pkg/path/test/windows_test.dart ('k') | pkg/stack_trace/test/vm_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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:path/path.dart' as path; 10 import 'package:path/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.
17 final path.Builder _path = new path.Builder(style: path.Style.posix);
18
19 /// A descriptor describing a directory containing multiple files. 16 /// A descriptor describing a directory containing multiple files.
20 class DirectoryDescriptor extends Descriptor implements LoadableDescriptor { 17 class DirectoryDescriptor extends Descriptor implements LoadableDescriptor {
21 /// The entries contained within this directory. This is intentionally 18 /// The entries contained within this directory. This is intentionally
22 /// mutable. 19 /// mutable.
23 final List<Descriptor> contents; 20 final List<Descriptor> contents;
24 21
25 DirectoryDescriptor(String name, Iterable<Descriptor> contents) 22 DirectoryDescriptor(String name, Iterable<Descriptor> contents)
26 : super(name), 23 : super(name),
27 contents = contents.toList(); 24 contents = contents.toList();
28 25
(...skipping 22 matching lines...) Expand all
51 .catchError((e) => e); 48 .catchError((e) => e);
52 })).then((results) { 49 })).then((results) {
53 var errors = results.where((e) => e != null); 50 var errors = results.where((e) => e != null);
54 if (errors.isEmpty) return; 51 if (errors.isEmpty) return;
55 throw _DirectoryValidationError.merge(errors); 52 throw _DirectoryValidationError.merge(errors);
56 }); 53 });
57 } 54 }
58 55
59 Stream<List<int>> load(String pathToLoad) { 56 Stream<List<int>> load(String pathToLoad) {
60 return futureStream(new Future.value().then((_) { 57 return futureStream(new Future.value().then((_) {
61 if (_path.isAbsolute(pathToLoad)) { 58 if (path.posix.isAbsolute(pathToLoad)) {
62 throw new ArgumentError("Can't load absolute path '$pathToLoad'."); 59 throw new ArgumentError("Can't load absolute path '$pathToLoad'.");
63 } 60 }
64 61
65 var split = _path.split(_path.normalize(pathToLoad)); 62 var split = path.posix.split(path.posix.normalize(pathToLoad));
66 if (split.isEmpty || split.first == '.' || split.first == '..') { 63 if (split.isEmpty || split.first == '.' || split.first == '..') {
67 throw new ArgumentError("Can't load '$pathToLoad' from within " 64 throw new ArgumentError("Can't load '$pathToLoad' from within "
68 "'$name'."); 65 "'$name'.");
69 } 66 }
70 67
71 var requiresReadable = split.length == 1; 68 var requiresReadable = split.length == 1;
72 var matchingEntries = contents.where((entry) { 69 var matchingEntries = contents.where((entry) {
73 return entry.name == split.first && (requiresReadable ? 70 return entry.name == split.first && (requiresReadable ?
74 entry is ReadableDescriptor : 71 entry is ReadableDescriptor :
75 entry is LoadableDescriptor); 72 entry is LoadableDescriptor);
76 }).toList(); 73 }).toList();
77 74
78 var adjective = requiresReadable ? 'readable' : 'loadable'; 75 var adjective = requiresReadable ? 'readable' : 'loadable';
79 if (matchingEntries.length == 0) { 76 if (matchingEntries.length == 0) {
80 fail("Couldn't find a $adjective entry named '${split.first}' within " 77 fail("Couldn't find a $adjective entry named '${split.first}' within "
81 "'$name'."); 78 "'$name'.");
82 } else if (matchingEntries.length > 1) { 79 } else if (matchingEntries.length > 1) {
83 fail("Found multiple $adjective entries named '${split.first}' within " 80 fail("Found multiple $adjective entries named '${split.first}' within "
84 "'$name'."); 81 "'$name'.");
85 } else { 82 } else {
86 var remainingPath = split.sublist(1); 83 var remainingPath = split.sublist(1);
87 if (remainingPath.isEmpty) { 84 if (remainingPath.isEmpty) {
88 return (matchingEntries.first as ReadableDescriptor).read(); 85 return (matchingEntries.first as ReadableDescriptor).read();
89 } else { 86 } else {
90 return (matchingEntries.first as LoadableDescriptor) 87 return (matchingEntries.first as LoadableDescriptor)
91 .load(_path.joinAll(remainingPath)); 88 .load(path.posix.joinAll(remainingPath));
92 } 89 }
93 } 90 }
94 })); 91 }));
95 } 92 }
96 93
97 String describe() { 94 String describe() {
98 if (contents.isEmpty) return name; 95 if (contents.isEmpty) return name;
99 96
100 var buffer = new StringBuffer(); 97 var buffer = new StringBuffer();
101 buffer.writeln(name); 98 buffer.writeln(name);
(...skipping 25 matching lines...) Expand all
127 124
128 _DirectoryValidationError(Iterable errors) 125 _DirectoryValidationError(Iterable errors)
129 : errors = errors.map((e) => e.toString()).toList(); 126 : errors = errors.map((e) => e.toString()).toList();
130 127
131 String toString() { 128 String toString() {
132 if (errors.length == 1) return errors.single; 129 if (errors.length == 1) return errors.single;
133 return errors.map((e) => prefixLines(e, prefix: ' ', firstPrefix: '* ')) 130 return errors.map((e) => prefixLines(e, prefix: ' ', firstPrefix: '* '))
134 .join('\n'); 131 .join('\n');
135 } 132 }
136 } 133 }
OLDNEW
« no previous file with comments | « pkg/path/test/windows_test.dart ('k') | pkg/stack_trace/test/vm_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698