This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
Add device selection to et run
#51184
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
void _appendTypeError( | ||
johnmccutchan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Map<String, Object?> map, | ||
String field, | ||
String expected, | ||
List<String> errors, { | ||
Object? element, | ||
}) { | ||
if (element == null) { | ||
final Type actual = map[field]!.runtimeType; | ||
errors.add( | ||
'For field "$field", expected type: $expected, actual type: $actual.', | ||
); | ||
} else { | ||
final Type actual = element.runtimeType; | ||
errors.add( | ||
'For element "$element" of "$field", ' | ||
'expected type: $expected, actual type: $actual', | ||
); | ||
} | ||
} | ||
|
||
/// Type safe getter of a List<String> field from map. | ||
List<String>? stringListOfJson( | ||
Map<String, Object?> map, | ||
String field, | ||
List<String> errors, | ||
) { | ||
if (map[field] == null) { | ||
return <String>[]; | ||
} | ||
if (map[field]! is! List<Object?>) { | ||
_appendTypeError(map, field, 'list', errors); | ||
return null; | ||
} | ||
for (final Object? obj in map[field]! as List<Object?>) { | ||
if (obj is! String) { | ||
_appendTypeError(map, field, element: obj, 'string', errors); | ||
return null; | ||
} | ||
} | ||
return (map[field]! as List<Object?>).cast<String>(); | ||
} | ||
|
||
/// Type safe getter of a String field from map. | ||
String? stringOfJson( | ||
Map<String, Object?> map, | ||
String field, | ||
List<String> errors, | ||
) { | ||
if (map[field] == null) { | ||
return '<undef>'; | ||
} | ||
if (map[field]! is! String) { | ||
_appendTypeError(map, field, 'string', errors); | ||
return null; | ||
} | ||
return map[field]! as String; | ||
} | ||
|
||
/// Type safe getter of an int field from map. | ||
int? intOfJson( | ||
Map<String, Object?> map, | ||
String field, | ||
List<String> errors, { | ||
int fallback = 0, | ||
}) { | ||
if (map[field] == null) { | ||
return fallback; | ||
} | ||
if (map[field]! is! int) { | ||
_appendTypeError(map, field, 'int', errors); | ||
return null; | ||
} | ||
return map[field]! as int; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import 'dart:convert'; | ||
|
||
import 'package:process_runner/process_runner.dart'; | ||
|
||
import 'environment.dart'; | ||
import 'json_utils.dart'; | ||
|
||
const String _targetPlatformKey = 'targetPlatform'; | ||
const String _nameKey = 'name'; | ||
const String _idKey = 'id'; | ||
|
||
/// Target to run a flutter application on. | ||
class RunTarget { | ||
/// Construct a RunTarget from a JSON map. | ||
factory RunTarget.fromJson(Map<String, Object> map) { | ||
final List<String> errors = <String>[]; | ||
final String name = stringOfJson(map, _nameKey, errors)!; | ||
final String id = stringOfJson(map, _idKey, errors)!; | ||
final String targetPlatform = | ||
stringOfJson(map, _targetPlatformKey, errors)!; | ||
|
||
if (errors.isNotEmpty) { | ||
throw FormatException('Failed to parse RunTarget: ${errors.join('\n')}'); | ||
} | ||
return RunTarget._(name, id, targetPlatform); | ||
} | ||
|
||
RunTarget._(this.name, this.id, this.targetPlatform); | ||
|
||
/// Name of target device. | ||
final String name; | ||
|
||
/// Id of target device. | ||
final String id; | ||
|
||
/// Target platform of device. | ||
final String targetPlatform; | ||
|
||
/// BuildConfig name for compilation mode. | ||
String buildConfigFor(String mode) { | ||
switch (targetPlatform) { | ||
case 'android-arm64': | ||
return 'android_${mode}_arm64'; | ||
case 'darwin': | ||
return 'host_$mode'; | ||
case 'web-javascript': | ||
return 'chrome_$mode'; | ||
default: | ||
throw UnimplementedError('No mapping for $targetPlatform'); | ||
} | ||
} | ||
} | ||
|
||
/// Parse the raw output of `flutter devices --machine`. | ||
List<RunTarget> parseDevices(Environment env, String flutterDevicesMachine) { | ||
late final List<dynamic> decoded; | ||
try { | ||
decoded = jsonDecode(flutterDevicesMachine) as List<dynamic>; | ||
} on FormatException catch (e) { | ||
env.logger.error( | ||
'Failed to parse flutter devices output: $e\n\n$flutterDevicesMachine\n\n'); | ||
return <RunTarget>[]; | ||
} | ||
|
||
final List<RunTarget> r = <RunTarget>[]; | ||
for (final dynamic device in decoded) { | ||
if (device is! Map<String, Object?>) { | ||
return <RunTarget>[]; | ||
} | ||
if (!device.containsKey(_nameKey) || !device.containsKey(_idKey)) { | ||
env.logger.error('device is missing required fields:\n$device\n'); | ||
return <RunTarget>[]; | ||
} | ||
if (!device.containsKey(_targetPlatformKey)) { | ||
env.logger.warning('Skipping ${device[_nameKey]}: ' | ||
'Could not find $_targetPlatformKey in device description.'); | ||
continue; | ||
} | ||
late final RunTarget target; | ||
try { | ||
target = RunTarget.fromJson(device.cast<String, Object>()); | ||
} on FormatException catch (e) { | ||
env.logger.error(e); | ||
return <RunTarget>[]; | ||
} | ||
r.add(target); | ||
} | ||
|
||
return r; | ||
} | ||
|
||
/// Return the default device to be used. | ||
RunTarget? defaultDevice(Environment env, List<RunTarget> targets) { | ||
if (targets.isEmpty) { | ||
return null; | ||
} | ||
return targets.first; | ||
} | ||
|
||
/// Select a run target. | ||
RunTarget? selectRunTarget(Environment env, String flutterDevicesMachine, | ||
[String? idPrefix]) { | ||
final List<RunTarget> targets = parseDevices(env, flutterDevicesMachine); | ||
if (idPrefix != null && idPrefix.isNotEmpty) { | ||
for (final RunTarget target in targets) { | ||
if (target.id.startsWith(idPrefix)) { | ||
return target; | ||
} | ||
} | ||
} | ||
return defaultDevice(env, targets); | ||
} | ||
|
||
/// Detects available targets and then selects one. | ||
Future<RunTarget?> detectAndSelectRunTarget(Environment env, | ||
[String? idPrefix]) async { | ||
final ProcessRunnerResult result = await env.processRunner | ||
.runProcess(<String>['flutter', 'devices', '--machine']); | ||
johnmccutchan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (result.exitCode != 0) { | ||
env.logger.error('flutter devices --machine failed:\n' | ||
'EXIT_CODE:${result.exitCode}\n' | ||
'STDOUT:\n${result.stdout}' | ||
'STDERR:\n${result.stderr}'); | ||
return null; | ||
} | ||
return selectRunTarget(env, result.stdout, idPrefix); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.