| 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:io'; | 8 import 'dart:io'; |
| 9 import 'dart:math' as math; | 9 import 'dart:math' as math; |
| 10 import 'dart:typed_data'; | 10 import 'dart:typed_data'; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 Future<api.Input> readBytesFromUri(Uri resourceUri, api.InputKind inputKind) { | 27 Future<api.Input> readBytesFromUri(Uri resourceUri, api.InputKind inputKind) { |
| 28 if (resourceUri.scheme == 'file') { | 28 if (resourceUri.scheme == 'file') { |
| 29 return _readFromFile(resourceUri, inputKind); | 29 return _readFromFile(resourceUri, inputKind); |
| 30 } else if (resourceUri.scheme == 'http' || resourceUri.scheme == 'https') { | 30 } else if (resourceUri.scheme == 'http' || resourceUri.scheme == 'https') { |
| 31 return _readFromHttp(resourceUri, inputKind); | 31 return _readFromHttp(resourceUri, inputKind); |
| 32 } else { | 32 } else { |
| 33 throw new ArgumentError("Unknown scheme in uri '$resourceUri'"); | 33 throw new ArgumentError("Unknown scheme in uri '$resourceUri'"); |
| 34 } | 34 } |
| 35 } | 35 } |
| 36 | 36 |
| 37 Future<api.Input> _readFromFile(Uri resourceUri, api.InputKind inputKind) { | 37 api.Input _readFromFileSync(Uri resourceUri, api.InputKind inputKind) { |
| 38 assert(resourceUri.scheme == 'file'); | 38 assert(resourceUri.scheme == 'file'); |
| 39 List<int> source; | 39 List<int> source; |
| 40 try { | 40 try { |
| 41 source = readAll(resourceUri.toFilePath(), | 41 source = readAll(resourceUri.toFilePath(), |
| 42 zeroTerminated: inputKind == api.InputKind.utf8); | 42 zeroTerminated: inputKind == api.InputKind.utf8); |
| 43 } on FileSystemException catch (ex) { | 43 } on FileSystemException catch (ex) { |
| 44 String message = ex.osError?.message; | 44 String message = ex.osError?.message; |
| 45 String detail = message != null ? ' ($message)' : ''; | 45 String detail = message != null ? ' ($message)' : ''; |
| 46 return new Future.error( | 46 throw "Error reading '${relativizeUri(resourceUri)}' $detail"; |
| 47 "Error reading '${relativizeUri(resourceUri)}' $detail"); | |
| 48 } | 47 } |
| 49 dartCharactersRead += source.length; | 48 dartCharactersRead += source.length; |
| 50 api.Input input; | 49 api.Input input; |
| 51 switch (inputKind) { | 50 switch (inputKind) { |
| 52 case api.InputKind.utf8: | 51 case api.InputKind.utf8: |
| 53 input = new CachingUtf8BytesSourceFile( | 52 input = new CachingUtf8BytesSourceFile( |
| 54 resourceUri, relativizeUri(resourceUri), source); | 53 resourceUri, relativizeUri(resourceUri), source); |
| 55 break; | 54 break; |
| 56 case api.InputKind.binary: | 55 case api.InputKind.binary: |
| 57 input = new Binary(resourceUri, source); | 56 input = new Binary(resourceUri, source); |
| 58 break; | 57 break; |
| 59 } | 58 } |
| 60 sourceFiles[resourceUri] = input; | 59 sourceFiles[resourceUri] = input; |
| 60 return input; |
| 61 } |
| 62 |
| 63 /// Read [resourceUri] directly as a UTF-8 file. If reading fails, `null` is |
| 64 /// returned. |
| 65 api.Input autoReadFromFile(Uri resourceUri) { |
| 66 try { |
| 67 return _readFromFileSync(resourceUri, InputKind.utf8); |
| 68 } catch (e) { |
| 69 // Silence the error. The [resourceUri] was not requested by the user and |
| 70 // was only needed to give better error messages. |
| 71 } |
| 72 return null; |
| 73 } |
| 74 |
| 75 Future<api.Input> _readFromFile(Uri resourceUri, api.InputKind inputKind) { |
| 76 api.Input input; |
| 77 try { |
| 78 input = _readFromFileSync(resourceUri, inputKind); |
| 79 } catch (e) { |
| 80 return new Future.error(e); |
| 81 } |
| 61 return new Future.value(input); | 82 return new Future.value(input); |
| 62 } | 83 } |
| 63 | 84 |
| 64 Future<api.Input> _readFromHttp(Uri resourceUri, api.InputKind inputKind) { | 85 Future<api.Input> _readFromHttp(Uri resourceUri, api.InputKind inputKind) { |
| 65 assert(resourceUri.scheme == 'http'); | 86 assert(resourceUri.scheme == 'http'); |
| 66 HttpClient client = new HttpClient(); | 87 HttpClient client = new HttpClient(); |
| 67 return client | 88 return client |
| 68 .getUrl(resourceUri) | 89 .getUrl(resourceUri) |
| 69 .then((HttpClientRequest request) => request.close()) | 90 .then((HttpClientRequest request) => request.close()) |
| 70 .then((HttpClientResponse response) { | 91 .then((HttpClientResponse response) { |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 } | 161 } |
| 141 | 162 |
| 142 class FormattingDiagnosticHandler implements CompilerDiagnostics { | 163 class FormattingDiagnosticHandler implements CompilerDiagnostics { |
| 143 final SourceFileProvider provider; | 164 final SourceFileProvider provider; |
| 144 bool showWarnings = true; | 165 bool showWarnings = true; |
| 145 bool showHints = true; | 166 bool showHints = true; |
| 146 bool verbose = false; | 167 bool verbose = false; |
| 147 bool isAborting = false; | 168 bool isAborting = false; |
| 148 bool enableColors = false; | 169 bool enableColors = false; |
| 149 bool throwOnError = false; | 170 bool throwOnError = false; |
| 171 bool autoReadFileUri = false; |
| 150 int throwOnErrorCount = 0; | 172 int throwOnErrorCount = 0; |
| 151 api.Diagnostic lastKind = null; | 173 api.Diagnostic lastKind = null; |
| 152 int fatalCount = 0; | 174 int fatalCount = 0; |
| 153 | 175 |
| 154 final int FATAL = api.Diagnostic.CRASH.ordinal | api.Diagnostic.ERROR.ordinal; | 176 final int FATAL = api.Diagnostic.CRASH.ordinal | api.Diagnostic.ERROR.ordinal; |
| 155 final int INFO = | 177 final int INFO = |
| 156 api.Diagnostic.INFO.ordinal | api.Diagnostic.VERBOSE_INFO.ordinal; | 178 api.Diagnostic.INFO.ordinal | api.Diagnostic.VERBOSE_INFO.ordinal; |
| 157 | 179 |
| 158 FormattingDiagnosticHandler([SourceFileProvider provider]) | 180 FormattingDiagnosticHandler([SourceFileProvider provider]) |
| 159 : this.provider = | 181 : this.provider = |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 225 } else { | 247 } else { |
| 226 throw 'Unknown kind: $kind (${kind.ordinal})'; | 248 throw 'Unknown kind: $kind (${kind.ordinal})'; |
| 227 } | 249 } |
| 228 if (!enableColors) { | 250 if (!enableColors) { |
| 229 color = (x) => x; | 251 color = (x) => x; |
| 230 } | 252 } |
| 231 if (uri == null) { | 253 if (uri == null) { |
| 232 print('${color(message)}'); | 254 print('${color(message)}'); |
| 233 } else { | 255 } else { |
| 234 api.Input file = provider.sourceFiles[uri]; | 256 api.Input file = provider.sourceFiles[uri]; |
| 257 if (file == null && |
| 258 autoReadFileUri && |
| 259 uri.scheme == 'file' && |
| 260 uri.path.endsWith('.dart')) { |
| 261 // When reading from .dill files, the original source files haven't been |
| 262 // loaded. Load the file if possible to provide a better error message. |
| 263 file = provider.autoReadFromFile(uri); |
| 264 } |
| 235 if (file is SourceFile) { | 265 if (file is SourceFile) { |
| 236 print(file.getLocationMessage(color(message), begin, end, | 266 print(file.getLocationMessage(color(message), begin, end, |
| 237 colorize: color)); | 267 colorize: color)); |
| 238 } else { | 268 } else { |
| 239 String position = end - begin > 0 ? '@$begin+${end - begin}' : ''; | 269 String position = end - begin > 0 ? '@$begin+${end - begin}' : ''; |
| 240 print('${provider.relativizeUri(uri)}$position:\n' | 270 print('${provider.relativizeUri(uri)}$position:\n' |
| 241 '${color(message)}'); | 271 '${color(message)}'); |
| 242 } | 272 } |
| 243 } | 273 } |
| 244 if (fatal && ++fatalCount >= throwOnErrorCount && throwOnError) { | 274 if (fatal && ++fatalCount >= throwOnErrorCount && throwOnError) { |
| (...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 443 resolvedUri = file; | 473 resolvedUri = file; |
| 444 break; | 474 break; |
| 445 } | 475 } |
| 446 } | 476 } |
| 447 } | 477 } |
| 448 api.Input result = await readBytesFromUri(resolvedUri, inputKind); | 478 api.Input result = await readBytesFromUri(resolvedUri, inputKind); |
| 449 sourceFiles[uri] = sourceFiles[resolvedUri]; | 479 sourceFiles[uri] = sourceFiles[resolvedUri]; |
| 450 return result; | 480 return result; |
| 451 } | 481 } |
| 452 } | 482 } |
| OLD | NEW |