Skip to content

Commit

Permalink
Merge pull request #2253 from leancodepl/use-java-from-flutter-doctor
Browse files Browse the repository at this point in the history
Use java from flutter doctor
  • Loading branch information
pdenert authored Jul 11, 2024
2 parents 66240b2 + 90c87fe commit f104929
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 22 deletions.
4 changes: 4 additions & 0 deletions packages/patrol_cli/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 3.0.0

- **Breaking:** Use `java` version from `flutter doctor`

## 2.8.1

- Fix parsing non string values from --dart-define-from-file (#2243).
Expand Down
49 changes: 48 additions & 1 deletion packages/patrol_cli/lib/src/android/android_test_backend.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:async';
import 'dart:io' show Process;

import 'package:adb/adb.dart';
Expand Down Expand Up @@ -37,8 +38,11 @@ class AndroidTestBackend {
final FileSystem _fs;
final DisposeScope _disposeScope;
final Logger _logger;
late final String javaPath;

Future<void> build(AndroidAppOptions options) async {
await loadJavaPathFromFlutterDoctor(options.flutter.command.executable);

await _disposeScope.run((scope) async {
final subject = options.description;
final task = _logger.task('Building $subject');
Expand All @@ -52,6 +56,9 @@ class AndroidTestBackend {
options.toGradleAssembleInvocation(isWindows: _platform.isWindows),
runInShell: true,
workingDirectory: _fs.currentDirectory.childDirectory('android').path,
environment: {
'JAVA_HOME': javaPath,
},
)
..disposedBy(scope);
process.listenStdOut((l) => _logger.detail('\t: $l')).disposedBy(scope);
Expand All @@ -73,6 +80,9 @@ class AndroidTestBackend {
options.toGradleAssembleTestInvocation(isWindows: _platform.isWindows),
runInShell: true,
workingDirectory: _fs.currentDirectory.childDirectory('android').path,
environment: {
'JAVA_HOME': javaPath,
},
)
..disposedBy(scope);
process.listenStdOut((l) => _logger.detail('\t: $l')).disposedBy(scope);
Expand All @@ -92,6 +102,40 @@ class AndroidTestBackend {
});
}

Future<void> loadJavaPathFromFlutterDoctor(String commandExecutable) async {
final javaCompleterPath = Completer<String?>();

await _disposeScope.run((scope) async {
final process = await _processManager.start(
[
commandExecutable,
'doctor',
'--verbose',
],
runInShell: true,
)
..disposedBy(scope);

process.listenStdOut(
(line) async {
if (line.contains('• Java binary at:') &&
javaCompleterPath.isCompleted == false) {
final path = line
.replaceAll('• Java binary at:', '')
.replaceAll(
_platform.isWindows ? r'\bin\java' : '/bin/java',
'',
)
.trim();
javaCompleterPath.complete(path);
}
},
).disposedBy(scope);
});

javaPath = await javaCompleterPath.future ?? '';
}

/// Executes the tests of the given [options] on the given [device].
///
/// [build] must be called before this method.
Expand All @@ -110,7 +154,10 @@ class AndroidTestBackend {
final process = await _processManager.start(
options.toGradleConnectedTestInvocation(isWindows: _platform.isWindows),
runInShell: true,
environment: {'ANDROID_SERIAL': device.id},
environment: {
'ANDROID_SERIAL': device.id,
'JAVA_HOME': javaPath,
},
workingDirectory: _fs.currentDirectory.childDirectory('android').path,
)
..disposedBy(scope);
Expand Down
2 changes: 1 addition & 1 deletion packages/patrol_cli/lib/src/base/constants.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
/// Version of Patrol CLI. Must be kept in sync with pubspec.yaml.
/// If you update this, make sure that compatibility-table.mdx is updated (if needed)
const version = '2.8.1';
const version = '3.0.0';
9 changes: 5 additions & 4 deletions packages/patrol_cli/lib/src/commands/develop.dart
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,6 @@ class DevelopCommand extends PatrolCommand {
),
);

await _compatibilityChecker.checkVersionsCompatibility(
flutterCommand: flutterCommand,
);

final targets = stringsArg('target');
if (targets.isEmpty) {
throwToolExit('No target provided with --target');
Expand Down Expand Up @@ -134,6 +130,11 @@ class DevelopCommand extends PatrolCommand {
);
final device = devices.single;

await _compatibilityChecker.checkVersionsCompatibility(
flutterCommand: flutterCommand,
targetPlatform: device.targetPlatform,
);

// `flutter logs` doesn't work on macOS, so we don't support it for now
// https://github.com/leancodepl/patrol/issues/1974
if (device.targetPlatform == TargetPlatform.macOS) {
Expand Down
9 changes: 5 additions & 4 deletions packages/patrol_cli/lib/src/commands/test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,6 @@ class TestCommand extends PatrolCommand {
),
);

await _compatibilityChecker.checkVersionsCompatibility(
flutterCommand: flutterCommand,
);

final config = _pubspecReader.read();
final testFileSuffix = config.testFileSuffix;

Expand Down Expand Up @@ -141,6 +137,11 @@ See https://github.com/leancodepl/patrol/issues/1316 to learn more.

final device = devices.single;

await _compatibilityChecker.checkVersionsCompatibility(
flutterCommand: flutterCommand,
targetPlatform: device.targetPlatform,
);

final packageName = stringArg('package-name') ?? config.android.packageName;
final bundleId = stringArg('bundle-id') ?? config.ios.bundleId;
final macosBundleId = stringArg('bundle-id') ?? config.macos.bundleId;
Expand Down
30 changes: 19 additions & 11 deletions packages/patrol_cli/lib/src/compatibility_checker.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import 'dart:async';
import 'dart:io' as io;

import 'package:dispose_scope/dispose_scope.dart';
import 'package:file/file.dart';
import 'package:patrol_cli/src/base/constants.dart' as constants;
import 'package:patrol_cli/src/base/exceptions.dart';
import 'package:patrol_cli/src/base/logger.dart';
import 'package:patrol_cli/src/base/process.dart';
import 'package:patrol_cli/src/devices.dart';
import 'package:patrol_cli/src/runner/flutter_command.dart';
import 'package:process/process.dart';
import 'package:version/version.dart';
Expand All @@ -28,10 +28,12 @@ class CompatibilityChecker {

Future<void> checkVersionsCompatibility({
required FlutterCommand flutterCommand,
required TargetPlatform targetPlatform,
}) async {
if (io.Platform.isAndroid) {
if (targetPlatform == TargetPlatform.android) {
await _checkJavaVersion(
_disposeScope,
flutterCommand.executable,
DisposeScope(),
_processManager,
_projectRoot,
_logger,
Expand Down Expand Up @@ -91,30 +93,36 @@ class CompatibilityChecker {
}

Future<void> _checkJavaVersion(
String flutterExecutable,
DisposeScope disposeScope,
ProcessManager processManager,
Directory projectRoot,
Logger logger,
) async {
Version? javaVersion;
final javaCompleter = Completer<Version?>();
final javaCompleterVersion = Completer<Version?>();

await disposeScope.run((scope) async {
final process = await processManager.start(
['javac', '--version'],
[flutterExecutable, 'doctor', '--verbose'],
workingDirectory: projectRoot.path,
runInShell: true,
)
..disposedBy(scope);

process.listenStdOut((line) async {
if (line.startsWith('javac')) {
javaCompleter.complete(Version.parse(line.split(' ').last));
}
}).disposedBy(scope);
process.listenStdOut(
(line) async {
if (line.contains('• Java version') &&
javaCompleterVersion.isCompleted == false) {
final versionString = line.split(' ').last.replaceAll(')', '');
javaCompleterVersion.complete(Version.parse(versionString));
}
},
).disposedBy(scope);
});

javaVersion = await javaCompleter.future;
javaVersion = await javaCompleterVersion.future;

if (javaVersion == null) {
throwToolExit(
'Failed to read Java version. Make sure you have Java installed and added to PATH',
Expand Down
2 changes: 1 addition & 1 deletion packages/patrol_cli/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: patrol_cli
description: >
Command-line tool for Patrol, a powerful Flutter-native UI testing framework.
version: 2.8.1 # Must be kept in sync with constants.dart
version: 3.0.0 # Must be kept in sync with constants.dart
homepage: https://patrol.leancode.co
repository: https://github.com/leancodepl/patrol/tree/master/packages/patrol_cli
issue_tracker: https://github.com/leancodepl/patrol/issues?q=is%3Aopen+is%3Aissue+label%3A%22package%3A+patrol_cli%22
Expand Down

0 comments on commit f104929

Please sign in to comment.