|
| 1 | +// Copyright 2014 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'base/common.dart'; |
| 6 | +import 'base/config.dart'; |
| 7 | +import 'base/platform.dart'; |
| 8 | +import 'features.dart'; |
| 9 | +import 'flutter_manifest.dart'; |
| 10 | + |
| 11 | +/// Reads configuration flags to possibly override the default flag value. |
| 12 | +/// |
| 13 | +/// See [isEnabled] for details on how feature flag values are resolved. |
| 14 | +interface class FlutterFeaturesConfig { |
| 15 | + /// Creates a feature configuration reader from the provided sources. |
| 16 | + /// |
| 17 | + /// [globalConfig] reads values stored by the `flutter config` tool, which |
| 18 | + /// are normally in the user's `%HOME` directory (varies by system), while |
| 19 | + /// [projectManifest] reads values from the _current_ Flutter project's |
| 20 | + /// `pubspec.yaml` |
| 21 | + const FlutterFeaturesConfig({ |
| 22 | + required Config globalConfig, |
| 23 | + required Platform platform, |
| 24 | + required FlutterManifest? projectManifest, |
| 25 | + }) : _globalConfig = globalConfig, |
| 26 | + _platform = platform, |
| 27 | + _projectManifest = projectManifest; |
| 28 | + |
| 29 | + final Config _globalConfig; |
| 30 | + final Platform _platform; |
| 31 | + |
| 32 | + // Can be null if no manifest file exists in the current directory. |
| 33 | + final FlutterManifest? _projectManifest; |
| 34 | + |
| 35 | + /// Returns whether [feature] has been turned on/off from configuration. |
| 36 | + /// |
| 37 | + /// If the feature was not configured, or cannot be configured, returns `null`. |
| 38 | + /// |
| 39 | + /// The value is resolved, if possible, in the following order, where if a |
| 40 | + /// step resolves to a boolean value, no further steps are attempted: |
| 41 | + /// |
| 42 | + /// |
| 43 | + /// ## 1. Local Project Configuration |
| 44 | + /// |
| 45 | + /// If [Feature.configSetting] is `null`, this step is skipped. |
| 46 | + /// |
| 47 | + /// If the value defined by the key `$configSetting` is set in `pubspec.yaml`, |
| 48 | + /// it is returned as a boolean value. |
| 49 | + /// |
| 50 | + /// Assuming there is a setting where `configSetting: 'enable-foo'`: |
| 51 | + /// |
| 52 | + /// ```yaml |
| 53 | + /// # true |
| 54 | + /// flutter: |
| 55 | + /// config: |
| 56 | + /// enable-foo: true |
| 57 | + /// |
| 58 | + /// # false |
| 59 | + /// flutter: |
| 60 | + /// config: |
| 61 | + /// enable-foo: false |
| 62 | + /// ``` |
| 63 | + /// |
| 64 | + /// ## 2. Global Tool Configuration |
| 65 | + /// |
| 66 | + /// If [Feature.configSetting] is `null`, this step is skipped. |
| 67 | + /// |
| 68 | + /// If the value defined by the key `$configSetting` is set in the global |
| 69 | + /// (platform dependent) configuration file, it is returned as a boolean |
| 70 | + /// value. |
| 71 | + /// |
| 72 | + /// Assuming there is a setting where `configSetting: 'enable-foo'`: |
| 73 | + /// |
| 74 | + /// ```sh |
| 75 | + /// # future runs will treat the value as true |
| 76 | + /// flutter config --enable-foo |
| 77 | + /// |
| 78 | + /// # future runs will treat the value as false |
| 79 | + /// flutter config --no-enable-foo |
| 80 | + /// ``` |
| 81 | + /// |
| 82 | + /// ## 3. Environment Variable |
| 83 | + /// |
| 84 | + /// If [Feature.environmentOverride] is `null`, this step is skipped. |
| 85 | + /// |
| 86 | + /// If the value defined by the key `$environmentOverride` is equal to the |
| 87 | + /// string `'true'` (case insensitive), returns `true`, or `false` otherwise. |
| 88 | + /// |
| 89 | + /// Assuming there is a flag where `environmentOverride: 'ENABLE_FOO'`: |
| 90 | + /// |
| 91 | + /// ```sh |
| 92 | + /// # true |
| 93 | + /// ENABLE_FOO=true flutter some-command |
| 94 | + /// |
| 95 | + /// # true |
| 96 | + /// ENABLE_FOO=TRUE flutter some-command |
| 97 | + /// |
| 98 | + /// # false |
| 99 | + /// ENABLE_FOO=false flutter some-command |
| 100 | + /// |
| 101 | + /// # false |
| 102 | + /// ENABLE_FOO=any-other-value flutter some-command |
| 103 | + /// ``` |
| 104 | + bool? isEnabled(Feature feature) { |
| 105 | + return _isEnabledByConfigValue(feature) ?? _isEnabledByPlatformEnvironment(feature); |
| 106 | + } |
| 107 | + |
| 108 | + bool? _isEnabledByConfigValue(Feature feature) { |
| 109 | + // If the feature cannot be configured by local/global config settings, return null. |
| 110 | + final String? featureName = feature.configSetting; |
| 111 | + if (featureName == null) { |
| 112 | + return null; |
| 113 | + } |
| 114 | + return _isEnabledAtProjectLevel(featureName) ?? _isEnabledByGlobalConfig(featureName); |
| 115 | + } |
| 116 | + |
| 117 | + bool? _isEnabledByPlatformEnvironment(Feature feature) { |
| 118 | + // If the feature cannot be configured by an environment variable, return null. |
| 119 | + final String? environmentName = feature.environmentOverride; |
| 120 | + if (environmentName == null) { |
| 121 | + return null; |
| 122 | + } |
| 123 | + final Object? environmentValue = _platform.environment[environmentName]?.toLowerCase(); |
| 124 | + if (environmentValue == null) { |
| 125 | + return null; |
| 126 | + } |
| 127 | + return environmentValue == 'true'; |
| 128 | + } |
| 129 | + |
| 130 | + bool? _isEnabledAtProjectLevel(String featureName) { |
| 131 | + final Object? configSection = _projectManifest?.flutterDescriptor['config']; |
| 132 | + if (configSection == null) { |
| 133 | + return null; |
| 134 | + } |
| 135 | + if (configSection is! Map) { |
| 136 | + throwToolExit( |
| 137 | + 'The "config" property of "flutter" in pubspec.yaml must be a map, but ' |
| 138 | + 'got $configSection (${configSection.runtimeType})', |
| 139 | + ); |
| 140 | + } |
| 141 | + return _requireBoolOrNull( |
| 142 | + configSection[featureName], |
| 143 | + featureName: featureName, |
| 144 | + source: '"flutter: config:" in pubspec.yaml', |
| 145 | + ); |
| 146 | + } |
| 147 | + |
| 148 | + bool? _isEnabledByGlobalConfig(String featureName) { |
| 149 | + return _requireBoolOrNull( |
| 150 | + _globalConfig.getValue(featureName), |
| 151 | + featureName: featureName, |
| 152 | + source: '"${_globalConfig.configPath}"', |
| 153 | + ); |
| 154 | + } |
| 155 | + |
| 156 | + static bool? _requireBoolOrNull( |
| 157 | + Object? value, { |
| 158 | + required String featureName, |
| 159 | + required String source, |
| 160 | + }) { |
| 161 | + if (value is bool?) { |
| 162 | + return value; |
| 163 | + } |
| 164 | + throwToolExit( |
| 165 | + 'The "$featureName" property in $source must be a boolean, but got $value (${value.runtimeType})', |
| 166 | + ); |
| 167 | + } |
| 168 | +} |
0 commit comments