2012-04-17 16:20:28 +00:00
|
|
|
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
|
2011-11-07 15:04:42 +00:00
|
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
|
|
|
2012-12-18 21:31:34 +00:00
|
|
|
part of dart.io;
|
|
|
|
|
2012-04-18 11:39:16 +00:00
|
|
|
class _Platform {
|
2012-10-30 10:32:30 +00:00
|
|
|
external static int _numberOfProcessors();
|
|
|
|
external static String _pathSeparator();
|
|
|
|
external static String _operatingSystem();
|
|
|
|
external static _localHostname();
|
2013-08-08 07:19:21 +00:00
|
|
|
external static _executable();
|
2015-06-15 08:13:21 +00:00
|
|
|
external static _resolvedExecutable();
|
2015-01-05 13:08:42 +00:00
|
|
|
/**
|
|
|
|
* Retrieve the entries of the process environment.
|
|
|
|
*
|
|
|
|
* The result is an [Iterable] of strings, where each string represents
|
|
|
|
* an environment entry.
|
|
|
|
*
|
|
|
|
* Environment entries should be strings containing
|
|
|
|
* a non-empty name and a value separated by a '=' character.
|
|
|
|
* The name does not contain a '=' character,
|
|
|
|
* so the name is everything up to the first '=' character.
|
|
|
|
* Values are everything after the first '=' charcacter.
|
|
|
|
* A value may contain further '=' characters, and it may be empty.
|
|
|
|
*
|
|
|
|
* Returns an [OSError] if retrieving the environment fails.
|
|
|
|
*/
|
2012-10-30 10:32:30 +00:00
|
|
|
external static _environment();
|
2013-08-21 15:21:55 +00:00
|
|
|
external static List<String> _executableArguments();
|
|
|
|
external static String _packageRoot();
|
2015-12-17 15:48:41 +00:00
|
|
|
external static String _packageConfig();
|
2013-06-25 12:56:23 +00:00
|
|
|
external static String _version();
|
2011-11-07 15:04:42 +00:00
|
|
|
|
2014-01-08 12:03:11 +00:00
|
|
|
static String executable = _executable();
|
2015-06-15 08:13:21 +00:00
|
|
|
static String resolvedExecutable = _resolvedExecutable();
|
2014-01-08 12:03:11 +00:00
|
|
|
static String packageRoot = _packageRoot();
|
2015-12-17 15:48:41 +00:00
|
|
|
static String packageConfig = _packageConfig();
|
2014-01-08 12:03:11 +00:00
|
|
|
|
2014-01-06 10:18:22 +00:00
|
|
|
// Cache the OS environemnt. This can be an OSError instance if
|
|
|
|
// retrieving the environment failed.
|
2016-05-17 10:55:11 +00:00
|
|
|
static var/*OSError|Map<String,String>*/ _environmentCache;
|
2014-01-06 10:18:22 +00:00
|
|
|
|
2013-06-25 12:56:23 +00:00
|
|
|
static int get numberOfProcessors => _numberOfProcessors();
|
|
|
|
static String get pathSeparator => _pathSeparator();
|
|
|
|
static String get operatingSystem => _operatingSystem();
|
2014-07-28 12:31:29 +00:00
|
|
|
static Uri script;
|
|
|
|
|
2012-08-24 10:11:05 +00:00
|
|
|
static String get localHostname {
|
2012-04-11 12:27:55 +00:00
|
|
|
var result = _localHostname();
|
|
|
|
if (result is OSError) {
|
|
|
|
throw result;
|
|
|
|
} else {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
2012-04-17 16:20:28 +00:00
|
|
|
|
2013-08-21 15:21:55 +00:00
|
|
|
static List<String> get executableArguments => _executableArguments();
|
2013-08-08 07:19:21 +00:00
|
|
|
|
2012-08-24 10:11:05 +00:00
|
|
|
static Map<String, String> get environment {
|
2014-01-06 10:18:22 +00:00
|
|
|
if (_environmentCache == null) {
|
|
|
|
var env = _environment();
|
|
|
|
if (env is !OSError) {
|
|
|
|
var isWindows = operatingSystem == 'windows';
|
2016-05-17 10:55:11 +00:00
|
|
|
var result = isWindows
|
|
|
|
? new _CaseInsensitiveStringMap<String>()
|
|
|
|
: new Map<String, String>();
|
2014-01-06 10:18:22 +00:00
|
|
|
for (var str in env) {
|
2016-08-24 17:10:41 +00:00
|
|
|
if (str == null) {
|
|
|
|
continue;
|
|
|
|
}
|
2015-01-05 13:08:42 +00:00
|
|
|
// The Strings returned by [_environment()] are expected to be
|
|
|
|
// valid environment entries, but exceptions have been seen
|
|
|
|
// (e.g., an entry of just '=' has been seen on OS/X).
|
|
|
|
// Invalid entries (lines without a '=' or with an empty name)
|
|
|
|
// are discarded.
|
2014-01-06 10:18:22 +00:00
|
|
|
var equalsIndex = str.indexOf('=');
|
2015-01-05 12:37:42 +00:00
|
|
|
if (equalsIndex > 0) {
|
|
|
|
result[str.substring(0, equalsIndex)] =
|
|
|
|
str.substring(equalsIndex + 1);
|
2014-01-06 10:18:22 +00:00
|
|
|
}
|
2012-07-02 10:58:24 +00:00
|
|
|
}
|
2014-06-10 18:37:08 +00:00
|
|
|
_environmentCache = new UnmodifiableMapView<String, String>(result);
|
2014-01-06 10:18:22 +00:00
|
|
|
} else {
|
|
|
|
_environmentCache = env;
|
2012-04-17 16:20:28 +00:00
|
|
|
}
|
2014-01-06 10:18:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_environmentCache is OSError) {
|
|
|
|
throw _environmentCache;
|
|
|
|
} else {
|
2016-05-17 10:55:11 +00:00
|
|
|
return _environmentCache as Object/*=Map<String, String>*/;
|
2012-04-17 16:20:28 +00:00
|
|
|
}
|
|
|
|
}
|
2013-06-25 12:56:23 +00:00
|
|
|
|
|
|
|
static String get version => _version();
|
2011-11-07 15:04:42 +00:00
|
|
|
}
|
2013-01-02 11:26:31 +00:00
|
|
|
|
|
|
|
// Environment variables are case-insensitive on Windows. In order
|
|
|
|
// to reflect that we use a case-insensitive string map on Windows.
|
|
|
|
class _CaseInsensitiveStringMap<V> implements Map<String, V> {
|
2014-06-10 18:37:08 +00:00
|
|
|
final Map<String, V> _map = new Map<String, V>();
|
2013-01-02 11:26:31 +00:00
|
|
|
|
2016-05-12 11:17:31 +00:00
|
|
|
bool containsKey(Object key) =>
|
|
|
|
key is String && _map.containsKey(key.toUpperCase());
|
2013-06-24 12:45:10 +00:00
|
|
|
bool containsValue(Object value) => _map.containsValue(value);
|
2016-05-12 11:17:31 +00:00
|
|
|
V operator [](Object key) => key is String ? _map[key.toUpperCase()] : null;
|
2013-01-02 11:26:31 +00:00
|
|
|
void operator []=(String key, V value) {
|
|
|
|
_map[key.toUpperCase()] = value;
|
|
|
|
}
|
|
|
|
V putIfAbsent(String key, V ifAbsent()) {
|
2016-05-12 11:17:31 +00:00
|
|
|
return _map.putIfAbsent(key.toUpperCase(), ifAbsent);
|
2013-01-02 11:26:31 +00:00
|
|
|
}
|
2016-05-17 10:55:11 +00:00
|
|
|
void addAll(Map<String, V> other) {
|
2013-06-13 09:48:59 +00:00
|
|
|
other.forEach((key, value) => this[key.toUpperCase()] = value);
|
|
|
|
}
|
2016-05-12 11:17:31 +00:00
|
|
|
V remove(Object key) => key is String ? _map.remove(key.toUpperCase()) : null;
|
2016-04-21 19:35:17 +00:00
|
|
|
void clear() { _map.clear(); }
|
|
|
|
void forEach(void f(String key, V value)) { _map.forEach(f); }
|
2013-01-07 13:05:27 +00:00
|
|
|
Iterable<String> get keys => _map.keys;
|
|
|
|
Iterable<V> get values => _map.values;
|
2013-01-02 11:26:31 +00:00
|
|
|
int get length => _map.length;
|
|
|
|
bool get isEmpty => _map.isEmpty;
|
2013-05-28 17:30:37 +00:00
|
|
|
bool get isNotEmpty => _map.isNotEmpty;
|
2014-06-10 18:37:08 +00:00
|
|
|
String toString() => _map.toString();
|
2013-01-02 11:26:31 +00:00
|
|
|
}
|