Skip to content

Commit

Permalink
feat(shorebird_cli): shorebird patch aar optimizations (#1026)
Browse files Browse the repository at this point in the history
  • Loading branch information
felangel authored Aug 4, 2023
1 parent 1554cf2 commit 0984683
Show file tree
Hide file tree
Showing 2 changed files with 206 additions and 166 deletions.
120 changes: 63 additions & 57 deletions packages/shorebird_cli/lib/src/commands/patch/patch_aar_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:async';
import 'dart:io';

import 'package:archive/archive_io.dart';
import 'package:collection/collection.dart';
import 'package:crypto/crypto.dart';
import 'package:http/http.dart' as http;
import 'package:mason_logger/mason_logger.dart';
Expand Down Expand Up @@ -51,15 +52,6 @@ The version of the associated release (e.g. "1.0.0"). This should be the version
of the Android app that is using this module.''',
mandatory: true,
)
..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',
)
..addFlag(
'force',
abbr: 'f',
Expand Down Expand Up @@ -112,16 +104,30 @@ of the Android app that is using this module.''',
return ExitCode.config.code;
}

final buildNumber = results['build-number'] as String;
final releaseVersion = results['release-version'] as String;

final shorebirdYaml = shorebirdEnv.getShorebirdYaml()!;
final appId = shorebirdYaml.getAppId();
final app = await codePushClientWrapper.getApp(appId: appId);
final release = await codePushClientWrapper.getRelease(
appId: appId,
releaseVersion: releaseVersion,
final releases = await codePushClientWrapper.getReleases(appId: appId);
final releaseVersion = results['release-version'] as String? ??
await _promptForReleaseVersion(releases);

final release = releases.firstWhereOrNull(
(r) => r.version == releaseVersion,
);

if (releaseVersion == null || release == null) {
logger.info('No releases found');
return ExitCode.success.code;
}

if (release.platformStatuses[ReleasePlatform.android] ==
ReleaseStatus.draft) {
logger.err('''
Release $releaseVersion is in an incomplete state. It's possible that the original release was terminated or failed to complete.
Please re-run the release command for this version or create a new release.''');
return ExitCode.software.code;
}

final shorebirdFlutterRevision = shorebirdEnv.flutterRevision;
if (release.flutterRevision != shorebirdFlutterRevision) {
final installFlutterRevisionProgress = logger.progress(
Expand All @@ -138,6 +144,32 @@ of the Android app that is using this module.''',
}
}

const platform = ReleasePlatform.android;
final releaseArtifacts = await codePushClientWrapper.getReleaseArtifacts(
appId: appId,
releaseId: release.id,
architectures: architectures,
platform: platform,
);

final releaseAarArtifact = await codePushClientWrapper.getReleaseArtifact(
appId: appId,
releaseId: release.id,
arch: 'aar',
platform: platform,
);

final Map<Arch, String> releaseArtifactPaths;
try {
releaseArtifactPaths = await _downloadReleaseArtifacts(
releaseArtifacts: releaseArtifacts,
httpClient: _httpClient,
);
} catch (_) {
return ExitCode.software.code;
}

final buildNumber = results['build-number'] as String;
final buildProgress = logger.progress('Building patch');
try {
await runScoped(
Expand All @@ -156,30 +188,10 @@ of the Android app that is using this module.''',
return ExitCode.software.code;
}

const platform = ReleasePlatform.android;
final channelName = results['channel'] as String;

if (release.platformStatuses[ReleasePlatform.android] ==
ReleaseStatus.draft) {
logger.err('''
Release $releaseVersion is in an incomplete state. It's possible that the original release was terminated or failed to complete.
Please re-run the release command for this version or create a new release.''');
return ExitCode.software.code;
}

final releaseArtifacts = await codePushClientWrapper.getReleaseArtifacts(
appId: appId,
releaseId: release.id,
architectures: architectures,
platform: platform,
);

final releaseAarArtifact = await codePushClientWrapper.getReleaseArtifact(
appId: appId,
releaseId: release.id,
arch: 'aar',
platform: platform,
final extractedAarDir = await extractAar(
packageName: shorebirdEnv.androidPackageName!,
buildNumber: buildNumber,
unzipFn: _unzipFn,
);

final shouldContinue =
Expand All @@ -194,25 +206,8 @@ Please re-run the release command for this version or create a new release.''');
archiveDiffer: _archiveDiffer,
force: force,
);
if (!shouldContinue) {
return ExitCode.success.code;
}

final Map<Arch, String> releaseArtifactPaths;
try {
releaseArtifactPaths = await _downloadReleaseArtifacts(
releaseArtifacts: releaseArtifacts,
httpClient: _httpClient,
);
} catch (_) {
return ExitCode.software.code;
}

final extractedAarDir = await extractAar(
packageName: shorebirdEnv.androidPackageName!,
buildNumber: buildNumber,
unzipFn: _unzipFn,
);
if (!shouldContinue) return ExitCode.success.code;

final patchArtifactBundles = await _createPatchArtifacts(
releaseArtifactPaths: releaseArtifactPaths,
Expand All @@ -235,6 +230,7 @@ Please re-run the release command for this version or create a new release.''');
return ExitCode.success.code;
}

const channelName = 'stable';
final summary = [
'''📱 App: ${lightCyan.wrap(app.displayName)} ${lightCyan.wrap('(${app.appId})')}''',
'📦 Release Version: ${lightCyan.wrap(releaseVersion)}',
Expand Down Expand Up @@ -273,6 +269,16 @@ ${summary.join('\n')}
return ExitCode.success.code;
}

Future<String?> _promptForReleaseVersion(List<Release> releases) async {
if (releases.isEmpty) return null;
final release = logger.chooseOne(
'Which release would you like to patch?',
choices: releases,
display: (release) => release.version,
);
return release.version;
}

Future<Map<Arch, PatchArtifactBundle>?> _createPatchArtifacts({
required Map<Arch, String> releaseArtifactPaths,
required String extractedAarDirectory,
Expand Down
Loading

0 comments on commit 0984683

Please sign in to comment.