OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 part of dart.io; |
| 6 |
| 7 /** |
| 8 * Information about the environment in which the current program is running. |
| 9 * |
| 10 * Platform provides information such as the operating system, |
| 11 * the hostname of the computer, the value of environment variables, |
| 12 * the path to the running program, |
| 13 * and so on. |
| 14 * |
| 15 * ## Get the URI to the current Dart script |
| 16 * |
| 17 * Use the [script] getter to get the URI to the currently running |
| 18 * Dart script. |
| 19 * |
| 20 * import 'dart:io' show Platform; |
| 21 * |
| 22 * void main() { |
| 23 * // Get the URI of the script being run. |
| 24 * var uri = Platform.script; |
| 25 * // Convert the URI to a path. |
| 26 * var path = uri.toFilePath(); |
| 27 * } |
| 28 * |
| 29 * ## Get the value of an environment variable |
| 30 * |
| 31 * The [environment] getter returns a the names and values of environment |
| 32 * variables in a [Map] that contains key-value pairs of strings. The Map is |
| 33 * unmodifiable. This sample shows how to get the value of the `PATH` |
| 34 * environment variable. |
| 35 * |
| 36 * import 'dart:io' show Platform; |
| 37 * |
| 38 * void main() { |
| 39 * Map<String, String> envVars = Platform.environment; |
| 40 * print(envVars['PATH']); |
| 41 * } |
| 42 * |
| 43 * ## Determine the OS |
| 44 * |
| 45 * You can get the name of the operating system as a string with the |
| 46 * [operatingSystem] getter. You can also use one of the static boolean |
| 47 * getters: [isMacOS], [isLinux], and [isWindows]. |
| 48 * |
| 49 * import 'dart:io' show Platform, stdout; |
| 50 * |
| 51 * void main() { |
| 52 * // Get the operating system as a string. |
| 53 * String os = Platform.operatingSystem; |
| 54 * // Or, use a predicate getter. |
| 55 * if (Platform.isMacOS) { |
| 56 * print('is a Mac'); |
| 57 * } else { |
| 58 * print('is not a Mac'); |
| 59 * } |
| 60 * } |
| 61 * |
| 62 * ## Other resources |
| 63 * |
| 64 * [Dart by Example](https://www.dartlang.org/dart-by-example/#dart-io-and-comma
nd-line-apps) |
| 65 * provides additional task-oriented code samples that show how to use |
| 66 * various API from the [dart:io] library. |
| 67 */ |
| 68 class Platform { |
| 69 static final _numberOfProcessors = _Platform.numberOfProcessors; |
| 70 static final _pathSeparator = _Platform.pathSeparator; |
| 71 static final _operatingSystem = _Platform.operatingSystem; |
| 72 static final _localHostname = _Platform.localHostname; |
| 73 static final _version = _Platform.version; |
| 74 |
| 75 /** |
| 76 * Get the number of processors of the machine. |
| 77 */ |
| 78 static int get numberOfProcessors => _numberOfProcessors; |
| 79 |
| 80 /** |
| 81 * Get the path separator used by the operating system to separate |
| 82 * components in file paths. |
| 83 */ |
| 84 static String get pathSeparator => _pathSeparator; |
| 85 |
| 86 /** |
| 87 * Get a string (`linux`, `macos`, `windows`, `android`, or `ios`) |
| 88 * representing the operating system. |
| 89 */ |
| 90 static String get operatingSystem => _operatingSystem; |
| 91 |
| 92 /** |
| 93 * Get the local hostname for the system. |
| 94 */ |
| 95 static String get localHostname => _localHostname; |
| 96 |
| 97 /** |
| 98 * Returns true if the operating system is Linux. |
| 99 */ |
| 100 static final bool isLinux = (_operatingSystem == "linux"); |
| 101 |
| 102 /** |
| 103 * Returns true if the operating system is OS X. |
| 104 */ |
| 105 static final bool isMacOS = (_operatingSystem == "macos"); |
| 106 |
| 107 /** |
| 108 * Returns true if the operating system is Windows. |
| 109 */ |
| 110 static final bool isWindows = (_operatingSystem == "windows"); |
| 111 |
| 112 /** |
| 113 * Returns true if the operating system is Android. |
| 114 */ |
| 115 static final bool isAndroid = (_operatingSystem == "android"); |
| 116 |
| 117 /** |
| 118 * Returns true if the operating system is iOS. |
| 119 */ |
| 120 static final bool isIOS = (_operatingSystem == "ios"); |
| 121 |
| 122 /** |
| 123 * Get the environment for this process. |
| 124 * |
| 125 * The returned environment is an unmodifiable map which content is |
| 126 * retrieved from the operating system on its first use. |
| 127 * |
| 128 * Environment variables on Windows are case-insensitive. The map |
| 129 * returned on Windows is therefore case-insensitive and will convert |
| 130 * all keys to upper case. On other platforms the returned map is |
| 131 * a standard case-sensitive map. |
| 132 */ |
| 133 static Map<String, String> get environment => _Platform.environment; |
| 134 |
| 135 /** |
| 136 * Returns the path of the executable used to run the script in this |
| 137 * isolate. |
| 138 * |
| 139 * The path returned is the literal path used to run the script. This |
| 140 * path might be relative or just be a name from which the executable |
| 141 * was found by searching the `PATH`. |
| 142 * |
| 143 * To get the absolute path to the resolved executable use |
| 144 * [resolvedExecutable]. |
| 145 */ |
| 146 static String get executable => _Platform.executable; |
| 147 |
| 148 /** |
| 149 * Returns the path of the executable used to run the script in this |
| 150 * isolate after it has been resolved by the OS. |
| 151 * |
| 152 * This is the absolute path, with all symlinks resolved, to the |
| 153 * executable used to run the script. |
| 154 */ |
| 155 static String get resolvedExecutable => _Platform.resolvedExecutable; |
| 156 |
| 157 /** |
| 158 * Returns the absolute URI of the script being run in this |
| 159 * isolate. |
| 160 * |
| 161 * If the script argument on the command line is relative, |
| 162 * it is resolved to an absolute URI before fetching the script, and |
| 163 * this absolute URI is returned. |
| 164 * |
| 165 * URI resolution only does string manipulation on the script path, and this |
| 166 * may be different from the file system's path resolution behavior. For |
| 167 * example, a symbolic link immediately followed by '..' will not be |
| 168 * looked up. |
| 169 * |
| 170 * If the executable environment does not support [script] an empty |
| 171 * [Uri] is returned. |
| 172 */ |
| 173 static Uri get script => _Platform.script; |
| 174 |
| 175 /** |
| 176 * Returns the flags passed to the executable used to run the script in this |
| 177 * isolate. These are the command-line flags between the executable name |
| 178 * and the script name. Each fetch of executableArguments returns a new |
| 179 * List, containing the flags passed to the executable. |
| 180 */ |
| 181 static List<String> get executableArguments => _Platform.executableArguments; |
| 182 |
| 183 /** |
| 184 * Returns the value of the `--package-root` flag passed to the executable |
| 185 * used to run the script in this isolate. This is the directory in which |
| 186 * Dart packages are looked up. |
| 187 * |
| 188 * If there is no `--package-root` flag, `null` is returned. |
| 189 */ |
| 190 static String get packageRoot => _Platform.packageRoot; |
| 191 |
| 192 /** |
| 193 * Returns the value of the `--packages` flag passed to the executable |
| 194 * used to run the script in this isolate. This is the configuration which |
| 195 * specifies how Dart packages are looked up. |
| 196 * |
| 197 * If there is no `--packages` flag, `null` is returned. |
| 198 */ |
| 199 static String get packageConfig => _Platform.packageConfig; |
| 200 |
| 201 /** |
| 202 * Returns the version of the current Dart runtime. |
| 203 * |
| 204 * The returned `String` is formatted as the |
| 205 * [semver](http://semver.org) version string of the current dart |
| 206 * runtime, possibly followed by whitespace and other version and |
| 207 * build details. |
| 208 */ |
| 209 static String get version => _version; |
| 210 } |
OLD | NEW |