From bd030dc6d4547cd9b2c6c8b95219d18ae8be510f Mon Sep 17 00:00:00 2001 From: Felix Angelov Date: Fri, 5 May 2023 14:14:15 -0500 Subject: [PATCH] feat(shorebird_cli): `shorebird release` supports flavors (#442) --- .../lib/src/commands/release_command.dart | 24 +++++++++++++--- .../lib/src/config/shorebird_yaml.dart | 7 +++++ .../src/commands/release_command_test.dart | 28 +++++++++++++++++-- 3 files changed, 53 insertions(+), 6 deletions(-) diff --git a/packages/shorebird_cli/lib/src/commands/release_command.dart b/packages/shorebird_cli/lib/src/commands/release_command.dart index 5b0d4d44..5712f81d 100644 --- a/packages/shorebird_cli/lib/src/commands/release_command.dart +++ b/packages/shorebird_cli/lib/src/commands/release_command.dart @@ -6,6 +6,7 @@ import 'package:mason_logger/mason_logger.dart'; import 'package:path/path.dart' as p; import 'package:shorebird_cli/src/auth_logger_mixin.dart'; import 'package:shorebird_cli/src/command.dart'; +import 'package:shorebird_cli/src/config/shorebird_yaml.dart'; import 'package:shorebird_cli/src/shorebird_build_mixin.dart'; import 'package:shorebird_cli/src/shorebird_config_mixin.dart'; import 'package:shorebird_cli/src/shorebird_create_app_mixin.dart'; @@ -43,6 +44,15 @@ class ReleaseCommand extends ShorebirdCommand allowed: ['android'], allowedHelp: {'android': 'The Android platform.'}, defaultsTo: 'android', + ) + ..addOption( + 'target', + abbr: 't', + help: 'The main entrypoint file of the application.', + ) + ..addOption( + 'flavor', + help: 'The product flavor to use when building the app.', ); } @@ -74,9 +84,11 @@ make smaller updates to your app. await logValidationIssues(); + final flavor = results['flavor'] as String?; + final target = results['target'] as String?; final buildProgress = logger.progress('Building release'); try { - await buildAppBundle(); + await buildAppBundle(flavor: flavor, target: target); buildProgress.complete(); } on ProcessException catch (error) { buildProgress.fail('Failed to build: ${error.message}'); @@ -104,7 +116,7 @@ make smaller updates to your app. return ExitCode.software.code; } - final appId = shorebirdYaml.appId; + final appId = shorebirdYaml.getAppId(flavor: flavor); final app = apps.firstWhereOrNull((a) => a.id == appId); if (app == null) { logger.err( @@ -196,7 +208,7 @@ ${styleBold.wrap(lightGreen.wrap('šŸš€ Ready to create a new release!'))} 'app', 'intermediates', 'stripped_native_libs', - 'release', + flavor != null ? '${flavor}Release' : 'release', 'out', 'lib', archMetadata.path, @@ -221,12 +233,16 @@ ${styleBold.wrap(lightGreen.wrap('šŸš€ Ready to create a new release!'))} createArtifactProgress.complete(); + final bundlePath = flavor != null + ? './build/app/outputs/bundle/${flavor}Release/app-$flavor-release.aab' + : './build/app/outputs/bundle/release/app-release.aab'; + logger ..success('\nāœ… Published Release!') ..info(''' Your next step is to upload the app bundle to the Play Store. -${lightCyan.wrap("./build/app/outputs/bundle/release/app-release.aab")} +${lightCyan.wrap(bundlePath)} See the following link for more information: ${link(uri: Uri.parse('https://support.google.com/googleplay/android-developer/answer/9859152?hl=en'))} diff --git a/packages/shorebird_cli/lib/src/config/shorebird_yaml.dart b/packages/shorebird_cli/lib/src/config/shorebird_yaml.dart index 3ae09c59..2e8244da 100644 --- a/packages/shorebird_cli/lib/src/config/shorebird_yaml.dart +++ b/packages/shorebird_cli/lib/src/config/shorebird_yaml.dart @@ -39,3 +39,10 @@ class ShorebirdYaml { /// The base url used to check for updates. final String? baseUrl; } + +extension AppIdExtension on ShorebirdYaml { + String getAppId({String? flavor}) { + if (flavor == null || flavors == null) return appId; + return flavors![flavor] ?? appId; + } +} diff --git a/packages/shorebird_cli/test/src/commands/release_command_test.dart b/packages/shorebird_cli/test/src/commands/release_command_test.dart index 9fd1ce9c..142006a8 100644 --- a/packages/shorebird_cli/test/src/commands/release_command_test.dart +++ b/packages/shorebird_cli/test/src/commands/release_command_test.dart @@ -89,7 +89,7 @@ flutter: return tempDir; } - void setUpTempArtifacts(Directory dir) { + void setUpTempArtifacts(Directory dir, {String? flavor}) { for (final archMetadata in ShorebirdBuildMixin.allAndroidArchitectures.values) { final artifactPath = p.join( @@ -98,7 +98,7 @@ flutter: 'app', 'intermediates', 'stripped_native_libs', - 'release', + flavor != null ? '${flavor}Release' : 'release', 'out', 'lib', archMetadata.path, @@ -426,6 +426,30 @@ Did you forget to run "shorebird init"?''', expect(capturedHostedUri, isNull); }); + test( + 'succeeds when release is successful ' + 'with flavors and target', () async { + const flavor = 'development'; + const target = './lib/main_development.dart'; + when(() => argResults['flavor']).thenReturn(flavor); + when(() => argResults['target']).thenReturn(target); + final tempDir = setUpTempDir(); + File( + p.join(tempDir.path, 'shorebird.yaml'), + ).writeAsStringSync(''' +app_id: productionAppId +flavors: + development: $appId'''); + setUpTempArtifacts(tempDir, flavor: flavor); + final exitCode = await IOOverrides.runZoned( + command.run, + getCurrentDirectory: () => tempDir, + ); + verify(() => logger.success('\nāœ… Published Release!')).called(1); + expect(exitCode, ExitCode.success.code); + expect(capturedHostedUri, isNull); + }); + test('prints flutter validation warnings', () async { when(() => flutterValidator.validate(any())).thenAnswer( (_) async => [