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 part of dart.io; | 5 part of dart.io; |
6 | 6 |
7 /** | 7 /** |
8 * The [Platform] class exposes details of the machine and operating | 8 * Information about the environment in which the current program is running. |
9 * system. | 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. |
10 */ | 67 */ |
11 class Platform { | 68 class Platform { |
12 static final _numberOfProcessors = _Platform.numberOfProcessors; | 69 static final _numberOfProcessors = _Platform.numberOfProcessors; |
13 static final _pathSeparator = _Platform.pathSeparator; | 70 static final _pathSeparator = _Platform.pathSeparator; |
14 static final _operatingSystem = _Platform.operatingSystem; | 71 static final _operatingSystem = _Platform.operatingSystem; |
15 static final _localHostname = _Platform.localHostname; | 72 static final _localHostname = _Platform.localHostname; |
16 static final _version = _Platform.version; | 73 static final _version = _Platform.version; |
17 | 74 |
18 // This script singleton is written to by the embedder if applicable. | 75 // This script singleton is written to by the embedder if applicable. |
19 static String _nativeScript = ''; | 76 static String _nativeScript = ''; |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 static bool get isWindows => _operatingSystem == "windows"; | 113 static bool get isWindows => _operatingSystem == "windows"; |
57 | 114 |
58 /** | 115 /** |
59 * Returns true if the operating system is Android. | 116 * Returns true if the operating system is Android. |
60 */ | 117 */ |
61 static bool get isAndroid => _operatingSystem == "android"; | 118 static bool get isAndroid => _operatingSystem == "android"; |
62 | 119 |
63 /** | 120 /** |
64 * Get the environment for this process. | 121 * Get the environment for this process. |
65 * | 122 * |
66 * The returned environment is a unmodifiable map which content is | 123 * The returned environment is an unmodifiable map which content is |
67 * retrieved from the operating system on its first use. | 124 * retrieved from the operating system on its first use. |
68 * | 125 * |
69 * Environment variables on Windows are case-insensitive. The map | 126 * Environment variables on Windows are case-insensitive. The map |
70 * returned on Windows is therefore case-insensitive and will convert | 127 * returned on Windows is therefore case-insensitive and will convert |
71 * all keys to upper case. On other platforms the returned map is | 128 * all keys to upper case. On other platforms the returned map is |
72 * a standard case-sensitive map. | 129 * a standard case-sensitive map. |
73 */ | 130 */ |
74 static Map<String, String> get environment => _Platform.environment; | 131 static Map<String, String> get environment => _Platform.environment; |
75 | 132 |
76 /** | 133 /** |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 * | 172 * |
116 * If there is no --package-root flag, then the empty string is returned. | 173 * If there is no --package-root flag, then the empty string is returned. |
117 */ | 174 */ |
118 static String get packageRoot => _Platform.packageRoot; | 175 static String get packageRoot => _Platform.packageRoot; |
119 | 176 |
120 /** | 177 /** |
121 * Returns the version of the current Dart runtime. | 178 * Returns the version of the current Dart runtime. |
122 */ | 179 */ |
123 static String get version => _version; | 180 static String get version => _version; |
124 } | 181 } |
OLD | NEW |