| OLD | NEW | 
|    1 // Copyright (c) 2013, the Dart project authors.  Please see the AUTHORS file |    1 // Copyright (c) 2014, 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.style; |    5 library path.internal_style; | 
|    6  |    6  | 
|    7 import 'context.dart'; |    7 import 'context.dart'; | 
|    8 import 'style/posix.dart'; |    8 import 'style.dart'; | 
|    9 import 'style/url.dart'; |  | 
|   10 import 'style/windows.dart'; |  | 
|   11  |    9  | 
|   12 /// An enum type describing a "flavor" of path. |   10 /// The internal interface for the [Style] type. | 
|   13 abstract class Style { |   11 /// | 
|   14   /// POSIX-style paths use "/" (forward slash) as separators. Absolute paths |   12 /// Users should be able to pass around instances of [Style] like an enum, but | 
|   15   /// start with "/". Used by UNIX, Linux, Mac OS X, and others. |   13 /// the members that [Context] uses should be hidden from them. Those members | 
|   16   static final posix = new PosixStyle(); |   14 /// are defined on this class instead. | 
|   17  |   15 abstract class InternalStyle extends Style { | 
|   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, |   16   /// The path separator for this style. On POSIX, this is `/`. On Windows, | 
|   55   /// it's `\`. |   17   /// it's `\`. | 
|   56   String get separator; |   18   String get separator; | 
|   57  |   19  | 
|   58   /// The [Pattern] that can be used to match a separator for a path in this |   20   /// 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 "\" |   21   /// style. Windows allows both "/" and "\" as path separators even though "\" | 
|   60   /// is the canonical one. |   22   /// is the canonical one. | 
|   61   Pattern get separatorPattern; |   23   Pattern get separatorPattern; | 
|   62  |   24  | 
|   63   /// The [Pattern] that matches path components that need a separator after |   25   /// The [Pattern] that matches path components that need a separator after | 
| (...skipping 10 matching lines...) Expand all  Loading... | 
|   74   /// path in this style. |   36   /// path in this style. | 
|   75   Pattern get rootPattern; |   37   Pattern get rootPattern; | 
|   76  |   38  | 
|   77   /// The [Pattern] that can be used to match the root prefix of a root-relative |   39   /// The [Pattern] that can be used to match the root prefix of a root-relative | 
|   78   /// path in this style. |   40   /// path in this style. | 
|   79   /// |   41   /// | 
|   80   /// This can be null to indicate that this style doesn't support root-relative |   42   /// This can be null to indicate that this style doesn't support root-relative | 
|   81   /// paths. |   43   /// paths. | 
|   82   final Pattern relativeRootPattern = null; |   44   final Pattern relativeRootPattern = null; | 
|   83  |   45  | 
|   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, |   46   /// Gets the root prefix of [path] if path is absolute. If [path] is relative, | 
|   88   /// returns `null`. |   47   /// returns `null`. | 
|   89   String getRoot(String path) { |   48   String getRoot(String path) { | 
|   90     // TODO(rnystrom): Use firstMatch() when #7080 is fixed. |   49     // TODO(rnystrom): Use firstMatch() when #7080 is fixed. | 
|   91     var matches = rootPattern.allMatches(path); |   50     var matches = rootPattern.allMatches(path); | 
|   92     if (matches.isNotEmpty) return matches.first[0]; |   51     if (matches.isNotEmpty) return matches.first[0]; | 
|   93     return getRelativeRoot(path); |   52     return getRelativeRoot(path); | 
|   94   } |   53   } | 
|   95  |   54  | 
|   96   /// Gets the root prefix of [path] if it's root-relative. |   55   /// Gets the root prefix of [path] if it's root-relative. | 
|   97   /// |   56   /// | 
|   98   /// If [path] is relative or absolute and not root-relative, returns `null`. |   57   /// If [path] is relative or absolute and not root-relative, returns `null`. | 
|   99   String getRelativeRoot(String path) { |   58   String getRelativeRoot(String path) { | 
|  100     if (relativeRootPattern == null) return null; |   59     if (relativeRootPattern == null) return null; | 
|  101     // TODO(rnystrom): Use firstMatch() when #7080 is fixed. |   60     // TODO(rnystrom): Use firstMatch() when #7080 is fixed. | 
|  102     var matches = relativeRootPattern.allMatches(path); |   61     var matches = relativeRootPattern.allMatches(path); | 
|  103     if (matches.isEmpty) return null; |   62     if (matches.isEmpty) return null; | 
|  104     return matches.first[0]; |   63     return matches.first[0]; | 
|  105   } |   64   } | 
|  106  |   65  | 
|  107   /// Returns the path represented by [uri] in this style. |   66   /// Returns the path represented by [uri] in this style. | 
|  108   String pathFromUri(Uri uri); |   67   String pathFromUri(Uri uri); | 
|  109  |   68  | 
|  110   /// Returns the URI that represents the relative path made of [parts]. |   69   /// Returns the URI that represents the relative path made of [parts]. | 
|  111   Uri relativePathToUri(String path) => |   70   Uri relativePathToUri(String path) => | 
|  112       new Uri(pathSegments: context.split(path)); |   71       new Uri(pathSegments: context.split(path)); | 
|  113  |   72  | 
|  114   /// Returns the URI that represents [path], which is assumed to be absolute. |   73   /// Returns the URI that represents [path], which is assumed to be absolute. | 
|  115   Uri absolutePathToUri(String path); |   74   Uri absolutePathToUri(String path); | 
|  116  |  | 
|  117   String toString() => name; |  | 
|  118 } |   75 } | 
| OLD | NEW |