run method Null safety
override
Runs this command.
The return value is wrapped in a Future
if necessary and returned by
CommandRunner.runCommand.
Implementation
@override
Future<int> run() async {
final privateKey = _privateKey;
final keyId = _keyId;
final issuer = _issuerId;
final appId = _appId;
final upgradeMode = _upgradeMode;
final next = _next;
late AppStoreCredentials credentials;
_logger.write('\n');
try {
credentials = AppStoreCredentials(
privateKey: privateKey,
keyId: keyId,
issuerId: issuer,
);
} catch (e) {
_logger.err('Error signing credentials: $e');
usageException(_invalidCredentialsError);
}
final client = AppStoreConnectApi(
client: http.Client(),
credentials: credentials,
);
late final AppStoreGenericResponse<AppStoreVersion, AppStoreBuild>
appStoreVersionResponse;
late final AppStoreGenericResponse<AppStorePreReleaseVersion, AppStoreBuild>
appStorePreReleaseVersionResponse;
final appStoreVersionChecking =
_logger.progress('Getting the latest version from App Store Connect');
try {
appStoreVersionResponse = await client.appStoreVersions(appId);
appStorePreReleaseVersionResponse =
await client.preReleaseVersions(appId);
} catch (e) {
appStoreVersionChecking
.fail('Error getting the latest version from App Store Connect: $e');
_logger
.err('Error getting the latest version from App Store Connect: $e');
exit(1);
}
final latestAppStoreVersion =
appStoreVersionResponse.latestVersion.attributes.version;
final latestPreReleaseVersion =
appStorePreReleaseVersionResponse.latestVersion.attributes.version;
final latestIsPreRelease = latestPreReleaseVersion > latestAppStoreVersion;
final latestVersion = Version.primary([
latestAppStoreVersion,
latestPreReleaseVersion,
]);
appStoreVersionChecking.complete(
'The latest version from App Store Connect is $latestVersion',
);
if (upgradeMode == UpgradeMode.never) {
exit(ExitCode.success.code);
}
late final PubSpec pubspec;
try {
pubspec = await PubSpec.load(Directory.current);
} catch (e) {
_logger.err(
'An error occured loading the pubspec.yaml file. '
'Check that you are in the root of the project and '
'that the file is properly formatted.',
);
exit(ExitCode.ioError.code);
}
final currentVersion = pubspec.version ?? Version.none;
final hasNewerVersion = currentVersion <= latestVersion;
final shouldUpgrade = upgradeMode == UpgradeMode.always ||
hasNewerVersion && upgradeMode == UpgradeMode.outdated;
if (!shouldUpgrade) {
_logger.success(
'The app version is already higher than the one in the App Store.',
);
exit(ExitCode.success.code);
}
final versionUpgradingProgress =
_logger.progress('Upgrading the app to the latest version...');
late final Version nextVersion;
if (next == NextVersion.build) {
if (latestIsPreRelease) {
nextVersion = latestVersion.next(next);
} else {
nextVersion = latestVersion.next(NextVersion.patch).next(next);
}
} else {
nextVersion = latestVersion.next(next).next(next);
}
try {
final updatedPubspc = pubspec.copy(version: nextVersion);
await updatedPubspc.save(Directory.current);
versionUpgradingProgress.complete();
_logger.success('The app version has been upgraded to $nextVersion.');
} catch (e) {
versionUpgradingProgress.fail(
'An error occured updating the pubspec.yaml file. '
'Check that you are in the root of the project and '
'that the file is properly formatted.',
);
exit(ExitCode.ioError.code);
}
exit(ExitCode.success.code);
}