OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library path.style; |
| 6 |
| 7 import 'context.dart'; |
| 8 import 'style/posix.dart'; |
| 9 import 'style/url.dart'; |
| 10 import 'style/windows.dart'; |
| 11 |
| 12 /// An enum type describing a "flavor" of path. |
| 13 abstract class Style { |
| 14 /// POSIX-style paths use "/" (forward slash) as separators. Absolute paths |
| 15 /// start with "/". Used by UNIX, Linux, Mac OS X, and others. |
| 16 static final posix = new PosixStyle(); |
| 17 |
| 18 /// Windows paths use "\" (backslash) as separators. Absolute paths start with |
| 19 /// a drive letter followed by a colon (example, "C:") or two backslashes |
| 20 /// ("\\") for UNC paths. |
| 21 // TODO(rnystrom): The UNC root prefix should include the drive name too, not |
| 22 // just the "\\". |
| 23 static final windows = new WindowsStyle(); |
| 24 |
| 25 /// URLs aren't filesystem paths, but they're supported to make it easier to |
| 26 /// manipulate URL paths in the browser. |
| 27 /// |
| 28 /// URLs use "/" (forward slash) as separators. Absolute paths either start |
| 29 /// with a protocol and optional hostname (e.g. `http://dartlang.org`, |
| 30 /// `file://`) or with "/". |
| 31 static final url = new UrlStyle(); |
| 32 |
| 33 /// The style of the host platform. |
| 34 /// |
| 35 /// When running on the command line, this will be [windows] or [posix] based |
| 36 /// on the host operating system. On a browser, this will be [url]. |
| 37 static final platform = _getPlatformStyle(); |
| 38 |
| 39 /// Gets the type of the host platform. |
| 40 static Style _getPlatformStyle() { |
| 41 // If we're running a Dart file in the browser from a `file:` URI, |
| 42 // [Uri.base] will point to a file. If we're running on the standalone, |
| 43 // it will point to a directory. We can use that fact to determine which |
| 44 // style to use. |
| 45 if (Uri.base.scheme != 'file') return Style.url; |
| 46 if (!Uri.base.path.endsWith('/')) return Style.url; |
| 47 if (new Uri(path: 'a/b').toFilePath() == 'a\\b') return Style.windows; |
| 48 return Style.posix; |
| 49 } |
| 50 |
| 51 /// The name of this path style. Will be "posix" or "windows". |
| 52 String get name; |
| 53 |
| 54 /// The path separator for this style. On POSIX, this is `/`. On Windows, |
| 55 /// it's `\`. |
| 56 String get separator; |
| 57 |
| 58 /// The [Pattern] that can be used to match a separator for a path in this |
| 59 /// style. Windows allows both "/" and "\" as path separators even though "\" |
| 60 /// is the canonical one. |
| 61 Pattern get separatorPattern; |
| 62 |
| 63 /// The [Pattern] that matches path components that need a separator after |
| 64 /// them. |
| 65 /// |
| 66 /// Windows and POSIX styles just need separators when the previous component |
| 67 /// doesn't already end in a separator, but the URL always needs to place a |
| 68 /// separator between the root and the first component, even if the root |
| 69 /// already ends in a separator character. For example, to join "file://" and |
| 70 /// "usr", an additional "/" is needed (making "file:///usr"). |
| 71 Pattern get needsSeparatorPattern; |
| 72 |
| 73 /// The [Pattern] that can be used to match the root prefix of an absolute |
| 74 /// path in this style. |
| 75 Pattern get rootPattern; |
| 76 |
| 77 /// The [Pattern] that can be used to match the root prefix of a root-relative |
| 78 /// path in this style. |
| 79 /// |
| 80 /// This can be null to indicate that this style doesn't support root-relative |
| 81 /// paths. |
| 82 final Pattern relativeRootPattern = null; |
| 83 |
| 84 /// A [Context] that uses this style. |
| 85 Context get context => new Context(style: this); |
| 86 |
| 87 /// Gets the root prefix of [path] if path is absolute. If [path] is relative, |
| 88 /// returns `null`. |
| 89 String getRoot(String path) { |
| 90 // TODO(rnystrom): Use firstMatch() when #7080 is fixed. |
| 91 var matches = rootPattern.allMatches(path); |
| 92 if (matches.isNotEmpty) return matches.first[0]; |
| 93 return getRelativeRoot(path); |
| 94 } |
| 95 |
| 96 /// Gets the root prefix of [path] if it's root-relative. |
| 97 /// |
| 98 /// If [path] is relative or absolute and not root-relative, returns `null`. |
| 99 String getRelativeRoot(String path) { |
| 100 if (relativeRootPattern == null) return null; |
| 101 // TODO(rnystrom): Use firstMatch() when #7080 is fixed. |
| 102 var matches = relativeRootPattern.allMatches(path); |
| 103 if (matches.isEmpty) return null; |
| 104 return matches.first[0]; |
| 105 } |
| 106 |
| 107 /// Returns the path represented by [uri] in this style. |
| 108 String pathFromUri(Uri uri); |
| 109 |
| 110 /// Returns the URI that represents the relative path made of [parts]. |
| 111 Uri relativePathToUri(String path) => |
| 112 new Uri(pathSegments: context.split(path)); |
| 113 |
| 114 /// Returns the URI that represents [path], which is assumed to be absolute. |
| 115 Uri absolutePathToUri(String path); |
| 116 |
| 117 String toString() => name; |
| 118 } |
OLD | NEW |