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

Side by Side Diff: pkg/path/lib/src/context.dart

Issue 296233002: Add a path.formatUri method and release path 1.2.0. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 7 months 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
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 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
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 human-readable representation of [uri].
476 ///
477 /// If [uri] can be made relative to the current working directory, that's
478 /// done. Otherwise, it's returned as-is. This gracefully handles non-`file:`
479 /// URIs for [Style.posix] and [Style.windows].
480 ///
481 /// The returned value is meant for human consumption, and may be either URI-
482 /// or path-formatted.
483 ///
484 /// // POSIX
485 /// var context = new Context(current: '/root/path');
486 /// context.formatUri('file:///root/path/a/b.dart'); // -> 'a/b.dart'
487 /// context.formatUri('http://dartlang.org/'); // -> 'http://dartlang.org'
488 ///
489 /// // Windows
490 /// var context = new Context(current: r'C:\root\path');
491 /// context.formatUri('file:///C:/root/path/a/b.dart'); // -> r'a\b.dart'
492 /// context.formatUri('http://dartlang.org/'); // -> 'http://dartlang.org'
493 ///
494 /// // URL
495 /// var context = new Context(current: 'http://dartlang.org/root/path');
496 /// context.formatUri('http://dartlang.org/root/path/a/b.dart');
497 /// // -> r'a/b.dart'
498 /// context.formatUri('file:///root/path'); // -> 'file:///root/path'
499 String formatUri(uri) {
500 if (uri is String) uri = Uri.parse(uri);
501 if (uri.scheme == 'file' && style == Style.url) return uri.toString();
502 if (uri.scheme != 'file' && uri.scheme != '' && style != Style.url) {
503 return uri.toString();
504 }
505
506 var path = fromUri(uri);
507 var rel = relative(path);
508 var components = split(rel);
509
510 // Return whichever of the relative path and the original path has the
511 // fewest components. This avoids ugly things like long "../" chains to get
Bob Nystrom 2014/05/23 21:57:52 How about replacing the first sentence with: "Onl
nweiz 2014/05/23 22:18:04 Done.
512 // to the root and then go back down.
513 return split(rel).length > split(path).length ? path : rel;
514 }
515
475 ParsedPath _parse(String path) => new ParsedPath.parse(path, style); 516 ParsedPath _parse(String path) => new ParsedPath.parse(path, style);
476 } 517 }
477 518
478 /// Validates that there are no non-null arguments following a null one and 519 /// Validates that there are no non-null arguments following a null one and
479 /// throws an appropriate [ArgumentError] on failure. 520 /// throws an appropriate [ArgumentError] on failure.
480 _validateArgList(String method, List<String> args) { 521 _validateArgList(String method, List<String> args) {
481 for (var i = 1; i < args.length; i++) { 522 for (var i = 1; i < args.length; i++) {
482 // Ignore nulls hanging off the end. 523 // Ignore nulls hanging off the end.
483 if (args[i] == null || args[i - 1] != null) continue; 524 if (args[i] == null || args[i - 1] != null) continue;
484 525
485 var numArgs; 526 var numArgs;
486 for (numArgs = args.length; numArgs >= 1; numArgs--) { 527 for (numArgs = args.length; numArgs >= 1; numArgs--) {
487 if (args[numArgs - 1] != null) break; 528 if (args[numArgs - 1] != null) break;
488 } 529 }
489 530
490 // Show the arguments. 531 // Show the arguments.
491 var message = new StringBuffer(); 532 var message = new StringBuffer();
492 message.write("$method("); 533 message.write("$method(");
493 message.write(args.take(numArgs) 534 message.write(args.take(numArgs)
494 .map((arg) => arg == null ? "null" : '"$arg"') 535 .map((arg) => arg == null ? "null" : '"$arg"')
495 .join(", ")); 536 .join(", "));
496 message.write("): part ${i - 1} was null, but part $i was not."); 537 message.write("): part ${i - 1} was null, but part $i was not.");
497 throw new ArgumentError(message.toString()); 538 throw new ArgumentError(message.toString());
498 } 539 }
499 } 540 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698