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)] = str.substring(equalsIndex + 1) ; | |
Anders Johnsen
2014/01/05 20:41:21
Long line.
Søren Gjesse
2014/01/06 10:17:32
Done.
| |
71 } | |
72 _environmentCache = new _UnmodifiableMap(result); | |
73 } else { | |
74 _environmentCache = env; | |
75 } | |
76 } | |
77 | |
78 if (_environmentCache is OSError) { | |
79 throw _environmentCache; | |
51 } else { | 80 } else { |
52 var isWindows = operatingSystem == 'windows'; | 81 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 } | 82 } |
71 } | 83 } |
72 | 84 |
73 static String get version => _version(); | 85 static String get version => _version(); |
74 } | 86 } |
75 | 87 |
76 // Environment variables are case-insensitive on Windows. In order | 88 // Environment variables are case-insensitive on Windows. In order |
77 // to reflect that we use a case-insensitive string map on Windows. | 89 // to reflect that we use a case-insensitive string map on Windows. |
78 class _CaseInsensitiveStringMap<V> implements Map<String, V> { | 90 class _CaseInsensitiveStringMap<V> implements Map<String, V> { |
79 _CaseInsensitiveStringMap() : _map = new Map<String, V>(); | 91 _CaseInsensitiveStringMap() : _map = new Map<String, V>(); |
(...skipping 21 matching lines...) Expand all Loading... | |
101 void clear() => _map.clear(); | 113 void clear() => _map.clear(); |
102 void forEach(void f(String key, V value)) => _map.forEach(f); | 114 void forEach(void f(String key, V value)) => _map.forEach(f); |
103 Iterable<String> get keys => _map.keys; | 115 Iterable<String> get keys => _map.keys; |
104 Iterable<V> get values => _map.values; | 116 Iterable<V> get values => _map.values; |
105 int get length => _map.length; | 117 int get length => _map.length; |
106 bool get isEmpty => _map.isEmpty; | 118 bool get isEmpty => _map.isEmpty; |
107 bool get isNotEmpty => _map.isNotEmpty; | 119 bool get isNotEmpty => _map.isNotEmpty; |
108 | 120 |
109 Map<String, V> _map; | 121 Map<String, V> _map; |
110 } | 122 } |
OLD | NEW |