Line data Source code
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 : import 'context.dart'; 6 : import 'style/posix.dart'; 7 : import 'style/url.dart'; 8 : import 'style/windows.dart'; 9 : 10 : /// An enum type describing a "flavor" of path. 11 : abstract class Style { 12 : /// POSIX-style paths use "/" (forward slash) as separators. Absolute paths 13 : /// start with "/". Used by UNIX, Linux, Mac OS X, and others. 14 33 : static final Style posix = PosixStyle(); 15 : 16 : /// Windows paths use `\` (backslash) as separators. Absolute paths start with 17 : /// a drive letter followed by a colon (example, `C:`) or two backslashes 18 : /// (`\\`) for UNC paths. 19 33 : static final Style windows = WindowsStyle(); 20 : 21 : /// URLs aren't filesystem paths, but they're supported to make it easier to 22 : /// manipulate URL paths in the browser. 23 : /// 24 : /// URLs use "/" (forward slash) as separators. Absolute paths either start 25 : /// with a protocol and optional hostname (e.g. `https://dart.dev`, 26 : /// `file://`) or with "/". 27 33 : static final Style url = UrlStyle(); 28 : 29 : /// The style of the host platform. 30 : /// 31 : /// When running on the command line, this will be [windows] or [posix] based 32 : /// on the host operating system. On a browser, this will be [url]. 33 33 : static final Style platform = _getPlatformStyle(); 34 : 35 : /// Gets the type of the host platform. 36 11 : static Style _getPlatformStyle() { 37 : // If we're running a Dart file in the browser from a `file:` URI, 38 : // [Uri.base] will point to a file. If we're running on the standalone, 39 : // it will point to a directory. We can use that fact to determine which 40 : // style to use. 41 33 : if (Uri.base.scheme != 'file') return Style.url; 42 33 : if (!Uri.base.path.endsWith('/')) return Style.url; 43 33 : if (Uri(path: 'a/b').toFilePath() == 'a\\b') return Style.windows; 44 11 : return Style.posix; 45 : } 46 : 47 : /// The name of this path style. Will be "posix" or "windows". 48 : String get name; 49 : 50 : /// A [Context] that uses this style. 51 0 : Context get context => Context(style: this); 52 : 53 : @Deprecated('Most Style members will be removed in path 2.0.') 54 : String get separator; 55 : 56 : @Deprecated('Most Style members will be removed in path 2.0.') 57 : Pattern get separatorPattern; 58 : 59 : @Deprecated('Most Style members will be removed in path 2.0.') 60 : Pattern get needsSeparatorPattern; 61 : 62 : @Deprecated('Most Style members will be removed in path 2.0.') 63 : Pattern get rootPattern; 64 : 65 : @Deprecated('Most Style members will be removed in path 2.0.') 66 : Pattern? get relativeRootPattern; 67 : 68 : @Deprecated('Most style members will be removed in path 2.0.') 69 : String? getRoot(String path); 70 : 71 : @Deprecated('Most style members will be removed in path 2.0.') 72 : String? getRelativeRoot(String path); 73 : 74 : @Deprecated('Most style members will be removed in path 2.0.') 75 : String pathFromUri(Uri uri); 76 : 77 : @Deprecated('Most style members will be removed in path 2.0.') 78 : Uri relativePathToUri(String path); 79 : 80 : @Deprecated('Most style members will be removed in path 2.0.') 81 : Uri absolutePathToUri(String path); 82 : 83 0 : @override 84 0 : String toString() => name; 85 : }