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 source_file_provider; | 5 library source_file_provider; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:convert'; | 8 import 'dart:convert'; |
9 import 'dart:io'; | 9 import 'dart:io'; |
10 import 'dart:math' as math; | 10 import 'dart:math' as math; |
(...skipping 12 matching lines...) Expand all Loading... |
23 // +1 to have a 0 terminated list, see [Scanner]. | 23 // +1 to have a 0 terminated list, see [Scanner]. |
24 var buffer = new Uint8List(length + 1); | 24 var buffer = new Uint8List(length + 1); |
25 var bytes = file.readIntoSync(buffer, 0, length); | 25 var bytes = file.readIntoSync(buffer, 0, length); |
26 file.closeSync(); | 26 file.closeSync(); |
27 return buffer; | 27 return buffer; |
28 } | 28 } |
29 | 29 |
30 abstract class SourceFileProvider { | 30 abstract class SourceFileProvider { |
31 bool isWindows = (Platform.operatingSystem == 'windows'); | 31 bool isWindows = (Platform.operatingSystem == 'windows'); |
32 Uri cwd = currentDirectory; | 32 Uri cwd = currentDirectory; |
33 Map<String, SourceFile> sourceFiles = <String, SourceFile>{}; | 33 Map<Uri, SourceFile> sourceFiles = <Uri, SourceFile>{}; |
34 int dartCharactersRead = 0; | 34 int dartCharactersRead = 0; |
35 | 35 |
36 Future<String> readStringFromUri(Uri resourceUri) { | 36 Future<String> readStringFromUri(Uri resourceUri) { |
37 return readUtf8BytesFromUri(resourceUri).then(UTF8.decode); | 37 return readUtf8BytesFromUri(resourceUri).then(UTF8.decode); |
38 } | 38 } |
39 | 39 |
40 Future<List<int>> readUtf8BytesFromUri(Uri resourceUri) { | 40 Future<List<int>> readUtf8BytesFromUri(Uri resourceUri) { |
41 if (resourceUri.scheme == 'file') { | 41 if (resourceUri.scheme == 'file') { |
42 return _readFromFile(resourceUri); | 42 return _readFromFile(resourceUri); |
43 } else if (resourceUri.scheme == 'http' || resourceUri.scheme == 'https') { | 43 } else if (resourceUri.scheme == 'http' || resourceUri.scheme == 'https') { |
(...skipping 11 matching lines...) Expand all Loading... |
55 } on FileSystemException catch (ex) { | 55 } on FileSystemException catch (ex) { |
56 OSError ose = ex.osError; | 56 OSError ose = ex.osError; |
57 String detail = (ose != null && ose.message != null) | 57 String detail = (ose != null && ose.message != null) |
58 ? ' (${ose.message})' | 58 ? ' (${ose.message})' |
59 : ''; | 59 : ''; |
60 return new Future.error( | 60 return new Future.error( |
61 "Error reading '${relativize(cwd, resourceUri, isWindows)}'" | 61 "Error reading '${relativize(cwd, resourceUri, isWindows)}'" |
62 "$detail"); | 62 "$detail"); |
63 } | 63 } |
64 dartCharactersRead += source.length; | 64 dartCharactersRead += source.length; |
65 sourceFiles[resourceUri.toString()] = | 65 sourceFiles[resourceUri] = |
66 new CachingUtf8BytesSourceFile(relativizeUri(resourceUri), source); | 66 new CachingUtf8BytesSourceFile( |
| 67 resourceUri, relativizeUri(resourceUri), source); |
67 return new Future.value(source); | 68 return new Future.value(source); |
68 } | 69 } |
69 | 70 |
70 Future<List<int>> _readFromHttp(Uri resourceUri) { | 71 Future<List<int>> _readFromHttp(Uri resourceUri) { |
71 assert(resourceUri.scheme == 'http'); | 72 assert(resourceUri.scheme == 'http'); |
72 HttpClient client = new HttpClient(); | 73 HttpClient client = new HttpClient(); |
73 return client.getUrl(resourceUri) | 74 return client.getUrl(resourceUri) |
74 .then((HttpClientRequest request) => request.close()) | 75 .then((HttpClientRequest request) => request.close()) |
75 .then((HttpClientResponse response) { | 76 .then((HttpClientResponse response) { |
76 if (response.statusCode != HttpStatus.OK) { | 77 if (response.statusCode != HttpStatus.OK) { |
77 String msg = 'Failure getting $resourceUri: ' | 78 String msg = 'Failure getting $resourceUri: ' |
78 '${response.statusCode} ${response.reasonPhrase}'; | 79 '${response.statusCode} ${response.reasonPhrase}'; |
79 throw msg; | 80 throw msg; |
80 } | 81 } |
81 return response.toList(); | 82 return response.toList(); |
82 }) | 83 }) |
83 .then((List<List<int>> splitContent) { | 84 .then((List<List<int>> splitContent) { |
84 int totalLength = splitContent.fold(0, (int old, List list) { | 85 int totalLength = splitContent.fold(0, (int old, List list) { |
85 return old + list.length; | 86 return old + list.length; |
86 }); | 87 }); |
87 Uint8List result = new Uint8List(totalLength); | 88 Uint8List result = new Uint8List(totalLength); |
88 int offset = 0; | 89 int offset = 0; |
89 for (List<int> contentPart in splitContent) { | 90 for (List<int> contentPart in splitContent) { |
90 result.setRange( | 91 result.setRange( |
91 offset, offset + contentPart.length, contentPart); | 92 offset, offset + contentPart.length, contentPart); |
92 offset += contentPart.length; | 93 offset += contentPart.length; |
93 } | 94 } |
94 dartCharactersRead += totalLength; | 95 dartCharactersRead += totalLength; |
95 sourceFiles[resourceUri.toString()] = | 96 sourceFiles[resourceUri] = |
96 new CachingUtf8BytesSourceFile(resourceUri.toString(), result); | 97 new CachingUtf8BytesSourceFile( |
| 98 resourceUri, resourceUri.toString(), result); |
97 return result; | 99 return result; |
98 }); | 100 }); |
99 } | 101 } |
100 | 102 |
101 Future/*<List<int> | String>*/ call(Uri resourceUri); | 103 Future/*<List<int> | String>*/ call(Uri resourceUri); |
102 | 104 |
103 relativizeUri(Uri uri) => relativize(cwd, uri, isWindows); | 105 relativizeUri(Uri uri) => relativize(cwd, uri, isWindows); |
104 } | 106 } |
105 | 107 |
106 class CompilerSourceFileProvider extends SourceFileProvider { | 108 class CompilerSourceFileProvider extends SourceFileProvider { |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 color = colors.green; | 197 color = colors.green; |
196 } else { | 198 } else { |
197 throw 'Unknown kind: $kind (${kind.ordinal})'; | 199 throw 'Unknown kind: $kind (${kind.ordinal})'; |
198 } | 200 } |
199 if (!enableColors) { | 201 if (!enableColors) { |
200 color = (x) => x; | 202 color = (x) => x; |
201 } | 203 } |
202 if (uri == null) { | 204 if (uri == null) { |
203 print('${color(message)}'); | 205 print('${color(message)}'); |
204 } else { | 206 } else { |
205 SourceFile file = provider.sourceFiles[uri.toString()]; | 207 SourceFile file = provider.sourceFiles[uri]; |
206 if (file != null) { | 208 if (file != null) { |
207 print(file.getLocationMessage( | 209 print(file.getLocationMessage( |
208 color(message), begin, end, colorize: color)); | 210 color(message), begin, end, colorize: color)); |
209 } else { | 211 } else { |
210 String position = end - begin > 0 ? '@$begin+${end - begin}' : ''; | 212 String position = end - begin > 0 ? '@$begin+${end - begin}' : ''; |
211 print('${provider.relativizeUri(uri)}$position:\n' | 213 print('${provider.relativizeUri(uri)}$position:\n' |
212 '${color(message)}'); | 214 '${color(message)}'); |
213 } | 215 } |
214 } | 216 } |
215 if (fatal && ++fatalCount >= throwOnErrorCount && throwOnError) { | 217 if (fatal && ++fatalCount >= throwOnErrorCount && throwOnError) { |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
323 var onAdd, onClose; | 325 var onAdd, onClose; |
324 | 326 |
325 EventSinkWrapper(this.onAdd, this.onClose); | 327 EventSinkWrapper(this.onAdd, this.onClose); |
326 | 328 |
327 void add(String data) => onAdd(data); | 329 void add(String data) => onAdd(data); |
328 | 330 |
329 void addError(error, [StackTrace stackTrace]) => throw error; | 331 void addError(error, [StackTrace stackTrace]) => throw error; |
330 | 332 |
331 void close() => onClose(); | 333 void close() => onClose(); |
332 } | 334 } |
OLD | NEW |