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 path.context; | 5 library path.context; |
6 | 6 |
7 import 'style.dart'; | 7 import 'style.dart'; |
8 import 'parsed_path.dart'; | 8 import 'parsed_path.dart'; |
9 import 'path_exception.dart'; | 9 import 'path_exception.dart'; |
10 import '../path.dart' as p; | 10 import '../path.dart' as p; |
(...skipping 454 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
465 /// context.toUri('http://dartlang.org/path/to/foo') | 465 /// context.toUri('http://dartlang.org/path/to/foo') |
466 /// // -> Uri.parse('http://dartlang.org/path/to/foo') | 466 /// // -> Uri.parse('http://dartlang.org/path/to/foo') |
467 Uri toUri(String path) { | 467 Uri toUri(String path) { |
468 if (isRelative(path)) { | 468 if (isRelative(path)) { |
469 return style.relativePathToUri(path); | 469 return style.relativePathToUri(path); |
470 } else { | 470 } else { |
471 return style.absolutePathToUri(join(current, path)); | 471 return style.absolutePathToUri(join(current, path)); |
472 } | 472 } |
473 } | 473 } |
474 | 474 |
| 475 /// Returns a terse, human-readable representation of [uri]. |
| 476 /// |
| 477 /// [uri] can be a [String] or a [Uri]. If it can be made relative to the |
| 478 /// current working directory, that's done. Otherwise, it's returned as-is. |
| 479 /// This gracefully handles non-`file:` URIs for [Style.posix] and |
| 480 /// [Style.windows]. |
| 481 /// |
| 482 /// The returned value is meant for human consumption, and may be either URI- |
| 483 /// or path-formatted. |
| 484 /// |
| 485 /// // POSIX |
| 486 /// var context = new Context(current: '/root/path'); |
| 487 /// context.prettyUri('file:///root/path/a/b.dart'); // -> 'a/b.dart' |
| 488 /// context.prettyUri('http://dartlang.org/'); // -> 'http://dartlang.org' |
| 489 /// |
| 490 /// // Windows |
| 491 /// var context = new Context(current: r'C:\root\path'); |
| 492 /// context.prettyUri('file:///C:/root/path/a/b.dart'); // -> r'a\b.dart' |
| 493 /// context.prettyUri('http://dartlang.org/'); // -> 'http://dartlang.org' |
| 494 /// |
| 495 /// // URL |
| 496 /// var context = new Context(current: 'http://dartlang.org/root/path'); |
| 497 /// context.prettyUri('http://dartlang.org/root/path/a/b.dart'); |
| 498 /// // -> r'a/b.dart' |
| 499 /// context.prettyUri('file:///root/path'); // -> 'file:///root/path' |
| 500 String prettyUri(uri) { |
| 501 if (uri is String) uri = Uri.parse(uri); |
| 502 if (uri.scheme == 'file' && style == Style.url) return uri.toString(); |
| 503 if (uri.scheme != 'file' && uri.scheme != '' && style != Style.url) { |
| 504 return uri.toString(); |
| 505 } |
| 506 |
| 507 var path = normalize(fromUri(uri)); |
| 508 var rel = relative(path); |
| 509 var components = split(rel); |
| 510 |
| 511 // Only return a relative path if it's actually shorter than the absolute |
| 512 // path. This avoids ugly things like long "../" chains to get to the root |
| 513 // and then go back down. |
| 514 return split(rel).length > split(path).length ? path : rel; |
| 515 } |
| 516 |
475 ParsedPath _parse(String path) => new ParsedPath.parse(path, style); | 517 ParsedPath _parse(String path) => new ParsedPath.parse(path, style); |
476 } | 518 } |
477 | 519 |
478 /// Validates that there are no non-null arguments following a null one and | 520 /// Validates that there are no non-null arguments following a null one and |
479 /// throws an appropriate [ArgumentError] on failure. | 521 /// throws an appropriate [ArgumentError] on failure. |
480 _validateArgList(String method, List<String> args) { | 522 _validateArgList(String method, List<String> args) { |
481 for (var i = 1; i < args.length; i++) { | 523 for (var i = 1; i < args.length; i++) { |
482 // Ignore nulls hanging off the end. | 524 // Ignore nulls hanging off the end. |
483 if (args[i] == null || args[i - 1] != null) continue; | 525 if (args[i] == null || args[i - 1] != null) continue; |
484 | 526 |
485 var numArgs; | 527 var numArgs; |
486 for (numArgs = args.length; numArgs >= 1; numArgs--) { | 528 for (numArgs = args.length; numArgs >= 1; numArgs--) { |
487 if (args[numArgs - 1] != null) break; | 529 if (args[numArgs - 1] != null) break; |
488 } | 530 } |
489 | 531 |
490 // Show the arguments. | 532 // Show the arguments. |
491 var message = new StringBuffer(); | 533 var message = new StringBuffer(); |
492 message.write("$method("); | 534 message.write("$method("); |
493 message.write(args.take(numArgs) | 535 message.write(args.take(numArgs) |
494 .map((arg) => arg == null ? "null" : '"$arg"') | 536 .map((arg) => arg == null ? "null" : '"$arg"') |
495 .join(", ")); | 537 .join(", ")); |
496 message.write("): part ${i - 1} was null, but part $i was not."); | 538 message.write("): part ${i - 1} was null, but part $i was not."); |
497 throw new ArgumentError(message.toString()); | 539 throw new ArgumentError(message.toString()); |
498 } | 540 } |
499 } | 541 } |
OLD | NEW |