From d66ae12e3aa5a9fbb63840e0fcf3a8a6fa946344 Mon Sep 17 00:00:00 2001 From: Felix Angelov Date: Tue, 28 Mar 2023 16:12:11 -0500 Subject: [PATCH] refactor(shorebird_cli): rename `publish` to `patch` (#189) --- .../shorebird_cli/lib/src/command_runner.dart | 2 +- .../lib/src/commands/commands.dart | 2 +- .../{publish_command.dart => patch.dart} | 82 ++++++++++++++----- .../lib/src/commands/release_command.dart | 2 +- ...mand_test.dart => patch_command_test.dart} | 29 ++++--- .../src/commands/release_command_test.dart | 2 +- 6 files changed, 84 insertions(+), 35 deletions(-) rename packages/shorebird_cli/lib/src/commands/{publish_command.dart => patch.dart} (73%) rename packages/shorebird_cli/test/src/commands/{publish_command_test.dart => patch_command_test.dart} (95%) diff --git a/packages/shorebird_cli/lib/src/command_runner.dart b/packages/shorebird_cli/lib/src/command_runner.dart index b4533950..5a1f0c26 100644 --- a/packages/shorebird_cli/lib/src/command_runner.dart +++ b/packages/shorebird_cli/lib/src/command_runner.dart @@ -51,7 +51,7 @@ class ShorebirdCliCommandRunner extends CompletionCommandRunner { addCommand(InitCommand(logger: _logger)); addCommand(LoginCommand(logger: _logger)); addCommand(LogoutCommand(logger: _logger)); - addCommand(PublishCommand(logger: _logger)); + addCommand(PatchCommand(logger: _logger)); addCommand(ReleaseCommand(logger: _logger)); addCommand(RunCommand(logger: _logger)); addCommand(UpgradeCommand(logger: _logger)); diff --git a/packages/shorebird_cli/lib/src/commands/commands.dart b/packages/shorebird_cli/lib/src/commands/commands.dart index 695afdaa..1f8ae99f 100644 --- a/packages/shorebird_cli/lib/src/commands/commands.dart +++ b/packages/shorebird_cli/lib/src/commands/commands.dart @@ -3,7 +3,7 @@ export 'build_command.dart'; export 'init_command.dart'; export 'login_command.dart'; export 'logout_command.dart'; -export 'publish_command.dart'; +export 'patch.dart'; export 'release_command.dart'; export 'run_command.dart'; export 'upgrade_command.dart'; diff --git a/packages/shorebird_cli/lib/src/commands/publish_command.dart b/packages/shorebird_cli/lib/src/commands/patch.dart similarity index 73% rename from packages/shorebird_cli/lib/src/commands/publish_command.dart rename to packages/shorebird_cli/lib/src/commands/patch.dart index 87a654b7..53007a58 100644 --- a/packages/shorebird_cli/lib/src/commands/publish_command.dart +++ b/packages/shorebird_cli/lib/src/commands/patch.dart @@ -11,36 +11,60 @@ import 'package:shorebird_cli/src/shorebird_create_app_mixin.dart'; import 'package:shorebird_cli/src/shorebird_engine_mixin.dart'; import 'package:shorebird_code_push_client/shorebird_code_push_client.dart'; -/// {@template publish_command} -/// -/// `shorebird publish ` -/// Publish new releases to the Shorebird CodePush server. +/// {@template patch_command} +/// `shorebird patch` +/// Publish new patches for a specific release to the Shorebird CodePush server. /// {@endtemplate} -class PublishCommand extends ShorebirdCommand +class PatchCommand extends ShorebirdCommand with ShorebirdConfigMixin, ShorebirdEngineMixin, ShorebirdBuildMixin, ShorebirdCreateAppMixin { - /// {@macro publish_command} - PublishCommand({ + /// {@macro patch_command} + PatchCommand({ required super.logger, super.auth, super.buildCodePushClient, super.runProcess, HashFunction? hashFn, - }) : _hashFn = hashFn ?? ((m) => sha256.convert(m).toString()); + }) : _hashFn = hashFn ?? ((m) => sha256.convert(m).toString()) { + argParser + ..addOption( + 'release-version', + help: 'The version of the release (e.g. "1.0.0").', + ) + ..addOption( + 'platform', + help: 'The platform of the release (e.g. "android").', + allowed: ['android'], + allowedHelp: {'android': 'The Android platform.'}, + defaultsTo: 'android', + ) + ..addOption( + 'arch', + help: 'The architecture of the release (e.g. "aarch64").', + allowed: ['aarch64'], + allowedHelp: {'aarch64': 'The 64-bit ARM architecture.'}, + defaultsTo: 'aarch64', + ) + ..addOption( + 'channel', + help: 'The channel the patch should be promoted to (e.g. "stable").', + allowed: ['stable'], + allowedHelp: { + 'stable': 'The stable channel which is consumed by production apps.' + }, + defaultsTo: 'stable', + ); + } @override - String get description => 'Publish an update.'; + String get description => + 'Publish new patches for a specific release to Shorebird.'; @override - String get name => 'publish'; - - // TODO(felangel): make these configurable. - static const String _arch = 'aarch64'; - static const String _platform = 'android'; - static const String _channel = 'stable'; + String get name => 'patch'; final HashFunction _hashFn; @@ -127,13 +151,29 @@ Did you forget to run "shorebird init"?''', return ExitCode.software.code; } + final releaseVersionArg = results['release-version'] as String?; + final pubspecVersion = pubspecYaml.version!; + final pubspecVersionString = + '''${pubspecVersion.major}.${pubspecVersion.minor}.${pubspecVersion.patch}'''; + final releaseVersion = releaseVersionArg ?? + logger.prompt( + '\nWhich release is this patch for?', + defaultValue: pubspecVersionString, + ); + final arch = results['arch'] as String; + final platform = results['platform'] as String; + final channelArg = results['channel'] as String; + logger.info( ''' ${styleBold.wrap(lightGreen.wrap('šŸš€ Ready to publish a new patch!'))} šŸ“± App: ${lightCyan.wrap(app.displayName)} ${lightCyan.wrap('(${app.id})')} -šŸ“¦ Release Version: ${lightCyan.wrap(versionString)} +šŸ“¦ Release Version: ${lightCyan.wrap(releaseVersion)} +āš™ļø Architecture: ${lightCyan.wrap(arch)} +šŸ•¹ļø Platform: ${lightCyan.wrap(platform)} +šŸ“ŗ Channel: ${lightCyan.wrap(channelArg)} #ļøāƒ£ Hash: ${lightCyan.wrap(hash)} ''', ); @@ -188,8 +228,8 @@ Please create a release using "shorebird release" and try again. await codePushClient.createPatchArtifact( patchId: patch.id, artifactPath: artifact.path, - arch: _arch, - platform: _platform, + arch: arch, + platform: platform, hash: hash, ); createArtifactProgress.complete(); @@ -203,7 +243,7 @@ Please create a release using "shorebird release" and try again. try { final channels = await codePushClient.getChannels(appId: app.id); channel = channels.firstWhereOrNull( - (channel) => channel.name == _channel, + (channel) => channel.name == channelArg, ); fetchChannelsProgress.complete(); } catch (error) { @@ -216,7 +256,7 @@ Please create a release using "shorebird release" and try again. try { channel = await codePushClient.createChannel( appId: app.id, - channel: _channel, + channel: channelArg, ); createChannelProgress.complete(); } catch (error) { @@ -237,7 +277,7 @@ Please create a release using "shorebird release" and try again. return ExitCode.software.code; } - logger.success('\nāœ… Published Successfully!'); + logger.success('\nāœ… Published Patch!'); return ExitCode.success.code; } } diff --git a/packages/shorebird_cli/lib/src/commands/release_command.dart b/packages/shorebird_cli/lib/src/commands/release_command.dart index 8b35233e..a4354b38 100644 --- a/packages/shorebird_cli/lib/src/commands/release_command.dart +++ b/packages/shorebird_cli/lib/src/commands/release_command.dart @@ -223,7 +223,7 @@ ${link(uri: Uri.parse('https://support.google.com/googleplay/android-developer/a return ExitCode.software.code; } - logger.success('\nāœ… Released Successfully!'); + logger.success('\nāœ… Published Release!'); return ExitCode.success.code; } } diff --git a/packages/shorebird_cli/test/src/commands/publish_command_test.dart b/packages/shorebird_cli/test/src/commands/patch_command_test.dart similarity index 95% rename from packages/shorebird_cli/test/src/commands/publish_command_test.dart rename to packages/shorebird_cli/test/src/commands/patch_command_test.dart index be5d76f5..dd445777 100644 --- a/packages/shorebird_cli/test/src/commands/publish_command_test.dart +++ b/packages/shorebird_cli/test/src/commands/patch_command_test.dart @@ -8,7 +8,7 @@ import 'package:mocktail/mocktail.dart'; import 'package:path/path.dart' as p; import 'package:shorebird_cli/src/auth/auth.dart'; import 'package:shorebird_cli/src/auth/session.dart'; -import 'package:shorebird_cli/src/commands/publish_command.dart'; +import 'package:shorebird_cli/src/commands/patch.dart'; import 'package:shorebird_cli/src/config/config.dart'; import 'package:shorebird_code_push_client/shorebird_code_push_client.dart'; import 'package:test/test.dart'; @@ -26,17 +26,20 @@ class _MockProcessResult extends Mock implements ProcessResult {} class _MockCodePushClient extends Mock implements CodePushClient {} void main() { - group('publish', () { + group('patch', () { const session = Session(apiKey: 'test-api-key'); const appId = 'test-app-id'; const version = '1.2.3'; + const arch = 'aarch64'; + const platform = 'android'; + const channelName = 'stable'; const appDisplayName = 'Test App'; const appMetadata = AppMetadata(appId: appId, displayName: appDisplayName); const patchArtifact = PatchArtifact( id: 0, patchId: 0, - arch: 'aarch64', - platform: 'android', + arch: arch, + platform: platform, hash: '#', size: 42, url: 'https://example.com', @@ -48,7 +51,7 @@ void main() { displayName: '1.2.3', ); const patch = Patch(id: 0, number: 1); - const channel = Channel(id: 0, appId: appId, name: 'stable'); + const channel = Channel(id: 0, appId: appId, name: channelName); const pubspecYamlContent = ''' name: example version: $version @@ -66,7 +69,7 @@ flutter: late Logger logger; late ProcessResult processResult; late CodePushClient codePushClient; - late PublishCommand command; + late PatchCommand command; late Uri? capturedHostedUri; Directory setUpTempDir() { @@ -88,7 +91,7 @@ flutter: logger = _MockLogger(); processResult = _MockProcessResult(); codePushClient = _MockCodePushClient(); - command = PublishCommand( + command = PatchCommand( auth: auth, buildCodePushClient: ({required String apiKey, Uri? hostedUri}) { capturedHostedUri = hostedUri; @@ -107,8 +110,14 @@ flutter: testApplicationConfigHome = (_) => applicationConfigHome.path; when(() => argResults.rest).thenReturn([]); + when(() => argResults['arch']).thenReturn(arch); + when(() => argResults['platform']).thenReturn(platform); + when(() => argResults['channel']).thenReturn(channelName); when(() => auth.currentSession).thenReturn(session); when(() => logger.progress(any())).thenReturn(progress); + when( + () => logger.prompt(any(), defaultValue: any(named: 'defaultValue')), + ).thenReturn(version); when(() => logger.confirm(any())).thenReturn(true); when(() => processResult.exitCode).thenReturn(ExitCode.success.code); when( @@ -552,7 +561,7 @@ Please create a release using "shorebird release" and try again. expect(exitCode, ExitCode.software.code); }); - test('succeeds when publish is successful', () async { + test('succeeds when patch is successful', () async { final tempDir = setUpTempDir(); Directory( p.join(command.shorebirdEnginePath, 'engine'), @@ -574,12 +583,12 @@ Please create a release using "shorebird release" and try again. command.run, getCurrentDirectory: () => tempDir, ); - verify(() => logger.success('\nāœ… Published Successfully!')).called(1); + verify(() => logger.success('\nāœ… Published Patch!')).called(1); expect(exitCode, ExitCode.success.code); expect(capturedHostedUri, isNull); }); - test('succeeds when publish is successful using custom base_url', () async { + test('succeeds when patch is successful using custom base_url', () async { final tempDir = setUpTempDir(); const baseUrl = 'https://example.com'; File( 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 7b6be83f..51b3b5ca 100644 --- a/packages/shorebird_cli/test/src/commands/release_command_test.dart +++ b/packages/shorebird_cli/test/src/commands/release_command_test.dart @@ -438,7 +438,7 @@ Did you forget to run "shorebird init"?''', command.run, getCurrentDirectory: () => tempDir, ); - verify(() => logger.success('\nāœ… Released Successfully!')).called(1); + verify(() => logger.success('\nāœ… Published Release!')).called(1); expect(exitCode, ExitCode.success.code); expect(capturedHostedUri, isNull); });