features.dart 7.97 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:meta/meta.dart';

import 'base/context.dart';
8
import 'globals.dart' as globals;
9 10 11 12 13 14 15 16 17

/// The current [FeatureFlags] implementation.
///
/// If not injected, a default implementation is provided.
FeatureFlags get featureFlags => context.get<FeatureFlags>();

/// The interface used to determine if a particular [Feature] is enabled.
///
/// The rest of the tools code should use this class instead of looking up
18
/// features directly. To facilitate rolls to google3 and other clients, all
19 20 21 22 23 24 25
/// flags should be provided with a default implementation here. Clients that
/// use this class should extent instead of implement, so that new flags are
/// picked up automatically.
class FeatureFlags {
  const FeatureFlags();

  /// Whether flutter desktop for linux is enabled.
26
  bool get isLinuxEnabled => isEnabled(flutterLinuxDesktopFeature);
27 28

  /// Whether flutter desktop for macOS is enabled.
29
  bool get isMacOSEnabled => isEnabled(flutterMacOSDesktopFeature);
30 31

  /// Whether flutter web is enabled.
32
  bool get isWebEnabled => isEnabled(flutterWebFeature);
33 34

  /// Whether flutter desktop for Windows is enabled.
35
  bool get isWindowsEnabled => isEnabled(flutterWindowsDesktopFeature);
36

37 38
  /// Whether the Android embedding V2 is enabled.
  bool get isAndroidEmbeddingV2Enabled => isEnabled(flutterAndroidEmbeddingV2Feature);
39

40 41 42 43
  /// Whether a particular feature is enabled for the current channel.
  ///
  /// Prefer using one of the specific getters above instead of this API.
  bool isEnabled(Feature feature) {
44
    final String currentChannel = globals.flutterVersion.channel;
45 46 47 48 49 50
    final FeatureChannelSetting featureSetting = feature.getSettingForChannel(currentChannel);
    if (!featureSetting.available) {
      return false;
    }
    bool isEnabled = featureSetting.enabledByDefault;
    if (feature.configSetting != null) {
51
      final bool configOverride = globals.config.getValue(feature.configSetting) as bool;
52 53 54 55 56
      if (configOverride != null) {
        isEnabled = configOverride;
      }
    }
    if (feature.environmentOverride != null) {
57
      if (globals.platform.environment[feature.environmentOverride]?.toLowerCase() == 'true') {
58 59 60 61 62 63 64 65 66 67 68 69 70
        isEnabled = true;
      }
    }
    return isEnabled;
  }
}

/// All current Flutter feature flags.
const List<Feature> allFeatures = <Feature>[
  flutterWebFeature,
  flutterLinuxDesktopFeature,
  flutterMacOSDesktopFeature,
  flutterWindowsDesktopFeature,
71
  flutterAndroidEmbeddingV2Feature,
72 73 74 75
];

/// The [Feature] for flutter web.
const Feature flutterWebFeature = Feature(
76
  name: 'Flutter for web',
77 78 79 80 81 82 83 84 85 86
  configSetting: 'enable-web',
  environmentOverride: 'FLUTTER_WEB',
  master: FeatureChannelSetting(
    available: true,
    enabledByDefault: false,
  ),
  dev: FeatureChannelSetting(
    available: true,
    enabledByDefault: false,
  ),
87 88 89 90
  beta: FeatureChannelSetting(
    available: true,
    enabledByDefault: false,
  ),
91 92 93 94
);

/// The [Feature] for macOS desktop.
const Feature flutterMacOSDesktopFeature = Feature(
95
  name: 'Flutter for desktop on macOS',
96
  configSetting: 'enable-macos-desktop',
97
  environmentOverride: 'FLUTTER_MACOS',
98 99 100 101
  master: FeatureChannelSetting(
    available: true,
    enabledByDefault: false,
  ),
102 103 104 105
  dev: FeatureChannelSetting(
    available: true,
    enabledByDefault: false,
  ),
106 107 108 109
);

/// The [Feature] for Linux desktop.
const Feature flutterLinuxDesktopFeature = Feature(
110
  name: 'Flutter for desktop on Linux',
111
  configSetting: 'enable-linux-desktop',
112
  environmentOverride: 'FLUTTER_LINUX',
113 114 115 116 117 118 119 120
  master: FeatureChannelSetting(
    available: true,
    enabledByDefault: false,
  ),
);

