From 2083419caa6435f33125588f10a315b87c27f5fc Mon Sep 17 00:00:00 2001 From: Rodrigo Reis Date: Sun, 9 Jun 2024 17:34:12 +0100 Subject: [PATCH] Feat #575 - Add fvm flavor command (#736) * Feat #575: Spwan on a flavor - This commit include the new command fvm flavor to execute this action. * Feat #575 : Update two pages from the docs with the new command - Basic Commands and Flavors --- .../documentation/guides/basic-commands.mdx | 32 +++++++++ .../documentation/guides/project-flavors.md | 8 +++ lib/src/commands/flavor_command.dart | 66 +++++++++++++++++++ lib/src/runner.dart | 2 + 4 files changed, 108 insertions(+) create mode 100644 lib/src/commands/flavor_command.dart diff --git a/docs/pages/documentation/guides/basic-commands.mdx b/docs/pages/documentation/guides/basic-commands.mdx index 603ff496..8df1f81c 100644 --- a/docs/pages/documentation/guides/basic-commands.mdx +++ b/docs/pages/documentation/guides/basic-commands.mdx @@ -329,3 +329,35 @@ To destroy the FVM cache and delete all cached Flutter SDK versions, simply run: ```bash fvm destroy ``` + +## Flavor + +The `flavor` command is used to execute Flutter commands using a specific Flutter SDK version defined by a project flavor. + +This command is particularly useful when you need to run a Flutter command (such as flutter build) with a version of the SDK associated with a particular flavor of your project. + +### Usage + +```bash +> fvm flavor [flavor] [flutter_command] [flutter_command_args] +``` + +`[flavor]`: The flavor of your project which defines the Flutter SDK version you want to use for running the command. + +`[flutter_command]`: The Flutter command you want to execute. + +`[flutter_command_args]`: Any additional arguments you want to pass to the Flutter command. + +### Examples +**Running a Build with a Specific Flavor**: +To build your Flutter project using the Flutter SDK version associated with the `development` flavor: + +```bash +> fvm flavor development flutter build +``` +**Running Tests with a Different Flavor**: +If you need to run tests using the Flutter SDK version associated with the `staging` flavor: + +```bash +> fvm flavor staging flutter test +``` \ No newline at end of file diff --git a/docs/pages/documentation/guides/project-flavors.md b/docs/pages/documentation/guides/project-flavors.md index 9c40937d..dcb37909 100644 --- a/docs/pages/documentation/guides/project-flavors.md +++ b/docs/pages/documentation/guides/project-flavors.md @@ -38,4 +38,12 @@ Will get the version configured for the flavor and set as the project version. fvm use {flavor_name} ``` +## Spwan a command with a flavor version + +Will get the version configured for the flavor and use to run a Flutter command. + +```bash +fvm flavor {flavor_name} {flutter_command} {flutter_command_args} +``` + diff --git a/lib/src/commands/flavor_command.dart b/lib/src/commands/flavor_command.dart new file mode 100644 index 00000000..26deeabf --- /dev/null +++ b/lib/src/commands/flavor_command.dart @@ -0,0 +1,66 @@ +import 'package:args/args.dart'; +import 'package:args/command_runner.dart'; + +import '../services/logger_service.dart'; +import '../services/project_service.dart'; +import '../utils/commands.dart'; +import '../workflows/ensure_cache.workflow.dart'; +import 'base_command.dart'; + +/// Executes a Flutter command using a specified version defined by the project flavor +class FlavorCommand extends BaseCommand { + @override + final name = 'flavor'; + @override + final description = + 'Executes a Flutter command using a specified version defined by the project flavor'; + @override + final argParser = ArgParser.allowAnything(); + @override + String get invocation => 'fvm flavor {flavor}'; + + /// Constructor + FlavorCommand(); + + @override + Future run() async { + if (argResults!.rest.isEmpty) { + throw UsageException( + 'A flavor must be specified to execute the Flutter command', + usage, + ); + } + + final project = ProjectService.fromContext.findAncestor(); + + final flavor = argResults!.rest[0]; + + if (!project.flavors.containsKey(flavor)) { + throw UsageException( + 'The specified flavor is not defined in the project configuration file', + usage, + ); + } + + final version = project.flavors[flavor]; + if (version != null) { + // Removes flavor from first arg + final flutterArgs = [...?argResults?.rest]..removeAt(0); + + // Will install version if not already instaled + final cacheVersion = await ensureCacheWorkflow(version); + // Runs flutter command with pinned version + logger + .info('Using Flutter version "$version" for the "$flavor" flavor...'); + + final results = await runFlutter(flutterArgs, version: cacheVersion); + + return results.exitCode; + } else { + throw UsageException( + 'A version must be specified for the flavor "$flavor" in the project configuration file to execute the Flutter command', + usage, + ); + } + } +} diff --git a/lib/src/runner.dart b/lib/src/runner.dart index 9ab2f3f8..6a4fa0f7 100644 --- a/lib/src/runner.dart +++ b/lib/src/runner.dart @@ -14,6 +14,7 @@ import 'commands/dart_command.dart'; import 'commands/destroy_command.dart'; import 'commands/doctor_command.dart'; import 'commands/exec_command.dart'; +import 'commands/flavor_command.dart'; import 'commands/flutter_command.dart'; import 'commands/global_command.dart'; import 'commands/install_command.dart'; @@ -60,6 +61,7 @@ class FvmCommandRunner extends CompletionCommandRunner { addCommand(DestroyCommand()); addCommand(APICommand()); addCommand(GlobalCommand()); + addCommand(FlavorCommand()); } /// Checks if the current version (set by the build runner on the