Skip to content

Commit

Permalink
Merge pull request mmcc007#85 from mmcc007/#79_adb_path
Browse files Browse the repository at this point in the history
Improved check for adb path
  • Loading branch information
mmcc007 authored Jul 17, 2019
2 parents 5346c65 + 68c3456 commit bf10e98
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 24 deletions.
56 changes: 34 additions & 22 deletions bin/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:io';

import 'package:args/args.dart';
import 'package:screenshots/screenshots.dart';
import 'package:path/path.dart' as path;

const usage =
'usage: screenshots [-h] [-c <config file>] [-m <normal|recording|comparison|archive>]';
Expand Down Expand Up @@ -34,6 +35,12 @@ void main(List<String> arguments) async {
_handleError(argParser, e.toString());
}

// show help
if (argResults[helpArg]) {
_showUsage(argParser);
exit(0);
}

// confirm os
switch (Platform.operatingSystem) {
case 'windows':
Expand Down Expand Up @@ -64,30 +71,11 @@ void main(List<String> arguments) async {
exit(1);
}

// check adb is in path
if (!cmd('sh', ['-c', 'which adb && echo adb || echo not installed'], '.',
true)
.toString()
.contains('adb')) {
stderr.write(
'#############################################################\n');
stderr.write("# 'adb' must be in the PATH to use Screenshots\n");
stderr.write("# You can usually add it to the PATH using\n"
"# export PATH='~/Library/Android/sdk/platform-tools:\$PATH' \n");
stderr.write(
'#############################################################\n');
exit(1);
}

// show help
if (argResults[helpArg]) {
_showUsage(argParser);
exit(0);
}
// check adb is found
getAdbPath();

// validate args
final file = File(argResults[configArg]);
if (!await file.exists()) {
if (!await File(argResults[configArg]).exists()) {
_handleError(argParser, "File not found: ${argResults[configArg]}");
}

Expand All @@ -105,3 +93,27 @@ void _showUsage(ArgParser argParser) {
print(argParser.usage);
exit(2);
}

/// Path to the `adb` executable.
String getAdbPath() {
final String androidHome = Platform.environment['ANDROID_HOME'] ??
Platform.environment['ANDROID_SDK_ROOT'];
if (androidHome == null) {
throw 'The ANDROID_SDK_ROOT and ANDROID_HOME environment variables are '
'missing. At least one of these variables must point to the Android '
'SDK directory containing platform-tools.';
}
final String adbPath = path.join(androidHome, 'platform-tools/adb');
final absPath = path.absolute(adbPath);
if (!File(adbPath).existsSync()) {
stderr.write(
'#############################################################\n');
stderr.write("# 'adb' must be in the PATH to use Screenshots\n");
stderr.write("# You can usually add it to the PATH using\n"
"# export PATH='\$HOME/Library/Android/sdk/platform-tools:\$PATH' \n");
stderr.write(
'#############################################################\n');
exit(1);
}
return absPath;
}
19 changes: 19 additions & 0 deletions test/common.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:async';
import 'dart:io';

import 'package:meta/meta.dart';
import 'package:screenshots/src/run.dart' as run;
import 'package:path/path.dart' as p;

Expand Down Expand Up @@ -101,6 +102,24 @@ Map diffMaps(Map orig, Map diff, {bool verbose = false}) {
return diffs;
}

/// Returns a future that completes with a path suitable for ANDROID_HOME
/// or with null, if ANDROID_HOME cannot be found.
Future<String> findAndroidHome() async {
final Iterable<String> hits = grep(
'ANDROID_HOME = ',
from: await run.cmd('flutter', <String>['doctor', '-v'], '.', true),
);
if (hits.isEmpty) return null;
return hits.first.split('= ').last;
}

/// Splits [from] into lines and selects those that contain [pattern].
Iterable<String> grep(Pattern pattern, {@required String from}) {
return from.split('\n').where((String line) {
return line.contains(pattern);
});
}

///// Wait for android emulator to stop.
//Future<void> waitAndroidEmulatorShutdown(String deviceId) async {
// int timeout = 100;
Expand Down
11 changes: 9 additions & 2 deletions test/screenshots_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'dart:convert';
import 'dart:io';

import 'package:process/process.dart';
import 'package:screenshots/src/archive.dart';
import 'package:screenshots/screenshots.dart';
import 'package:screenshots/src/config.dart';
import 'package:screenshots/src/daemon_client.dart';
import 'package:screenshots/src/globals.dart';
Expand All @@ -16,6 +16,7 @@ import 'package:yaml/yaml.dart';
import 'package:screenshots/src/fastlane.dart' as fastlane;
import 'package:path/path.dart' as p;

import '../bin/main.dart';
import 'common.dart';

void main() {
Expand Down Expand Up @@ -633,7 +634,6 @@ devices:
});

group('archiving', () {

test('run with archiving enabled', () async {
final origDir = Directory.current;
Directory.current = 'example';
Expand All @@ -659,4 +659,11 @@ devices:
run.cmd('git', ['checkout', dirPath]);
});
});

group('adb path', () {
test('find adb path', () async {
final _adbPath = getAdbPath();
print('adbPath=$_adbPath');
});
});
}

0 comments on commit bf10e98

Please sign in to comment.