Skip to content

Commit

Permalink
make update command also downloadn new artifacts
Browse files Browse the repository at this point in the history
  • Loading branch information
bartekpacia committed Oct 18, 2022
1 parent 6630273 commit 1c6955d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 26 deletions.
8 changes: 7 additions & 1 deletion packages/patrol_cli/lib/src/command_runner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,13 @@ class PatrolCommandRunner extends CommandRunner<int> {
logger: _logger,
),
);
addCommand(UpdateCommand(pubUpdater: _pubUpdater, logger: _logger));
addCommand(
UpdateCommand(
pubUpdater: _pubUpdater,
artifactsRepository: _artifactsRepository,
logger: _logger,
),
);

argParser
..addFlag(
Expand Down
32 changes: 17 additions & 15 deletions packages/patrol_cli/lib/src/common/artifacts_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -255,33 +255,35 @@ class ArtifactsRepository {
}
}

/// Downloads artifacts for the current patrol_cli version.
Future<void> downloadArtifacts({String? ver}) async {
ver ??= globalVersion;
/// Downloads and extracts artifacts for [version] of patrol_cli.
Future<void> downloadArtifacts({String? version = globalVersion}) async {
final androidArtifacts = [
Artifacts.androidApp,
Artifacts.androidInstrumentation,
].map((artifact) => artifact.copyWith(version: version));

final iosArtifacts = [
Artifacts.ios,
Artifacts.iosDevice,
Artifacts.iosSimulatorArm,
Artifacts.iosSimulatorAmd,
].map((artifact) => artifact.copyWith(version: version));

await Future.wait<void>([
_downloadArtifact(Artifacts.androidApp.copyWith(version: ver)),
_downloadArtifact(
Artifacts.androidInstrumentation.copyWith(version: ver),
),
...androidArtifacts.map(_downloadArtifact),
if (platform.isMacOS) ...[
_downloadArtifact(Artifacts.ios),
_downloadArtifact(Artifacts.iosDevice),
_downloadArtifact(Artifacts.iosSimulatorArm),
_downloadArtifact(Artifacts.iosSimulatorAmd),
...iosArtifacts.map(_downloadArtifact),
],
]);

if (platform.isMacOS) {
await _extractArtifact(Artifacts.ios);
await _extractArtifact(Artifacts.iosDevice);
await _extractArtifact(Artifacts.iosSimulatorArm);
await _extractArtifact(Artifacts.iosSimulatorAmd);
await Future.wait<void>(iosArtifacts.map(_extractArtifact));
}
}

Future<void> _downloadArtifact(Artifact artifact) async {
final response = await _httpClient.get(artifact.uri);
print('downloading ${artifact.uri.path}');

if (response.statusCode != 200) {
throw Exception('Failed to download $artifact from ${artifact.uri}');
Expand Down
33 changes: 23 additions & 10 deletions packages/patrol_cli/lib/src/features/update/update_command.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import 'package:args/command_runner.dart';
import 'package:logging/logging.dart';
import 'package:patrol_cli/src/common/artifacts_repository.dart';
import 'package:patrol_cli/src/common/common.dart';
import 'package:pub_updater/pub_updater.dart';

class UpdateCommand extends Command<int> {
UpdateCommand({
required PubUpdater pubUpdater,
required ArtifactsRepository artifactsRepository,
required Logger logger,
}) : _pubUpdater = pubUpdater,
_artifactsRepository = artifactsRepository,
_logger = logger;

final PubUpdater _pubUpdater;
final ArtifactsRepository _artifactsRepository;
final Logger _logger;

@override
Expand All @@ -27,10 +31,10 @@ class UpdateCommand extends Command<int> {
);

if (!isLatestVersion) {
final latestVersion = await _pubUpdater.getLatestVersion(
patrolCliPackage,
);
await _update(latestVersion);
final latestVersion =
await _pubUpdater.getLatestVersion(patrolCliPackage);
await _updateTool(latestVersion);
await _downloadArtifacts(latestVersion);
} else {
_logger.info(
'You already have the newest version of $patrolCliPackage ($globalVersion)',
Expand All @@ -40,18 +44,27 @@ class UpdateCommand extends Command<int> {
return 0;
}

Future<void> _update(String latestVersion) async {
Future<void> _updateTool(String ver) async {
final progress = _logger.progress(
'Updating $patrolCliPackage to version $latestVersion',
'Updating $patrolCliPackage to version $ver',
);

try {
await _pubUpdater.update(packageName: patrolCliPackage);
progress.complete('Updated $patrolCliPackage to version $latestVersion');
progress.complete('Updated $patrolCliPackage to version $ver');
} catch (err) {
progress.fail(
'Failed to update $patrolCliPackage to version $latestVersion',
);
progress.fail('Failed to update $patrolCliPackage to version $ver');
rethrow;
}
}

Future<void> _downloadArtifacts(String ver) async {
final progress = _logger.progress('Downloading artifacts for version $ver');
try {
await _artifactsRepository.downloadArtifacts(version: ver);
progress.complete('Downloaded artifacts for version $ver');
} catch (err) {
progress.fail('Failed to download artifacts for version $ver');
rethrow;
}
}
Expand Down

0 comments on commit 1c6955d

Please sign in to comment.