Skip to content

Commit

Permalink
feat(shorebird_cli): add shorebird build aar command (#570)
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanoltman authored May 31, 2023
1 parent efda257 commit 6c7473d
Show file tree
Hide file tree
Showing 5 changed files with 393 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/shorebird_cli/lib/src/commands/build/build.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export 'build_aar_command.dart';
export 'build_apk_command.dart';
export 'build_app_bundle_command.dart';
export 'build_command.dart';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import 'dart:async';
import 'dart:io';

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/shorebird_build_mixin.dart';
import 'package:shorebird_cli/src/shorebird_config_mixin.dart';
import 'package:shorebird_cli/src/shorebird_validation_mixin.dart';

/// {@template build_aar_command}
///
/// `shorebird build aar`
/// Build an Android aar file from your app.
/// {@endtemplate}
class BuildAarCommand extends ShorebirdCommand
with
AuthLoggerMixin,
ShorebirdValidationMixin,
ShorebirdConfigMixin,
ShorebirdBuildMixin {
BuildAarCommand({
required super.logger,
super.auth,
}) {
// We would have a "target" option here, similar to what [BuildApkCommand]
// and [BuildAabCommand] have, but target cannot currently be configured in
// `flutter build aar` and is always assumed to be lib/main.dart.
argParser
..addOption(
'flavor',
help: 'The product flavor to use when building the app.',
)
// `flutter build aar` defaults to a build number of 1.0, so we do the
// same.
..addOption(
'build-number',
help: 'The build number of the aar',
defaultsTo: '1.0',
);
}

@override
String get name => 'aar';

@override
String get description => 'Build an Android AAR file from your module.';

@override
Future<int> run() async {
if (!auth.isAuthenticated) {
printNeedsAuthInstructions();
return ExitCode.noUser.code;
}

final pubspec = getPubspecYaml();
if (pubspec == null) {
logger.err('No pubspec.yaml file found.');
return ExitCode.config.code;
}

final module = pubspec.flutter?['module'] as Map?;
final androidPackageName = module?['androidPackage'] as String?;
if (androidPackageName == null) {
logger.err('Could not find androidPackage in pubspec.yaml.');
return ExitCode.config.code;
}

final flavor = results['flavor'] as String?;
final buildNumber = results['build-number'] as String;
final buildProgress = logger.progress('Building aar');
try {
await buildAar(buildNumber: buildNumber, flavor: flavor);
} on ProcessException catch (error) {
buildProgress.fail('Failed to build: ${error.message}');
return ExitCode.software.code;
}

buildProgress.complete();

final aarPath = p.joinAll([
'build',
'host',
'outputs',
'repo',
...androidPackageName.split('.'),
'flutter_release',
buildNumber,
'flutter_release-$buildNumber.aar',
]);

logger.info('''
📦 Generated an aar at:
${lightCyan.wrap(aarPath)}''');

return ExitCode.success.code;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:shorebird_cli/src/commands/build/build.dart';
class BuildCommand extends ShorebirdCommand {
/// {@macro build_command}
BuildCommand({required super.logger}) {
addSubcommand(BuildAarCommand(logger: logger));
addSubcommand(BuildApkCommand(logger: logger));
addSubcommand(BuildAppBundleCommand(logger: logger));
addSubcommand(BuildIpaCommand(logger: logger));
Expand Down
31 changes: 31 additions & 0 deletions packages/shorebird_cli/lib/src/shorebird_build_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,37 @@ mixin ShorebirdBuildMixin on ShorebirdCommand {
}
}

Future<void> buildAar({
required String buildNumber,
String? flavor,
}) async {
const executable = 'flutter';
final arguments = [
'build',
'aar',
'--no-debug',
'--no-profile',
'--build-number=$buildNumber',
if (flavor != null) '--flavor=$flavor',
...results.rest,
];

final result = await process.run(
executable,
arguments,
runInShell: true,
);

if (result.exitCode != ExitCode.success.code) {
throw ProcessException(
'flutter',
arguments,
result.stderr.toString(),
result.exitCode,
);
}
}

Future<void> buildApk({String? flavor, String? target}) async {
const executable = 'flutter';
final arguments = [
Expand Down
Loading

0 comments on commit 6c7473d

Please sign in to comment.