OLD | NEW |
---|---|
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 /// A comprehensive, cross-platform path manipulation library. | 5 /// A comprehensive, cross-platform path manipulation library. |
6 /// | 6 /// |
7 /// ## Installing ## | 7 /// ## Installing ## |
8 /// | 8 /// |
9 /// Use [pub][] to install this package. Add the following to your | 9 /// Use [pub][] to install this package. Add the following to your |
10 /// `pubspec.yaml` file. | 10 /// `pubspec.yaml` file. |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
48 | 48 |
49 @MirrorsUsed(targets: 'dart.dom.html.window, ' | 49 @MirrorsUsed(targets: 'dart.dom.html.window, ' |
50 'dart.io.Directory.current, ' | 50 'dart.io.Directory.current, ' |
51 'dart.io.Platform.operatingSystem') | 51 'dart.io.Platform.operatingSystem') |
52 import 'dart:mirrors'; | 52 import 'dart:mirrors'; |
53 | 53 |
54 /// An internal builder for the current OS so we can provide a straight | 54 /// An internal builder for the current OS so we can provide a straight |
55 /// functional interface and not require users to create one. | 55 /// functional interface and not require users to create one. |
56 final _builder = new Builder(); | 56 final _builder = new Builder(); |
57 | 57 |
58 /** | 58 /// A default builder for manipulating POSIX paths. |
59 * Inserts [length] elements in front of the [list] and fills them with the | 59 final posix = new Builder(style: Style.posix); |
60 * [fillValue]. | 60 |
61 */ | 61 /// A default builder for manipulating Windows paths. |
62 final windows = new Builder(style: Style.windows); | |
63 | |
64 /// A default builder for manipulating URLs. | |
65 final url = new Builder(style: Style.url); | |
66 | |
67 /// Inserts [length] elements in front of the [list] and fills them with the | |
68 /// [fillValue]. | |
62 void _growListFront(List list, int length, fillValue) => | 69 void _growListFront(List list, int length, fillValue) => |
63 list.insertAll(0, new List.filled(length, fillValue)); | 70 list.insertAll(0, new List.filled(length, fillValue)); |
64 | 71 |
65 /// If we're running in the server-side Dart VM, this will return a | 72 /// If we're running in the server-side Dart VM, this will return a |
66 /// [LibraryMirror] that gives access to the `dart:io` library. | 73 /// [LibraryMirror] that gives access to the `dart:io` library. |
67 /// | 74 /// |
68 /// If `dart:io` is not available, this returns null. | 75 /// If `dart:io` is not available, this returns null. |
69 LibraryMirror get _io => currentMirrorSystem().libraries[Uri.parse('dart:io')]; | 76 LibraryMirror get _io => currentMirrorSystem().libraries[Uri.parse('dart:io')]; |
70 | 77 |
71 // TODO(nweiz): when issue 6490 or 6943 are fixed, make this work under dart2js. | 78 // TODO(nweiz): when issue 6490 or 6943 are fixed, make this work under dart2js. |
(...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
369 throw new ArgumentError(message.toString()); | 376 throw new ArgumentError(message.toString()); |
370 } | 377 } |
371 } | 378 } |
372 | 379 |
373 /// An instantiable class for manipulating paths. Unlike the top-level | 380 /// An instantiable class for manipulating paths. Unlike the top-level |
374 /// functions, this lets you explicitly select what platform the paths will use. | 381 /// functions, this lets you explicitly select what platform the paths will use. |
375 class Builder { | 382 class Builder { |
376 /// Creates a new path builder for the given style and root directory. | 383 /// Creates a new path builder for the given style and root directory. |
377 /// | 384 /// |
378 /// If [style] is omitted, it uses the host operating system's path style. If | 385 /// If [style] is omitted, it uses the host operating system's path style. If |
379 /// [root] is omitted, it defaults to the current working directory. If [root] | 386 /// [root] is omitted, it defaults ".". If *both* [style] and [root] are |
nweiz
2013/07/29 23:54:44
"If [root] is omitted" -> "If only [root] is omitt
Bob Nystrom
2013/07/30 01:04:27
Done.
| |
380 /// is relative, it is considered relative to the current working directory. | 387 /// omitted, [root] defaults to the current working directory. |
381 /// | 388 /// |
382 /// On the browser, the path style is [Style.url]. In Dartium, [root] defaults | 389 /// On the browser, the path style is [Style.url]. In Dartium, [root] defaults |
383 /// to the current URL. When using dart2js, it currently defaults to `.` due | 390 /// to the current URL. When using dart2js, it currently defaults to `.` due |
384 /// to technical constraints. | 391 /// to technical constraints. |
385 factory Builder({Style style, String root}) { | 392 factory Builder({Style style, String root}) { |
386 if (style == null) { | 393 if (root == null) { |
387 if (_io == null) { | 394 if (style == null) { |
388 style = Style.url; | 395 root = current; |
389 } else if (_io.classes[const Symbol('Platform')] | |
390 .getField(const Symbol('operatingSystem')).reflectee == 'windows') { | |
391 style = Style.windows; | |
392 } else { | 396 } else { |
393 style = Style.posix; | 397 root = "."; |
394 } | 398 } |
395 } | 399 } |
396 | 400 |
397 if (root == null) root = current; | 401 if (style == null) style = Style.platform; |
398 | 402 |
399 return new Builder._(style, root); | 403 return new Builder._(style, root); |
400 } | 404 } |
401 | 405 |
402 Builder._(this.style, this.root); | 406 Builder._(this.style, this.root); |
403 | 407 |
404 /// The style of path that this builder works with. | 408 /// The style of path that this builder works with. |
405 final Style style; | 409 final Style style; |
406 | 410 |
407 /// The root directory that relative paths will be relative to. | 411 /// The root directory that relative paths will be relative to. |
(...skipping 440 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
848 static final windows = new _WindowsStyle(); | 852 static final windows = new _WindowsStyle(); |
849 | 853 |
850 /// URLs aren't filesystem paths, but they're supported to make it easier to | 854 /// URLs aren't filesystem paths, but they're supported to make it easier to |
851 /// manipulate URL paths in the browser. | 855 /// manipulate URL paths in the browser. |
852 /// | 856 /// |
853 /// URLs use "/" (forward slash) as separators. Absolute paths either start | 857 /// URLs use "/" (forward slash) as separators. Absolute paths either start |
854 /// with a protocol and optional hostname (e.g. `http://dartlang.org`, | 858 /// with a protocol and optional hostname (e.g. `http://dartlang.org`, |
855 /// `file://`) or with "/". | 859 /// `file://`) or with "/". |
856 static final url = new _UrlStyle(); | 860 static final url = new _UrlStyle(); |
857 | 861 |
862 /// The style of the host platform. | |
863 /// | |
864 /// When running on the command line, this will be [windows] or [posix] based | |
865 /// on the host operating system. On a browser, this will be [url]. | |
866 static final platform = _getPlatformStyle(); | |
867 | |
868 /// Gets the type of the host platform. | |
869 static Style _getPlatformStyle() { | |
870 if (_io == null) return Style.url; | |
871 | |
872 if (_io.classes[const Symbol('Platform')] | |
873 .getField(const Symbol('operatingSystem')).reflectee == 'windows') { | |
874 return Style.windows; | |
875 } | |
876 | |
877 return Style.posix; | |
878 } | |
879 | |
858 /// The name of this path style. Will be "posix" or "windows". | 880 /// The name of this path style. Will be "posix" or "windows". |
859 String get name; | 881 String get name; |
860 | 882 |
861 /// The path separator for this style. On POSIX, this is `/`. On Windows, | 883 /// The path separator for this style. On POSIX, this is `/`. On Windows, |
862 /// it's `\`. | 884 /// it's `\`. |
863 String get separator; | 885 String get separator; |
864 | 886 |
865 /// The [Pattern] that can be used to match a separator for a path in this | 887 /// The [Pattern] that can be used to match a separator for a path in this |
866 /// style. Windows allows both "/" and "\" as path separators even though "\" | 888 /// style. Windows allows both "/" and "\" as path separators even though "\" |
867 /// is the canonical one. | 889 /// is the canonical one. |
(...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1176 // doesn't count. | 1198 // doesn't count. |
1177 if (lastDot <= 0) return [file, '']; | 1199 if (lastDot <= 0) return [file, '']; |
1178 | 1200 |
1179 return [file.substring(0, lastDot), file.substring(lastDot)]; | 1201 return [file.substring(0, lastDot), file.substring(lastDot)]; |
1180 } | 1202 } |
1181 | 1203 |
1182 _ParsedPath clone() => new _ParsedPath( | 1204 _ParsedPath clone() => new _ParsedPath( |
1183 style, root, isRootRelative, | 1205 style, root, isRootRelative, |
1184 new List.from(parts), new List.from(separators)); | 1206 new List.from(parts), new List.from(separators)); |
1185 } | 1207 } |
OLD | NEW |