Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2017, 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 import 'configuration.dart'; | |
| 6 | |
| 7 typedef String _LookUpFunction(Configuration configuration); | |
| 8 typedef bool _BoolLookUpFunction(Configuration configuration); | |
| 9 | |
| 10 // TODO(rnystrom): Instead of synthesized negated variables like "unchecked", | |
|
Bill Hesse
2017/05/29 13:08:28
File issue for TODO, use issue number
Bob Nystrom
2017/05/30 23:29:31
Done.
| |
| 11 // consider adding support for "!" to status expressions. | |
| 12 final _variables = { | |
| 13 "analyzer": new _Variable.bool((c) => c.compiler == Compiler.dart2analyzer), | |
| 14 "arch": new _Variable((c) => c.architecture.name, Architecture.names), | |
| 15 "browser": new _Variable.bool((c) => c.runtime.isBrowser), | |
| 16 "builder_tag": new _Variable((c) => c.builderTag ?? "", const []), | |
| 17 "checked": new _Variable.bool((c) => c.isChecked), | |
| 18 "compiler": new _Variable((c) => c.compiler.name, Compiler.names), | |
| 19 "csp": new _Variable.bool((c) => c.isCsp), | |
| 20 "dart2js_with_kernel": new _Variable.bool((c) => c.useDart2JSWithKernel), | |
| 21 "fast_startup": new _Variable.bool((c) => c.useFastStartup), | |
| 22 "host_checked": new _Variable.bool((c) => c.isHostChecked), | |
| 23 "host_unchecked": new _Variable.bool((c) => !c.isHostChecked), | |
| 24 "hot_reload": new _Variable.bool((c) => c.hotReload), | |
| 25 "hot_reload_rollback": new _Variable.bool((c) => c.hotReloadRollback), | |
| 26 "ie": new _Variable.bool((c) => c.runtime.isIE), | |
| 27 "jscl": new _Variable.bool((c) => c.runtime.isJSCommandLine), | |
| 28 "minified": new _Variable.bool((c) => c.isMinified), | |
| 29 "mode": new _Variable((c) => c.mode.name, Mode.names), | |
| 30 "runtime": new _Variable((c) => c.runtime.name, Runtime.names), | |
| 31 "strong": new _Variable.bool((c) => c.isStrong), | |
| 32 "system": new _Variable((c) => c.system.name, System.names), | |
| 33 "unchecked": new _Variable.bool((c) => !c.isChecked), | |
| 34 "unminified": new _Variable.bool((c) => !c.isMinified), | |
| 35 "use_sdk": new _Variable.bool((c) => c.useSdk) | |
| 36 }; | |
| 37 | |
| 38 /// Defines the variables that are available for use inside a status file | |
| 39 /// section header. | |
| 40 /// | |
| 41 /// These mostly map to command line arguments with the same name, though this | |
| 42 /// is only a subset of the full set of command line arguments. | |
| 43 class Environment { | |
| 44 /// Validates that the variable with [name] exists and can be compared | |
| 45 /// against [value]. | |
| 46 /// | |
| 47 /// If any errors are found, adds them to [errors]. | |
| 48 static void validate(String name, String value, List<String> errors) { | |
| 49 var variable = _variables[name]; | |
| 50 if (variable == null) { | |
| 51 errors.add('Unknown variable "$name".'); | |
| 52 return; | |
| 53 } | |
| 54 | |
| 55 // The "builder_tag" variable doesn't have an enumerated set of values. | |
| 56 if (variable.allowedValues.isEmpty) return; | |
| 57 | |
| 58 if (!variable.allowedValues.contains(value)) { | |
| 59 errors.add( | |
| 60 'Variable "$name" cannot have value "$value". Allowed values are:\n' + | |
| 61 variable.allowedValues.join(', ') + | |
| 62 '.'); | |
| 63 } | |
| 64 } | |
| 65 | |
| 66 /// The configuration where variable data is found. | |
| 67 final Configuration _configuration; | |
| 68 | |
| 69 Environment(this._configuration); | |
| 70 | |
| 71 /// Looks up the value of the variable with [name]. | |
| 72 String lookUp(String name) { | |
| 73 var variable = _variables[name]; | |
| 74 if (variable == null) { | |
| 75 // This shouldn't happen since we validate variables before evaluating | |
| 76 // expressions. | |
| 77 throw new ArgumentError('Unknown variable "$variable".'); | |
| 78 } | |
| 79 | |
| 80 return variable.lookUp(_configuration); | |
| 81 } | |
| 82 } | |
| 83 | |
| 84 // TODO(rnystrom): There's some overlap between these and _Option in | |
| 85 // options.dart. Unify? | |
| 86 /// Describes a variable name whose value can be tested in a status file. | |
| 87 /// | |
| 88 /// Each variable is an enumerated string type that only accepts a limited range | |
| 89 /// of values. Each instance of this class defines one variable, the values it | |
| 90 /// permits, and the logic needed to look up the variable's value from a | |
| 91 /// [Configuration] | |
| 92 class _Variable { | |
| 93 final _LookUpFunction _lookUp; | |
| 94 final List<String> allowedValues; | |
| 95 | |
| 96 _Variable(this._lookUp, Iterable<String> allowed) | |
| 97 : allowedValues = allowed.toList(); | |
| 98 | |
| 99 /// Creates a Boolean variable with allowed values "true" and "false". | |
| 100 _Variable.bool(_BoolLookUpFunction lookUp) | |
| 101 : _lookUp = ((configuration) => lookUp(configuration).toString()), | |
| 102 allowedValues = const ["true", "false"]; | |
| 103 | |
| 104 String lookUp(Configuration configuration) => _lookUp(configuration); | |
| 105 } | |
| OLD | NEW |