Skip to content

Commit

Permalink
refactor(shorebird_cli): use scoped ShorebirdEnv and remove `buildC…
Browse files Browse the repository at this point in the history
…odePushClient` (#1005)
  • Loading branch information
felangel authored Aug 3, 2023
1 parent 32d9893 commit 6b50023
Show file tree
Hide file tree
Showing 75 changed files with 1,857 additions and 2,112 deletions.
2 changes: 2 additions & 0 deletions packages/shorebird_cli/bin/shorebird.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import 'package:shorebird_cli/src/java.dart';
import 'package:shorebird_cli/src/logger.dart';
import 'package:shorebird_cli/src/platform.dart';
import 'package:shorebird_cli/src/process.dart';
import 'package:shorebird_cli/src/shorebird_env.dart';
import 'package:shorebird_cli/src/shorebird_validator.dart';
import 'package:shorebird_cli/src/shorebird_version_manager.dart';
import 'package:shorebird_cli/src/xcodebuild.dart';
Expand All @@ -40,6 +41,7 @@ Future<void> main(List<String> args) async {
loggerRef,
platformRef,
processRef,
shorebirdEnvRef,
shorebirdValidatorRef,
shorebirdVersionManagerRef,
xcodeBuildRef,
Expand Down
8 changes: 4 additions & 4 deletions packages/shorebird_cli/lib/src/cache.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import 'package:path/path.dart' as p;
import 'package:platform/platform.dart';
import 'package:scoped/scoped.dart';
import 'package:shorebird_cli/src/http_client/http_client.dart';
import 'package:shorebird_cli/src/shorebird_environment.dart';
import 'package:shorebird_cli/src/platform.dart';
import 'package:shorebird_cli/src/shorebird_env.dart';

typedef ArchiveExtracter = Future<void> Function(
String archivePath,
Expand All @@ -32,7 +33,6 @@ class Cache {
Cache({
http.Client? httpClient,
this.extractArchive = _defaultArchiveExtractor,
Platform platform = const LocalPlatform(),
}) : httpClient = httpClient ?? retryingHttpClient(http.Client()) {
registerArtifact(PatchArtifact(cache: this, platform: platform));
registerArtifact(BundleToolArtifact(cache: this, platform: platform));
Expand Down Expand Up @@ -72,7 +72,7 @@ class Cache {
/// The Shorebird cache directory.
static Directory get shorebirdCacheDirectory {
return Directory(
p.join(ShorebirdEnvironment.shorebirdRoot.path, 'bin', 'cache'),
p.join(shorebirdEnv.shorebirdRoot.path, 'bin', 'cache'),
);
}

Expand Down Expand Up @@ -174,7 +174,7 @@ class PatchArtifact extends CachedArtifact {
artifactName += 'windows-x64.zip';
}

return '${cache.storageBaseUrl}/${cache.storageBucket}/shorebird/${ShorebirdEnvironment.shorebirdEngineRevision}/$artifactName';
return '${cache.storageBaseUrl}/${cache.storageBucket}/shorebird/${shorebirdEnv.shorebirdEngineRevision}/$artifactName';
}
}

Expand Down
23 changes: 21 additions & 2 deletions packages/shorebird_cli/lib/src/code_push_client_wrapper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import 'package:scoped/scoped.dart';
import 'package:shorebird_cli/src/auth/auth.dart';
import 'package:shorebird_cli/src/logger.dart';
import 'package:shorebird_cli/src/shorebird_build_mixin.dart';
import 'package:shorebird_cli/src/shorebird_environment.dart';
import 'package:shorebird_cli/src/shorebird_env.dart';
import 'package:shorebird_cli/src/third_party/flutter_tools/lib/flutter_tools.dart';
import 'package:shorebird_code_push_client/shorebird_code_push_client.dart';

Expand Down Expand Up @@ -44,7 +44,7 @@ ScopedRef<CodePushClientWrapper> codePushClientWrapperRef = create(() {
return CodePushClientWrapper(
codePushClient: CodePushClient(
httpClient: auth.client,
hostedUri: ShorebirdEnvironment.hostedUri,
hostedUri: shorebirdEnv.hostedUri,
),
);
});
Expand All @@ -63,6 +63,25 @@ class CodePushClientWrapper {

final CodePushClient codePushClient;

Future<App> createApp({String? appName}) async {
late final String displayName;
if (appName == null) {
String? defaultAppName;
try {
defaultAppName = shorebirdEnv.getPubspecYaml()?.name;
} catch (_) {}

displayName = logger.prompt(
'${lightGreen.wrap('?')} How should we refer to this app?',
defaultValue: defaultAppName,
);
} else {
displayName = appName;
}

return codePushClient.createApp(displayName: displayName);
}

Future<List<AppMetadata>> getApps() async {
final fetchAppsProgress = logger.progress('Fetching apps');
try {
Expand Down
6 changes: 0 additions & 6 deletions packages/shorebird_cli/lib/src/command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@ typedef StartProcess = Future<Process> Function(
});

abstract class ShorebirdCommand extends Command<int> {
ShorebirdCommand({
CodePushClientBuilder? buildCodePushClient,
}) : buildCodePushClient = buildCodePushClient ?? CodePushClient.new;

final CodePushClientBuilder buildCodePushClient;

// We don't currently have a test involving both a CommandRunner
// and a Command, so we can't test this getter.
// coverage:ignore-start
Expand Down
4 changes: 2 additions & 2 deletions packages/shorebird_cli/lib/src/command_runner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import 'package:scoped/scoped.dart';
import 'package:shorebird_cli/src/commands/commands.dart';
import 'package:shorebird_cli/src/logger.dart';
import 'package:shorebird_cli/src/process.dart';
import 'package:shorebird_cli/src/shorebird_environment.dart';
import 'package:shorebird_cli/src/shorebird_env.dart';
import 'package:shorebird_cli/src/version.dart';

const executableName = 'shorebird';
Expand Down Expand Up @@ -144,7 +144,7 @@ ${lightCyan.wrap('shorebird release android -- --no-pub lib/main.dart')}''',
logger.info(
'''
Shorebird $packageVersion
Shorebird Engine • revision ${ShorebirdEnvironment.shorebirdEngineRevision}''',
Shorebird Engine • revision ${shorebirdEnv.shorebirdEngineRevision}''',
);
exitCode = ExitCode.success.code;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ import 'dart:async';

import 'package:intl/intl.dart';
import 'package:mason_logger/mason_logger.dart';
import 'package:shorebird_cli/src/auth/auth.dart';
import 'package:shorebird_cli/src/code_push_client_wrapper.dart';
import 'package:shorebird_cli/src/command.dart';
import 'package:shorebird_cli/src/logger.dart';
import 'package:shorebird_cli/src/shorebird_environment.dart';
import 'package:shorebird_cli/src/shorebird_validator.dart';
import 'package:shorebird_code_push_client/shorebird_code_push_client.dart';

class DowngradeAccountCommand extends ShorebirdCommand {
DowngradeAccountCommand({super.buildCodePushClient});
DowngradeAccountCommand();

@override
String get name => 'downgrade';
Expand All @@ -28,14 +27,10 @@ class DowngradeAccountCommand extends ShorebirdCommand {
return e.exitCode.code;
}

final client = buildCodePushClient(
httpClient: auth.client,
hostedUri: ShorebirdEnvironment.hostedUri,
);

final User user;
try {
final currentUser = await client.getCurrentUser();
final currentUser =
await codePushClientWrapper.codePushClient.getCurrentUser();
if (currentUser == null) {
throw Exception('Failed to retrieve user information.');
}
Expand Down Expand Up @@ -65,7 +60,8 @@ class DowngradeAccountCommand extends ShorebirdCommand {

final DateTime cancellationDate;
try {
cancellationDate = await client.cancelSubscription();
cancellationDate =
await codePushClientWrapper.codePushClient.cancelSubscription();
} catch (error) {
progress.fail('Failed to downgrade plan. Error: $error');
return ExitCode.software.code;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import 'dart:async';

import 'package:mason_logger/mason_logger.dart';
import 'package:shorebird_cli/src/auth/auth.dart';
import 'package:shorebird_cli/src/code_push_client_wrapper.dart';
import 'package:shorebird_cli/src/command.dart';
import 'package:shorebird_cli/src/logger.dart';
import 'package:shorebird_cli/src/shorebird_environment.dart';
import 'package:shorebird_cli/src/shorebird_validator.dart';
import 'package:shorebird_code_push_client/shorebird_code_push_client.dart';

Expand All @@ -13,7 +12,7 @@ import 'package:shorebird_code_push_client/shorebird_code_push_client.dart';
/// {@endtemplate}
class UpgradeAccountCommand extends ShorebirdCommand {
/// {@macro upgrade_account_command}
UpgradeAccountCommand({super.buildCodePushClient});
UpgradeAccountCommand();

@override
String get name => 'upgrade';
Expand All @@ -38,16 +37,11 @@ Please use $consoleLink instead.''',
return e.exitCode.code;
}

final client = buildCodePushClient(
httpClient: auth.client,
hostedUri: ShorebirdEnvironment.hostedUri,
);

final progress = logger.progress('Retrieving account information');

final User? user;
try {
user = await client.getCurrentUser();
user = await codePushClientWrapper.codePushClient.getCurrentUser();
if (user == null) {
progress.fail('''
We're having trouble retrieving your account information.
Expand All @@ -69,7 +63,8 @@ Please try logging out using ${lightCyan.wrap('shorebird logout')} and logging b

final Uri paymentLink;
try {
paymentLink = await client.createPaymentLink();
paymentLink =
await codePushClientWrapper.codePushClient.createPaymentLink();
} catch (error) {
progress.fail(error.toString());
return ExitCode.software.code;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import 'dart:async';

import 'package:mason_logger/mason_logger.dart';
import 'package:shorebird_cli/src/code_push_client_wrapper.dart';
import 'package:shorebird_cli/src/command.dart';
import 'package:shorebird_cli/src/logger.dart';
import 'package:shorebird_cli/src/shorebird_config_mixin.dart';
import 'package:shorebird_cli/src/shorebird_create_app_mixin.dart';
import 'package:shorebird_cli/src/shorebird_validator.dart';
import 'package:shorebird_code_push_client/shorebird_code_push_client.dart';

Expand All @@ -13,10 +12,9 @@ import 'package:shorebird_code_push_client/shorebird_code_push_client.dart';
/// `shorebird apps create`
/// Create a new app on Shorebird.
/// {@endtemplate}
class CreateAppCommand extends ShorebirdCommand
with ShorebirdConfigMixin, ShorebirdCreateAppMixin {
class CreateAppCommand extends ShorebirdCommand {
/// {@macro create_app_command}
CreateAppCommand({super.buildCodePushClient}) {
CreateAppCommand() {
argParser.addOption(
'app-name',
help: '''
Expand Down Expand Up @@ -44,7 +42,7 @@ Defaults to the name in "pubspec.yaml".''',
final appName = results['app-name'] as String?;
late final App app;
try {
app = await createApp(appName: appName);
app = await codePushClientWrapper.createApp(appName: appName);
} catch (error) {
logger.err('$error');
return ExitCode.software.code;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import 'dart:async';

import 'package:mason_logger/mason_logger.dart';
import 'package:shorebird_cli/src/auth/auth.dart';
import 'package:shorebird_cli/src/code_push_client_wrapper.dart';
import 'package:shorebird_cli/src/command.dart';
import 'package:shorebird_cli/src/logger.dart';
import 'package:shorebird_cli/src/shorebird_environment.dart';
import 'package:shorebird_cli/src/shorebird_env.dart';
import 'package:shorebird_cli/src/shorebird_validator.dart';

/// {@template delete_app_command}
Expand All @@ -14,7 +14,7 @@ import 'package:shorebird_cli/src/shorebird_validator.dart';
/// {@endtemplate}
class DeleteAppCommand extends ShorebirdCommand {
/// {@macro delete_app_command}
DeleteAppCommand({super.buildCodePushClient}) {
DeleteAppCommand() {
argParser
..addOption(
'app-id',
Expand Down Expand Up @@ -60,7 +60,7 @@ Please use $consoleLink instead.''',
if (appIdArg == null) {
String? defaultAppId;
try {
defaultAppId = ShorebirdEnvironment.getShorebirdYaml()?.appId;
defaultAppId = shorebirdEnv.getShorebirdYaml()?.appId;
} catch (_) {}

appId = logger.prompt(
Expand All @@ -71,11 +71,6 @@ Please use $consoleLink instead.''',
appId = appIdArg;
}

final client = buildCodePushClient(
httpClient: auth.client,
hostedUri: ShorebirdEnvironment.hostedUri,
);

final shouldProceed =
force || logger.confirm('Deleting an app is permanent. Continue?');
if (!shouldProceed) {
Expand All @@ -84,7 +79,7 @@ Please use $consoleLink instead.''',
}

try {
await client.deleteApp(appId: appId);
await codePushClientWrapper.codePushClient.deleteApp(appId: appId);
} catch (error) {
logger.err('$error');
return ExitCode.software.code;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@ import 'package:path/path.dart' as p;
import 'package:shorebird_cli/src/command.dart';
import 'package:shorebird_cli/src/logger.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_env.dart';
import 'package:shorebird_cli/src/shorebird_validator.dart';

/// {@template build_aar_command}
///
/// `shorebird build aar`
/// Build an Android aar file from your app.
/// {@endtemplate}
class BuildAarCommand extends ShorebirdCommand
with ShorebirdConfigMixin, ShorebirdBuildMixin {
class BuildAarCommand extends ShorebirdCommand with ShorebirdBuildMixin {
BuildAarCommand() {
// We would have a "target" option here, similar to what [BuildApkCommand]
// and [BuildAabCommand] have, but target cannot currently be configured in
Expand Down Expand Up @@ -47,7 +46,7 @@ class BuildAarCommand extends ShorebirdCommand
return e.exitCode.code;
}

if (androidPackageName == null) {
if (shorebirdEnv.androidPackageName == null) {
logger.err('Could not find androidPackage in pubspec.yaml.');
return ExitCode.config.code;
}
Expand All @@ -68,7 +67,7 @@ class BuildAarCommand extends ShorebirdCommand
'host',
'outputs',
'repo',
...androidPackageName!.split('.'),
...shorebirdEnv.androidPackageName!.split('.'),
'flutter_release',
buildNumber,
'flutter_release-$buildNumber.aar',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@ import 'package:shorebird_cli/src/command.dart';
import 'package:shorebird_cli/src/doctor.dart';
import 'package:shorebird_cli/src/logger.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_validator.dart';

/// {@template build_apk_command}
///
/// `shorebird build apk`
/// Build an Android APK file from your app.
/// {@endtemplate}
class BuildApkCommand extends ShorebirdCommand
with ShorebirdConfigMixin, ShorebirdBuildMixin {
class BuildApkCommand extends ShorebirdCommand with ShorebirdBuildMixin {
/// {@macro build_apk_command}
BuildApkCommand() {
argParser
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@ import 'package:shorebird_cli/src/command.dart';
import 'package:shorebird_cli/src/doctor.dart';
import 'package:shorebird_cli/src/logger.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_validator.dart';

/// {@template build_app_bundle_command}
///
/// `shorebird build appbundle`
/// Build an Android App Bundle file from your app.
/// {@endtemplate}
class BuildAppBundleCommand extends ShorebirdCommand
with ShorebirdConfigMixin, ShorebirdBuildMixin {
class BuildAppBundleCommand extends ShorebirdCommand with ShorebirdBuildMixin {
/// {@macro build_app_bundle_command}
BuildAppBundleCommand() {
argParser
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@ import 'package:shorebird_cli/src/command.dart';
import 'package:shorebird_cli/src/doctor.dart';
import 'package:shorebird_cli/src/logger.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_validator.dart';

/// {@template build_ipa_command}
/// `shorebird build ipa`
/// Builds an .xcarchive and optionally .ipa for an iOS app to be generated for
/// App Store submission.
/// {@endtemplate}
class BuildIpaCommand extends ShorebirdCommand
with ShorebirdConfigMixin, ShorebirdBuildMixin {
class BuildIpaCommand extends ShorebirdCommand with ShorebirdBuildMixin {
/// {@macro build_ipa_command}
BuildIpaCommand() {
argParser
Expand Down
Loading

0 comments on commit 6b50023

Please sign in to comment.