From aa498cd51adf23c904e8b288f178168088a5d091 Mon Sep 17 00:00:00 2001 From: Andrew Kolos Date: Thu, 28 Sep 2023 10:38:54 -0700 Subject: [PATCH] Add API to read flavor from framework at run time (#134179) Resolves #128046. Adds a services API that allows flutter app developers to write app code that determines `--flavor` the app was built with. This is implemented by having the tool adding the value of `--flavor` to its list of dart environment declarations, which will be available to the app at run time. Specifically,`FLUTTER_APP_FLAVOR` is set. I chose this implementation for its simplicity. There is some precedent for this, but only for web ([example](https://github.com/flutter/flutter/blob/cd2f3f5e78409027d3c9014172708e4dec7f2185/packages/flutter_tools/lib/src/runner/flutter_command.dart#L1231)). --- .../integration_test/integration_test.dart | 2 + packages/flutter/lib/services.dart | 1 + packages/flutter/lib/src/services/flavor.dart | 10 +++ .../lib/src/runner/flutter_command.dart | 16 +++- .../runner/flutter_command_test.dart | 83 +++++++++++++++++++ 5 files changed, 109 insertions(+), 3 deletions(-) create mode 100644 packages/flutter/lib/src/services/flavor.dart diff --git a/dev/integration_tests/flavors/integration_test/integration_test.dart b/dev/integration_tests/flavors/integration_test/integration_test.dart index d2cdd10dd4..4fda3ce79b 100644 --- a/dev/integration_tests/flavors/integration_test/integration_test.dart +++ b/dev/integration_tests/flavors/integration_test/integration_test.dart @@ -3,6 +3,7 @@ // found in the LICENSE file. import 'package:flavors/main.dart' as app; +import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:integration_test/integration_test.dart'; @@ -16,6 +17,7 @@ void main() { await tester.pumpAndSettle(); expect(find.text('paid'), findsOneWidget); + expect(appFlavor, 'paid'); }); }); } diff --git a/packages/flutter/lib/services.dart b/packages/flutter/lib/services.dart index 9cfde5e909..cdd209fd1f 100644 --- a/packages/flutter/lib/services.dart +++ b/packages/flutter/lib/services.dart @@ -19,6 +19,7 @@ export 'src/services/browser_context_menu.dart'; export 'src/services/clipboard.dart'; export 'src/services/debug.dart'; export 'src/services/deferred_component.dart'; +export 'src/services/flavor.dart'; export 'src/services/font_loader.dart'; export 'src/services/haptic_feedback.dart'; export 'src/services/hardware_keyboard.dart'; diff --git a/packages/flutter/lib/src/services/flavor.dart b/packages/flutter/lib/src/services/flavor.dart new file mode 100644 index 0000000000..292c3b37c4 --- /dev/null +++ b/packages/flutter/lib/src/services/flavor.dart @@ -0,0 +1,10 @@ +// Copyright 2014 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +/// The flavor this app was built with. +/// +/// This is equivalent to the value argued to the `--flavor` option at build time. +/// This will be `null` if the `--flavor` option was not provided. +const String? appFlavor = String.fromEnvironment('FLUTTER_APP_FLAVOR') != '' ? + String.fromEnvironment('FLUTTER_APP_FLAVOR') : null; diff --git a/packages/flutter_tools/lib/src/runner/flutter_command.dart b/packages/flutter_tools/lib/src/runner/flutter_command.dart index 36cf0c575f..1d99c9a29e 100644 --- a/packages/flutter_tools/lib/src/runner/flutter_command.dart +++ b/packages/flutter_tools/lib/src/runner/flutter_command.dart @@ -1247,10 +1247,20 @@ abstract class FlutterCommand extends Command { } } + final String? flavor = argParser.options.containsKey('flavor') ? stringArg('flavor') : null; + if (flavor != null) { + if (globals.platform.environment['FLUTTER_APP_FLAVOR'] != null) { + throwToolExit('FLUTTER_APP_FLAVOR is used by the framework and cannot be set in the environment.'); + } + if (dartDefines.any((String define) => define.startsWith('FLUTTER_APP_FLAVOR'))) { + throwToolExit('FLUTTER_APP_FLAVOR is used by the framework and cannot be ' + 'set using --${FlutterOptions.kDartDefinesOption} or --${FlutterOptions.kDartDefineFromFileOption}'); + } + dartDefines.add('FLUTTER_APP_FLAVOR=$flavor'); + } + return BuildInfo(buildMode, - argParser.options.containsKey('flavor') - ? stringArg('flavor') - : null, + flavor, trackWidgetCreation: trackWidgetCreation, frontendServerStarterPath: argParser.options .containsKey(FlutterOptions.kFrontendServerStarterPath) diff --git a/packages/flutter_tools/test/general.shard/runner/flutter_command_test.dart b/packages/flutter_tools/test/general.shard/runner/flutter_command_test.dart index 810c8ef337..9f6d9c5e2c 100644 --- a/packages/flutter_tools/test/general.shard/runner/flutter_command_test.dart +++ b/packages/flutter_tools/test/general.shard/runner/flutter_command_test.dart @@ -11,11 +11,14 @@ import 'package:flutter_tools/src/base/common.dart'; import 'package:flutter_tools/src/base/error_handling_io.dart'; import 'package:flutter_tools/src/base/file_system.dart'; import 'package:flutter_tools/src/base/io.dart'; +import 'package:flutter_tools/src/base/logger.dart'; +import 'package:flutter_tools/src/base/platform.dart'; import 'package:flutter_tools/src/base/signals.dart'; import 'package:flutter_tools/src/base/time.dart'; import 'package:flutter_tools/src/base/user_messages.dart'; import 'package:flutter_tools/src/build_info.dart'; import 'package:flutter_tools/src/cache.dart'; +import 'package:flutter_tools/src/commands/run.dart'; import 'package:flutter_tools/src/dart/pub.dart'; import 'package:flutter_tools/src/device.dart'; import 'package:flutter_tools/src/globals.dart' as globals; @@ -700,6 +703,67 @@ void main() { expect(testLogger.statusText, contains(UserMessages().flutterSpecifyDevice)); }); }); + + group('--flavor', () { + late _TestDeviceManager testDeviceManager; + late Logger logger; + late FileSystem fileSystem; + + setUp(() { + logger = BufferLogger.test(); + testDeviceManager = _TestDeviceManager(logger: logger); + fileSystem = MemoryFileSystem.test(); + }); + + testUsingContext("tool exits when FLUTTER_APP_FLAVOR is already set in user's environment", () async { + fileSystem.file('lib/main.dart').createSync(recursive: true); + fileSystem.file('pubspec.yaml').createSync(); + fileSystem.file('.packages').createSync(); + + final FakeDevice device = FakeDevice('name', 'id'); + testDeviceManager.devices = [device]; + final _TestRunCommandThatOnlyValidates command = _TestRunCommandThatOnlyValidates(); + final CommandRunner runner = createTestCommandRunner(command); + + expect(runner.run(['run', '--no-pub', '--no-hot', '--flavor=strawberry']), + throwsToolExit(message: 'FLUTTER_APP_FLAVOR is used by the framework and cannot be set in the environment.')); + + }, overrides: { + DeviceManager: () => testDeviceManager, + Platform: () => FakePlatform( + environment: { + 'FLUTTER_APP_FLAVOR': 'I was already set' + } + ), + Cache: () => Cache.test(processManager: FakeProcessManager.any()), + FileSystem: () => fileSystem, + ProcessManager: () => FakeProcessManager.any(), + }); + + testUsingContext('tool exits when FLUTTER_APP_FLAVOR is set in --dart-define or --dart-define-from-file', () async { + fileSystem.file('lib/main.dart').createSync(recursive: true); + fileSystem.file('pubspec.yaml').createSync(); + fileSystem.file('.packages').createSync(); + fileSystem.file('config.json')..createSync()..writeAsStringSync('{"FLUTTER_APP_FLAVOR": "strawberry"}'); + + final FakeDevice device = FakeDevice('name', 'id'); + testDeviceManager.devices = [device]; + final _TestRunCommandThatOnlyValidates command = _TestRunCommandThatOnlyValidates(); + final CommandRunner runner = createTestCommandRunner(command); + + expect(runner.run(['run', '--dart-define=FLUTTER_APP_FLAVOR=strawberry', '--no-pub', '--no-hot', '--flavor=strawberry']), + throwsToolExit(message: 'FLUTTER_APP_FLAVOR is used by the framework and cannot be set using --dart-define or --dart-define-from-file')); + + expect(runner.run(['run', '--dart-define-from-file=config.json', '--no-pub', '--no-hot', '--flavor=strawberry']), + throwsToolExit(message: 'FLUTTER_APP_FLAVOR is used by the framework and cannot be set using --dart-define or --dart-define-from-file')); + }, overrides: { + DeviceManager: () => testDeviceManager, + Platform: () => FakePlatform(), + Cache: () => Cache.test(processManager: FakeProcessManager.any()), + FileSystem: () => fileSystem, + ProcessManager: () => FakeProcessManager.any(), + }); + }); }); } @@ -853,3 +917,22 @@ class FakePub extends Fake implements Pub { PubOutputMode outputMode = PubOutputMode.all, }) async { } } + +class _TestDeviceManager extends DeviceManager { + _TestDeviceManager({required super.logger}); + List devices = []; + + @override + List get deviceDiscoverers { + final FakePollingDeviceDiscovery discoverer = FakePollingDeviceDiscovery(); + devices.forEach(discoverer.addDevice); + return [discoverer]; + } +} + +class _TestRunCommandThatOnlyValidates extends RunCommand { + @override + Future runCommand() async { + return FlutterCommandResult.success(); + } +}