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. When using dart2js, this |
89 /// currently returns `.` due to technical constraints. In the future, it will | 71 /// currently returns `.` due to technical constraints. In the future, it will |
90 /// return the current URL. | 72 /// return the current URL. |
91 String get current { | 73 String get current { |
92 if (_io != null) { | 74 var uri = Uri.base; |
93 return (_io.declarations[#Directory] as ClassMirror) | 75 if (Style.platform == Style.url) { |
94 .getField(#current).reflectee.path; | 76 return uri.toString(); |
95 } else if (_html != null) { | |
96 return _html.getField(#window).reflectee.location.href; | |
97 } else { | 77 } else { |
98 return '.'; | 78 var path = uri.toFilePath(); |
79 // Remove trailing '/' or '\'. | |
80 return path.substring(0, path.length - 1); | |
99 } | 81 } |
100 } | 82 } |
101 | 83 |
102 /// Gets the path separator for the current platform. This is `\` on Windows | 84 /// Gets the path separator for the current platform. This is `\` on Windows |
103 /// and `/` on other platforms (including the browser). | 85 /// and `/` on other platforms (including the browser). |
104 String get separator => _builder.separator; | 86 String get separator => _builder.separator; |
105 | 87 |
106 /// Converts [path] to an absolute path by resolving it relative to the current | 88 /// 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. | 89 /// working directory. If [path] is already an absolute path, just returns it. |
108 /// | 90 /// |
(...skipping 750 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
859 static final url = new _UrlStyle(); | 841 static final url = new _UrlStyle(); |
860 | 842 |
861 /// The style of the host platform. | 843 /// The style of the host platform. |
862 /// | 844 /// |
863 /// When running on the command line, this will be [windows] or [posix] based | 845 /// 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]. | 846 /// on the host operating system. On a browser, this will be [url]. |
865 static final platform = _getPlatformStyle(); | 847 static final platform = _getPlatformStyle(); |
866 | 848 |
867 /// Gets the type of the host platform. | 849 /// Gets the type of the host platform. |
868 static Style _getPlatformStyle() { | 850 static Style _getPlatformStyle() { |
869 if (_io == null) return Style.url; | 851 if (Uri.base.scheme != 'file') return Style.url; |
Bob Nystrom
2013/11/06 18:00:36
This does the wrong thing if you open a file in Da
Anders Johnsen
2013/11/06 18:07:10
Ahh, interesting point. I'll see if I can come up
| |
870 | 852 if (new Uri.file('a/b').toFilePath() == 'a\\b') return Style.windows; |
871 if ((_io.declarations[#Platform] as ClassMirror).getField(#operatingSystem) | |
872 .reflectee == 'windows') { | |
873 return Style.windows; | |
874 } | |
875 | |
876 return Style.posix; | 853 return Style.posix; |
877 } | 854 } |
878 | 855 |
879 /// The name of this path style. Will be "posix" or "windows". | 856 /// The name of this path style. Will be "posix" or "windows". |
880 String get name; | 857 String get name; |
881 | 858 |
882 /// The path separator for this style. On POSIX, this is `/`. On Windows, | 859 /// The path separator for this style. On POSIX, this is `/`. On Windows, |
883 /// it's `\`. | 860 /// it's `\`. |
884 String get separator; | 861 String get separator; |
885 | 862 |
(...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1197 // doesn't count. | 1174 // doesn't count. |
1198 if (lastDot <= 0) return [file, '']; | 1175 if (lastDot <= 0) return [file, '']; |
1199 | 1176 |
1200 return [file.substring(0, lastDot), file.substring(lastDot)]; | 1177 return [file.substring(0, lastDot), file.substring(lastDot)]; |
1201 } | 1178 } |
1202 | 1179 |
1203 _ParsedPath clone() => new _ParsedPath( | 1180 _ParsedPath clone() => new _ParsedPath( |
1204 style, root, isRootRelative, | 1181 style, root, isRootRelative, |
1205 new List.from(parts), new List.from(separators)); | 1182 new List.from(parts), new List.from(separators)); |
1206 } | 1183 } |
OLD | NEW |