Skip to content

Commit

Permalink
Feat #575 - Add fvm flavor command (#736)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
digoreis authored Jun 9, 2024
1 parent f6b7c38 commit 2083419
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 0 deletions.
32 changes: 32 additions & 0 deletions docs/pages/documentation/guides/basic-commands.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
8 changes: 8 additions & 0 deletions docs/pages/documentation/guides/project-flavors.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}
```


66 changes: 66 additions & 0 deletions lib/src/commands/flavor_command.dart
Original file line number Diff line number Diff line change
@@ -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<int> 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,
);
}
}
}
2 changes: 2 additions & 0 deletions lib/src/runner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -60,6 +61,7 @@ class FvmCommandRunner extends CompletionCommandRunner<int> {
addCommand(DestroyCommand());
addCommand(APICommand());
addCommand(GlobalCommand());
addCommand(FlavorCommand());
}

/// Checks if the current version (set by the build runner on the
Expand Down

0 comments on commit 2083419

Please sign in to comment.