/// The [Feature] for Windows desktop.
const Feature flutterWindowsDesktopFeature = Feature(
121
  name: 'Flutter for desktop on Windows',
122
  configSetting: 'enable-windows-desktop',
123
  environmentOverride: 'FLUTTER_WINDOWS',
124 125 126 127 128 129
  master: FeatureChannelSetting(
    available: true,
    enabledByDefault: false,
  ),
);

130
/// The [Feature] for generating projects using the new Android embedding.
131 132 133 134
const Feature flutterAndroidEmbeddingV2Feature = Feature(
  name: 'flutter create generates projects using the Android embedding V2',
  environmentOverride: 'ENABLE_ANDROID_EMBEDDING_V2',
  configSetting: 'enable-android-embedding-v2',
135 136 137 138 139 140 141 142
  beta: FeatureChannelSetting(
    available: true,
    enabledByDefault: true,
  ),
  dev: FeatureChannelSetting(
    available: true,
    enabledByDefault: true,
  ),
143 144
  master: FeatureChannelSetting(
    available: true,
145
    enabledByDefault: true,
146
  ),
147 148 149 150
  stable: FeatureChannelSetting(
    available: true,
    enabledByDefault: true,
  ),
151 152
);

153 154 155 156 157 158
/// A [Feature] is a process for conditionally enabling tool features.
///
/// All settings are optional, and if not provided will generally default to
/// a "safe" value, such as being off.
///
/// The top level feature settings can be provided to apply to all channels.
159
/// Otherwise, more specific settings take precedence over higher level
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
/// settings.
class Feature {
  /// Creates a [Feature].
  const Feature({
    @required this.name,
    this.environmentOverride,
    this.configSetting,
    this.master = const FeatureChannelSetting(),
    this.dev = const FeatureChannelSetting(),
    this.beta = const FeatureChannelSetting(),
    this.stable = const FeatureChannelSetting(),
  });

  /// The user visible name for this feature.
  final String name;

  /// The settings for the master branch and other unknown channels.
  final FeatureChannelSetting master;

  /// The settings for the dev branch.
  final FeatureChannelSetting dev;

  /// The settings for the beta branch.
  final FeatureChannelSetting beta;

  /// The settings for the stable branch.
  final FeatureChannelSetting stable;

  /// The name of an environment variable that can override the setting.
  ///
  /// The environment variable needs to be set to the value 'true'. This is
  /// only intended for usage by CI and not as an advertised method to enable
  /// a feature.
  ///
  /// If not provided, defaults to `null` meaning there is no override.
  final String environmentOverride;

  /// The name of a setting that can be used to enable this feature.
  ///
  /// If not provided, defaults to `null` meaning there is no config setting.
  final String configSetting;

  /// A help message for the `flutter config` command, or null if unsupported.
  String generateHelpMessage() {
    if (configSetting == null) {
      return null;
    }
207 208
    final StringBuffer buffer = StringBuffer('Enable or disable $name. '
        'This setting will take effect on ');
209 210 211 212 213 214 215 216
    final List<String> channels = <String>[
      if (master.available) 'master',
      if (dev.available) 'dev',
      if (beta.available) 'beta',
      if (stable.available) 'stable',
    ];
    if (channels.length == 1) {
      buffer.write('the ${channels.single} channel.');
217 218
    } else if (channels.length == 2) {
      buffer.write('the ${channels.join(' and ')} channels.');
219
    } else {
220 221 222
      final String prefix = (channels.toList()
        ..removeLast()).join(', ');
      buffer.write('the $prefix, and ${channels.last} channels.');
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
    }
    return buffer.toString();
  }

  /// Retrieve the correct setting for the provided `channel`.
  FeatureChannelSetting getSettingForChannel(String channel) {
    switch (channel) {
      case 'stable':
        return stable;
      case 'beta':
        return beta;
      case 'dev':
        return dev;
      case 'master':
      default:
        return master;
    }
  }
}

/// A description of the conditions to enable a feature for a particular channel.
class FeatureChannelSetting {
  const FeatureChannelSetting({
    this.available = false,
    this.enabledByDefault = false,
  });

  /// Whether the feature is available on this channel.
  ///
252
  /// If not provided, defaults to `false`. This implies that the feature
253 254 255 256 257 258 259 260
  /// cannot be enabled even by the settings below.
  final bool available;

  /// Whether the feature is enabled by default.
  ///
  /// If not provided, defaults to `false`.
  final bool enabledByDefault;
}