diff --git a/bin/main.dart b/bin/main.dart index ea26ede6..3641324c 100644 --- a/bin/main.dart +++ b/bin/main.dart @@ -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 ] [-m ]'; @@ -34,6 +35,12 @@ void main(List arguments) async { _handleError(argParser, e.toString()); } + // show help + if (argResults[helpArg]) { + _showUsage(argParser); + exit(0); + } + // confirm os switch (Platform.operatingSystem) { case 'windows': @@ -64,30 +71,11 @@ void main(List 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]}"); } @@ -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; +} diff --git a/test/screenshots_test.dart b/test/screenshots_test.dart index e38f29f3..112a9cdd 100644 --- a/test/screenshots_test.dart +++ b/test/screenshots_test.dart @@ -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'; @@ -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() { @@ -633,7 +634,6 @@ devices: }); group('archiving', () { - test('run with archiving enabled', () async { final origDir = Directory.current; Directory.current = 'example'; @@ -659,4 +659,11 @@ devices: run.cmd('git', ['checkout', dirPath]); }); }); + + group('adb path', () { + test('find adb path', () async { + final _adbPath = getAdbPath(); + print('adbPath=$_adbPath'); + }); + }); }