Skip to content

Commit

Permalink
Add maestro update command and automatically inform about new versi…
Browse files Browse the repository at this point in the history
…on (#85)

* add `maestro update` command and automatically inform about new version

* Update packages/maestro_cli/lib/src/command_runner.dart

* don't fetch latest version when not needed

Co-authored-by: Marcin Wojnarowski <[email protected]>
  • Loading branch information
bartekpacia and shilangyu authored Jul 5, 2022
1 parent be6fccd commit 77d4465
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 5 deletions.
8 changes: 8 additions & 0 deletions AutomatorServer/make
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash
set -euo pipefail

# make is a convenience command for running common Gradle tasks.

./gradlew assembleDebugAndroidTest
./gradlew installDebugAndroidTest
./gradlew install
44 changes: 39 additions & 5 deletions packages/maestro_cli/lib/src/command_runner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import 'package:maestro_cli/src/features/bootstrap/bootstrap_command.dart';
import 'package:maestro_cli/src/features/clean/clean_command.dart';
import 'package:maestro_cli/src/features/doctor/doctor_command.dart';
import 'package:maestro_cli/src/features/drive/drive_command.dart';
import 'package:maestro_cli/src/features/update/update_command.dart';
import 'package:pub_updater/pub_updater.dart';

Future<int> maestroCommandRunner(List<String> args) async {
final devices = await const Adb().devices();
Expand Down Expand Up @@ -44,6 +46,7 @@ class MaestroCommandRunner extends CommandRunner<int> {
addCommand(DriveCommand(devices));
addCommand(DoctorCommand());
addCommand(CleanCommand());
addCommand(UpdateCommand());

argParser
..addFlag(
Expand Down Expand Up @@ -80,7 +83,11 @@ class MaestroCommandRunner extends CommandRunner<int> {
log.info('Debug mode enabled. Non-versioned artifacts will be used.');
}

if (_commandRequiresArtifacts(results.arguments)) {
if (!_isUpdateCommand(results.command?.name)) {
await _checkIfUsingLatestVersion();
}

if (_isCommandRequiringArtifacts(results.command?.name)) {
try {
await _ensureArtifactsArePresent(debugFlag);
} catch (err, st) {
Expand All @@ -95,10 +102,37 @@ class MaestroCommandRunner extends CommandRunner<int> {
}
}

bool _commandRequiresArtifacts(List<String> arguments) {
return !arguments.contains('clean') &&
!arguments.contains('doctor') &&
!arguments.contains('help');
bool _isUpdateCommand(String? commandName) {
return commandName == 'update';
}

bool _isCommandRequiringArtifacts(String? commandName) {
if (commandName == 'clean' ||
commandName == 'doctor' ||
commandName == 'update' ||
commandName == 'help') {
return false;
}

return true;
}

Future<void> _checkIfUsingLatestVersion() async {
final pubUpdater = PubUpdater();

final latestVersion = await pubUpdater.getLatestVersion(maestroCliPackage);
final isLatestVersion = await pubUpdater.isUpToDate(
packageName: maestroCliPackage,
currentVersion: version,
);

if (!isLatestVersion) {
log
..info(
'Newer version of $maestroCliPackage is available ($latestVersion)',
)
..info('Run `maestro update` to update');
}
}

Future<void> _ensureArtifactsArePresent(bool debug) async {
Expand Down
53 changes: 53 additions & 0 deletions packages/maestro_cli/lib/src/features/update/update_command.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import 'package:args/command_runner.dart';
import 'package:pub_updater/pub_updater.dart';

import '../../common/common.dart';

class UpdateCommand extends Command<int> {
UpdateCommand() : _pubUpdater = PubUpdater();

final PubUpdater _pubUpdater;

@override
String get name => 'update';

@override
String get description => 'Updates maestro CLI.';

@override
Future<int> run() async {
final isLatestVersion = await _pubUpdater.isUpToDate(
packageName: maestroCliPackage,
currentVersion: version,
);

if (!isLatestVersion) {
final latestVersion = await _pubUpdater.getLatestVersion(
maestroCliPackage,
);
await _update(latestVersion);
} else {
log.info(
'You already have the newest version of $maestroCliPackage ($version)',
);
}

return 0;
}

Future<void> _update(String latestVersion) async {
final progress = log.progress(
'Updating $maestroCliPackage to version $latestVersion',
);

try {
await _pubUpdater.update(packageName: maestroCliPackage);
progress.complete('Updated $maestroCliPackage to version $latestVersion');
} catch (err) {
progress.fail(
'Failed to update $maestroCliPackage to version $latestVersion',
);
rethrow;
}
}
}
28 changes: 28 additions & 0 deletions packages/maestro_cli/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.6.4"
json_annotation:
dependency: transitive
description:
name: json_annotation
url: "https://pub.dartlang.org"
source: hosted
version: "4.5.0"
leancode_lint:
dependency: "direct dev"
description:
Expand Down Expand Up @@ -225,20 +232,41 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "4.4.0"
platform:
dependency: transitive
description:
name: platform
url: "https://pub.dartlang.org"
source: hosted
version: "3.1.0"
pool:
dependency: transitive
description:
name: pool
url: "https://pub.dartlang.org"
source: hosted
version: "1.5.0"
process:
dependency: transitive
description:
name: process
url: "https://pub.dartlang.org"
source: hosted
version: "4.2.4"
pub_semver:
dependency: transitive
description:
name: pub_semver
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.1"
pub_updater:
dependency: "direct main"
description:
name: pub_updater
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.2"
quiver:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions packages/maestro_cli/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ dependencies:
logging: ^1.0.2
mason_logger: ^0.1.0-dev.10
path: ^1.8.2
pub_updater: ^0.2.2
toml: ^0.12.0
yaml: ^3.1.1
dev_dependencies:
Expand Down

0 comments on commit 77d4465

Please sign in to comment.