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 class _Platform { | 7 class _Platform { |
8 external static int _numberOfProcessors(); | 8 external static int _numberOfProcessors(); |
9 external static String _pathSeparator(); | 9 external static String _pathSeparator(); |
10 external static String _operatingSystem(); | 10 external static String _operatingSystem(); |
11 external static _localHostname(); | 11 external static _localHostname(); |
12 external static _executable(); | 12 external static _executable(); |
13 external static _environment(); | 13 external static _environment(); |
14 external static List<String> _executableArguments(); | 14 external static List<String> _executableArguments(); |
15 external static String _packageRoot(); | 15 external static String _packageRoot(); |
16 external static String _version(); | 16 external static String _version(); |
17 | 17 |
| 18 // Cache the OS environemnt. This can be an OSError instance if |
| 19 // retrieving the environment failed. |
| 20 static var _environmentCache; |
| 21 |
18 static int get numberOfProcessors => _numberOfProcessors(); | 22 static int get numberOfProcessors => _numberOfProcessors(); |
19 static String get pathSeparator => _pathSeparator(); | 23 static String get pathSeparator => _pathSeparator(); |
20 static String get operatingSystem => _operatingSystem(); | 24 static String get operatingSystem => _operatingSystem(); |
21 static Uri script = _script(); | 25 static Uri script = _script(); |
22 static Uri _script() { | 26 static Uri _script() { |
23 // The embedder (Dart executable) creates the Platform._nativeScript field. | 27 // The embedder (Dart executable) creates the Platform._nativeScript field. |
24 var s = Platform._nativeScript; | 28 var s = Platform._nativeScript; |
25 if (s.startsWith('http:') || | 29 if (s.startsWith('http:') || |
26 s.startsWith('https:') || | 30 s.startsWith('https:') || |
27 s.startsWith('file:')) { | 31 s.startsWith('file:')) { |
(...skipping 10 matching lines...) Expand all Loading... |
38 } else { | 42 } else { |
39 return result; | 43 return result; |
40 } | 44 } |
41 } | 45 } |
42 | 46 |
43 static String executable = _executable(); | 47 static String executable = _executable(); |
44 static String packageRoot = _packageRoot(); | 48 static String packageRoot = _packageRoot(); |
45 static List<String> get executableArguments => _executableArguments(); | 49 static List<String> get executableArguments => _executableArguments(); |
46 | 50 |
47 static Map<String, String> get environment { | 51 static Map<String, String> get environment { |
48 var env = _environment(); | 52 if (_environmentCache == null) { |
49 if (env is OSError) { | 53 var env = _environment(); |
50 throw env; | 54 if (env is !OSError) { |
| 55 var isWindows = operatingSystem == 'windows'; |
| 56 var result = isWindows ? new _CaseInsensitiveStringMap() : new Map(); |
| 57 for (var str in env) { |
| 58 // When running on Windows through cmd.exe there are strange |
| 59 // environment variables that are used to record the current |
| 60 // working directory for each drive and the exit code for the |
| 61 // last command. As an example: '=A:=A:\subdir' records the |
| 62 // current working directory on the 'A' drive. In order to |
| 63 // handle these correctly we search for a second occurrence of |
| 64 // of '=' in the string if the first occurrence is at index 0. |
| 65 var equalsIndex = str.indexOf('='); |
| 66 if (equalsIndex == 0) { |
| 67 equalsIndex = str.indexOf('=', 1); |
| 68 } |
| 69 assert(equalsIndex != -1); |
| 70 result[str.substring(0, equalsIndex)] = |
| 71 str.substring(equalsIndex + 1); |
| 72 } |
| 73 _environmentCache = new _UnmodifiableMap(result); |
| 74 } else { |
| 75 _environmentCache = env; |
| 76 } |
| 77 } |
| 78 |
| 79 if (_environmentCache is OSError) { |
| 80 throw _environmentCache; |
51 } else { | 81 } else { |
52 var isWindows = operatingSystem == 'windows'; | 82 return _environmentCache; |
53 var result = isWindows ? new _CaseInsensitiveStringMap() : new Map(); | |
54 for (var str in env) { | |
55 // When running on Windows through cmd.exe there are strange | |
56 // environment variables that are used to record the current | |
57 // working directory for each drive and the exit code for the | |
58 // last command. As an example: '=A:=A:\subdir' records the | |
59 // current working directory on the 'A' drive. In order to | |
60 // handle these correctly we search for a second occurrence of | |
61 // of '=' in the string if the first occurrence is at index 0. | |
62 var equalsIndex = str.indexOf('='); | |
63 if (equalsIndex == 0) { | |
64 equalsIndex = str.indexOf('=', 1); | |
65 } | |
66 assert(equalsIndex != -1); | |
67 result[str.substring(0, equalsIndex)] = str.substring(equalsIndex + 1); | |
68 } | |
69 return result; | |
70 } | 83 } |
71 } | 84 } |
72 | 85 |
73 static String get version => _version(); | 86 static String get version => _version(); |
74 } | 87 } |
75 | 88 |
76 // Environment variables are case-insensitive on Windows. In order | 89 // Environment variables are case-insensitive on Windows. In order |
77 // to reflect that we use a case-insensitive string map on Windows. | 90 // to reflect that we use a case-insensitive string map on Windows. |
78 class _CaseInsensitiveStringMap<V> implements Map<String, V> { | 91 class _CaseInsensitiveStringMap<V> implements Map<String, V> { |
79 _CaseInsensitiveStringMap() : _map = new Map<String, V>(); | 92 _CaseInsensitiveStringMap() : _map = new Map<String, V>(); |
(...skipping 21 matching lines...) Expand all Loading... |
101 void clear() => _map.clear(); | 114 void clear() => _map.clear(); |
102 void forEach(void f(String key, V value)) => _map.forEach(f); | 115 void forEach(void f(String key, V value)) => _map.forEach(f); |
103 Iterable<String> get keys => _map.keys; | 116 Iterable<String> get keys => _map.keys; |
104 Iterable<V> get values => _map.values; | 117 Iterable<V> get values => _map.values; |
105 int get length => _map.length; | 118 int get length => _map.length; |
106 bool get isEmpty => _map.isEmpty; | 119 bool get isEmpty => _map.isEmpty; |
107 bool get isNotEmpty => _map.isNotEmpty; | 120 bool get isNotEmpty => _map.isNotEmpty; |
108 | 121 |
109 Map<String, V> _map; | 122 Map<String, V> _map; |
110 } | 123 } |
OLD | NEW |