Chromium Code Reviews| 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 28 matching lines...) Expand all Loading... | |
| 39 /// underlying platform that the program is running on, you can create a | 39 /// underlying platform that the program is running on, you can create a |
| 40 /// [Builder] and give it an explicit [Style]: | 40 /// [Builder] and give it an explicit [Style]: |
| 41 /// | 41 /// |
| 42 /// var builder = new path.Builder(style: Style.windows); | 42 /// var builder = new path.Builder(style: Style.windows); |
| 43 /// builder.join("directory", "file.txt"); | 43 /// builder.join("directory", "file.txt"); |
| 44 /// | 44 /// |
| 45 /// This will join "directory" and "file.txt" using the Windows path separator, | 45 /// This will join "directory" and "file.txt" using the Windows path separator, |
| 46 /// even when the program is run on a POSIX machine. | 46 /// even when the program is run on a POSIX machine. |
| 47 library path; | 47 library path; |
| 48 | 48 |
| 49 @MirrorsUsed(targets: 'dart.dom.html.window, ' | |
| 50 'dart.io.Directory.current, ' | |
| 51 'dart.io.Platform.operatingSystem') | |
| 52 import 'dart:mirrors'; | |
| 53 | |
| 54 /// An internal builder for the current OS so we can provide a straight | 49 /// An internal builder for the current OS so we can provide a straight |
| 55 /// functional interface and not require users to create one. | 50 /// functional interface and not require users to create one. |
| 56 final _builder = new Builder(); | 51 final _builder = new Builder(); |
| 57 | 52 |
| 58 /// A default builder for manipulating POSIX paths. | 53 /// A default builder for manipulating POSIX paths. |
| 59 final posix = new Builder(style: Style.posix); | 54 final posix = new Builder(style: Style.posix); |
| 60 | 55 |
| 61 /// A default builder for manipulating Windows paths. | 56 /// A default builder for manipulating Windows paths. |
| 62 final windows = new Builder(style: Style.windows); | 57 final windows = new Builder(style: Style.windows); |
| 63 | 58 |
| 64 /// A default builder for manipulating URLs. | 59 /// A default builder for manipulating URLs. |
| 65 final url = new Builder(style: Style.url); | 60 final url = new Builder(style: Style.url); |
| 66 | 61 |
| 67 /// Inserts [length] elements in front of the [list] and fills them with the | 62 /// Inserts [length] elements in front of the [list] and fills them with the |
| 68 /// [fillValue]. | 63 /// [fillValue]. |
| 69 void _growListFront(List list, int length, fillValue) => | 64 void _growListFront(List list, int length, fillValue) => |
| 70 list.insertAll(0, new List.filled(length, fillValue)); | 65 list.insertAll(0, new List.filled(length, fillValue)); |
| 71 | 66 |
| 72 /// If we're running in the server-side Dart VM, this will return a | |
| 73 /// [LibraryMirror] that gives access to the `dart:io` library. | |
| 74 /// | |
| 75 /// If `dart:io` is not available, this returns null. | |
| 76 LibraryMirror get _io => currentMirrorSystem().libraries[Uri.parse('dart:io')]; | |
| 77 | |
| 78 // TODO(nweiz): when issue 6490 or 6943 are fixed, make this work under dart2js. | |
| 79 /// If we're running in Dartium, this will return a [LibraryMirror] that gives | |
| 80 /// access to the `dart:html` library. | |
| 81 /// | |
| 82 /// If `dart:html` is not available, this returns null. | |
| 83 LibraryMirror get _html => | |
| 84 currentMirrorSystem().libraries[Uri.parse('dart:html')]; | |
| 85 | 67 |
| 86 /// Gets the path to the current working directory. | 68 /// Gets the path to the current working directory. |
| 87 /// | 69 /// |
| 88 /// In the browser, this means the current URL. When using dart2js, this | 70 /// In the browser, this means the current URL, without the last file segment. |
| 89 /// currently returns `.` due to technical constraints. In the future, it will | |
| 90 /// return the current URL. | |
| 91 String get current { | 71 String get current { |
| 92 if (_io != null) { | 72 var uri = Uri.base; |
| 93 return (_io.declarations[#Directory] as ClassMirror) | 73 if (Style.platform == Style.url) { |
| 94 .getField(#current).reflectee.path; | 74 return uri.resolve('.').toString(); |
| 95 } else if (_html != null) { | |
| 96 return _html.getField(#window).reflectee.location.href; | |
| 97 } else { | 75 } else { |
| 98 return '.'; | 76 var path = uri.toFilePath(); |
| 77 // Remove trailing '/' or '\'. | |
| 78 assert(path[path.length - 1] == '/' || path[path.length - 1] == '\\'); | |
|
kasperl
2013/11/07 08:06:58
Maybe put 'path.length - 1' in a local variable (l
Anders Johnsen
2013/11/07 08:10:45
Done.
| |
| 79 return path.substring(0, path.length - 1); | |
| 99 } | 80 } |
| 100 } | 81 } |
| 101 | 82 |
| 102 /// Gets the path separator for the current platform. This is `\` on Windows | 83 /// Gets the path separator for the current platform. This is `\` on Windows |
| 103 /// and `/` on other platforms (including the browser). | 84 /// and `/` on other platforms (including the browser). |
| 104 String get separator => _builder.separator; | 85 String get separator => _builder.separator; |
| 105 | 86 |
| 106 /// Converts [path] to an absolute path by resolving it relative to the current | 87 /// Converts [path] to an absolute path by resolving it relative to the current |
| 107 /// working directory. If [path] is already an absolute path, just returns it. | 88 /// working directory. If [path] is already an absolute path, just returns it. |
| 108 /// | 89 /// |
| (...skipping 750 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 859 static final url = new _UrlStyle(); | 840 static final url = new _UrlStyle(); |
| 860 | 841 |
| 861 /// The style of the host platform. | 842 /// The style of the host platform. |
| 862 /// | 843 /// |
| 863 /// When running on the command line, this will be [windows] or [posix] based | 844 /// When running on the command line, this will be [windows] or [posix] based |
| 864 /// on the host operating system. On a browser, this will be [url]. | 845 /// on the host operating system. On a browser, this will be [url]. |
| 865 static final platform = _getPlatformStyle(); | 846 static final platform = _getPlatformStyle(); |
| 866 | 847 |
| 867 /// Gets the type of the host platform. | 848 /// Gets the type of the host platform. |
| 868 static Style _getPlatformStyle() { | 849 static Style _getPlatformStyle() { |
| 869 if (_io == null) return Style.url; | 850 // If we're running a Dart file in the browser from a `file:` URI, |
| 870 | 851 // [Uri.base] will point to a file. If we're running on the standalone, |
| 871 if ((_io.declarations[#Platform] as ClassMirror).getField(#operatingSystem) | 852 // it will point to a directory. We can use that fact to determine which |
| 872 .reflectee == 'windows') { | 853 // style to use. |
| 873 return Style.windows; | 854 if (Uri.base.scheme != 'file') return Style.url; |
| 874 } | 855 if (!Uri.base.path.endsWith('/')) return Style.url; |
| 875 | 856 if (new Uri(path: 'a/b').toFilePath() == 'a\\b') return Style.windows; |
| 876 return Style.posix; | 857 return Style.posix; |
| 877 } | 858 } |
| 878 | 859 |
| 879 /// The name of this path style. Will be "posix" or "windows". | 860 /// The name of this path style. Will be "posix" or "windows". |
| 880 String get name; | 861 String get name; |
| 881 | 862 |
| 882 /// The path separator for this style. On POSIX, this is `/`. On Windows, | 863 /// The path separator for this style. On POSIX, this is `/`. On Windows, |
| 883 /// it's `\`. | 864 /// it's `\`. |
| 884 String get separator; | 865 String get separator; |
| 885 | 866 |
| (...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1197 // doesn't count. | 1178 // doesn't count. |
| 1198 if (lastDot <= 0) return [file, '']; | 1179 if (lastDot <= 0) return [file, '']; |
| 1199 | 1180 |
| 1200 return [file.substring(0, lastDot), file.substring(lastDot)]; | 1181 return [file.substring(0, lastDot), file.substring(lastDot)]; |
| 1201 } | 1182 } |
| 1202 | 1183 |
| 1203 _ParsedPath clone() => new _ParsedPath( | 1184 _ParsedPath clone() => new _ParsedPath( |
| 1204 style, root, isRootRelative, | 1185 style, root, isRootRelative, |
| 1205 new List.from(parts), new List.from(separators)); | 1186 new List.from(parts), new List.from(separators)); |
| 1206 } | 1187 } |
| OLD | NEW |