Update the tool to know about all our new platforms (#132423)
...and add a test so we remember to keep it in sync.
This commit is contained in:
parent
5fa8de05ee
commit
d6bf1447f4
@ -87,6 +87,9 @@ Future<void> run(List<String> arguments) async {
|
||||
foundError(<String>['The analyze.dart script must be run with --enable-asserts.']);
|
||||
}
|
||||
|
||||
printProgress('TargetPlatform tool/framework consistency');
|
||||
await verifyTargetPlatform(flutterRoot);
|
||||
|
||||
printProgress('No Double.clamp');
|
||||
await verifyNoDoubleClamp(flutterRoot);
|
||||
|
||||
@ -266,6 +269,84 @@ class _DoubleClampVisitor extends RecursiveAstVisitor<CompilationUnit> {
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> verifyTargetPlatform(String workingDirectory) async {
|
||||
final File framework = File('$workingDirectory/packages/flutter/lib/src/foundation/platform.dart');
|
||||
final Set<String> frameworkPlatforms = <String>{};
|
||||
List<String> lines = framework.readAsLinesSync();
|
||||
int index = 0;
|
||||
while (true) {
|
||||
if (index >= lines.length) {
|
||||
foundError(<String>['${framework.path}: Can no longer find TargetPlatform enum.']);
|
||||
return;
|
||||
}
|
||||
if (lines[index].startsWith('enum TargetPlatform {')) {
|
||||
index += 1;
|
||||
break;
|
||||
}
|
||||
index += 1;
|
||||
}
|
||||
while (true) {
|
||||
if (index >= lines.length) {
|
||||
foundError(<String>['${framework.path}: Could not find end of TargetPlatform enum.']);
|
||||
return;
|
||||
}
|
||||
String line = lines[index].trim();
|
||||
final int comment = line.indexOf('//');
|
||||
if (comment >= 0) {
|
||||
line = line.substring(0, comment);
|
||||
}
|
||||
if (line == '}') {
|
||||
break;
|
||||
}
|
||||
if (line.isNotEmpty) {
|
||||
if (line.endsWith(',')) {
|
||||
frameworkPlatforms.add(line.substring(0, line.length - 1));
|
||||
} else {
|
||||
foundError(<String>['${framework.path}:$index: unparseable line when looking for TargetPlatform values']);
|
||||
}
|
||||
}
|
||||
index += 1;
|
||||
}
|
||||
final File tool = File('$workingDirectory/packages/flutter_tools/lib/src/resident_runner.dart');
|
||||
final Set<String> toolPlatforms = <String>{};
|
||||
lines = tool.readAsLinesSync();
|
||||
index = 0;
|
||||
while (true) {
|
||||
if (index >= lines.length) {
|
||||
foundError(<String>['${tool.path}: Can no longer find nextPlatform logic.']);
|
||||
return;
|
||||
}
|
||||
if (lines[index].trim().startsWith('const List<String> platforms = <String>[')) {
|
||||
index += 1;
|
||||
break;
|
||||
}
|
||||
index += 1;
|
||||
}
|
||||
while (true) {
|
||||
if (index >= lines.length) {
|
||||
foundError(<String>['${tool.path}: Could not find end of nextPlatform logic.']);
|
||||
return;
|
||||
}
|
||||
final String line = lines[index].trim();
|
||||
if (line.startsWith("'") && line.endsWith("',")) {
|
||||
toolPlatforms.add(line.substring(1, line.length - 2));
|
||||
} else if (line == '];') {
|
||||
break;
|
||||
} else {
|
||||
foundError(<String>['${tool.path}:$index: unparseable line when looking for nextPlatform values']);
|
||||
}
|
||||
index += 1;
|
||||
}
|
||||
final Set<String> frameworkExtra = frameworkPlatforms.difference(toolPlatforms);
|
||||
if (frameworkExtra.isNotEmpty) {
|
||||
foundError(<String>['TargetPlatform has some extra values not found in the tool: ${frameworkExtra.join(", ")}']);
|
||||
}
|
||||
final Set<String> toolExtra = toolPlatforms.difference(frameworkPlatforms);
|
||||
if (toolExtra.isNotEmpty) {
|
||||
foundError(<String>['The nextPlatform logic in the tool has some extra values not found in TargetPlatform: ${toolExtra.join(", ")}']);
|
||||
}
|
||||
}
|
||||
|
||||
/// Verify that we use clampDouble instead of Double.clamp for performance reasons.
|
||||
///
|
||||
/// We currently can't distinguish valid uses of clamp from problematic ones so
|
||||
|
@ -561,33 +561,22 @@ abstract class BindingBase {
|
||||
name: FoundationServiceExtensions.platformOverride.name,
|
||||
callback: (Map<String, String> parameters) async {
|
||||
if (parameters.containsKey('value')) {
|
||||
switch (parameters['value']) {
|
||||
case 'android':
|
||||
debugDefaultTargetPlatformOverride = TargetPlatform.android;
|
||||
case 'fuchsia':
|
||||
debugDefaultTargetPlatformOverride = TargetPlatform.fuchsia;
|
||||
case 'iOS':
|
||||
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
|
||||
case 'linux':
|
||||
debugDefaultTargetPlatformOverride = TargetPlatform.linux;
|
||||
case 'macOS':
|
||||
debugDefaultTargetPlatformOverride = TargetPlatform.macOS;
|
||||
case 'windows':
|
||||
debugDefaultTargetPlatformOverride = TargetPlatform.windows;
|
||||
case 'default':
|
||||
default:
|
||||
debugDefaultTargetPlatformOverride = null;
|
||||
final String value = parameters['value']!;
|
||||
debugDefaultTargetPlatformOverride = null;
|
||||
for (final TargetPlatform candidate in TargetPlatform.values) {
|
||||
if (candidate.name == value) {
|
||||
debugDefaultTargetPlatformOverride = candidate;
|
||||
break;
|
||||
}
|
||||
}
|
||||
_postExtensionStateChangedEvent(
|
||||
FoundationServiceExtensions.platformOverride.name,
|
||||
defaultTargetPlatform.toString().substring('$TargetPlatform.'.length),
|
||||
defaultTargetPlatform.name,
|
||||
);
|
||||
await reassembleApplication();
|
||||
}
|
||||
return <String, dynamic>{
|
||||
'value': defaultTargetPlatform
|
||||
.toString()
|
||||
.substring('$TargetPlatform.'.length),
|
||||
'value': defaultTargetPlatform.name,
|
||||
};
|
||||
},
|
||||
);
|
||||
|
@ -35,7 +35,7 @@ import '_platform_io.dart'
|
||||
//
|
||||
// When adding support for a new platform (e.g. Windows Phone, Raspberry Pi),
|
||||
// first create a new value on the [TargetPlatform] enum, then add a rule for
|
||||
// selecting that platform here.
|
||||
// selecting that platform in `_platform_io.dart` and `_platform_web.dart`.
|
||||
//
|
||||
// It would be incorrect to make a platform that isn't supported by
|
||||
// [TargetPlatform] default to the behavior of another platform, because doing
|
||||
@ -47,6 +47,15 @@ TargetPlatform get defaultTargetPlatform => platform.defaultTargetPlatform;
|
||||
/// The platform that user interaction should adapt to target.
|
||||
///
|
||||
/// The [defaultTargetPlatform] getter returns the current platform.
|
||||
///
|
||||
/// When using the "flutter run" command, the "o" key will toggle between
|
||||
/// values of this enum when updating [debugDefaultTargetPlatformOverride].
|
||||
/// This lets one test how the application will work on various platforms
|
||||
/// without having to switch emulators or physical devices.
|
||||
//
|
||||
// When you add values here, make sure to also add them to
|
||||
// nextPlatform() in flutter_tools/lib/src/resident_runner.dart so that
|
||||
// the tool can support the new platform for its "o" option.
|
||||
enum TargetPlatform {
|
||||
/// Android: <https://www.android.com/>
|
||||
android,
|
||||
|
@ -1877,19 +1877,17 @@ class DebugConnectionInfo {
|
||||
/// These values must match what is available in
|
||||
/// `packages/flutter/lib/src/foundation/binding.dart`.
|
||||
String nextPlatform(String currentPlatform) {
|
||||
switch (currentPlatform) {
|
||||
case 'android':
|
||||
return 'iOS';
|
||||
case 'iOS':
|
||||
return 'fuchsia';
|
||||
case 'fuchsia':
|
||||
return 'macOS';
|
||||
case 'macOS':
|
||||
return 'android';
|
||||
default:
|
||||
assert(false); // Invalid current platform.
|
||||
return 'android';
|
||||
}
|
||||
const List<String> platforms = <String>[
|
||||
'android',
|
||||
'iOS',
|
||||
'windows',
|
||||
'macOS',
|
||||
'linux',
|
||||
'fuchsia',
|
||||
];
|
||||
final int index = platforms.indexOf(currentPlatform);
|
||||
assert(index >= 0, 'unknown platform "$currentPlatform"');
|
||||
return platforms[(index + 1) % platforms.length];
|
||||
}
|
||||
|
||||
/// A launcher for the devtools debugger and analysis tool.
|
||||
|
@ -2210,9 +2210,11 @@ flutter:
|
||||
|
||||
testUsingContext('nextPlatform moves through expected platforms', () {
|
||||
expect(nextPlatform('android'), 'iOS');
|
||||
expect(nextPlatform('iOS'), 'fuchsia');
|
||||
expect(nextPlatform('fuchsia'), 'macOS');
|
||||
expect(nextPlatform('macOS'), 'android');
|
||||
expect(nextPlatform('iOS'), 'windows');
|
||||
expect(nextPlatform('windows'), 'macOS');
|
||||
expect(nextPlatform('macOS'), 'linux');
|
||||
expect(nextPlatform('linux'), 'fuchsia');
|
||||
expect(nextPlatform('fuchsia'), 'android');
|
||||
expect(() => nextPlatform('unknown'), throwsAssertionError);
|
||||
});
|
||||
|
||||
|
@ -464,10 +464,10 @@ void main() {
|
||||
method: 'ext.flutter.platformOverride',
|
||||
args: <String, Object>{
|
||||
'isolateId': '1',
|
||||
'value': 'fuchsia',
|
||||
'value': 'windows',
|
||||
},
|
||||
jsonResponse: <String, Object>{
|
||||
'value': 'fuchsia',
|
||||
'value': 'windows',
|
||||
},
|
||||
),
|
||||
// Request 2.
|
||||
@ -496,7 +496,7 @@ void main() {
|
||||
await terminalHandler.processTerminalInput('o');
|
||||
await terminalHandler.processTerminalInput('O');
|
||||
|
||||
expect(terminalHandler.logger.statusText, contains('Switched operating system to fuchsia'));
|
||||
expect(terminalHandler.logger.statusText, contains('Switched operating system to windows'));
|
||||
expect(terminalHandler.logger.statusText, contains('Switched operating system to iOS'));
|
||||
});
|
||||
|
||||
@ -518,10 +518,10 @@ void main() {
|
||||
method: 'ext.flutter.platformOverride',
|
||||
args: <String, Object>{
|
||||
'isolateId': '1',
|
||||
'value': 'fuchsia',
|
||||
'value': 'windows',
|
||||
},
|
||||
jsonResponse: <String, Object>{
|
||||
'value': 'fuchsia',
|
||||
'value': 'windows',
|
||||
},
|
||||
),
|
||||
// Request 2.
|
||||
@ -550,7 +550,7 @@ void main() {
|
||||
await terminalHandler.processTerminalInput('o');
|
||||
await terminalHandler.processTerminalInput('O');
|
||||
|
||||
expect(terminalHandler.logger.statusText, contains('Switched operating system to fuchsia'));
|
||||
expect(terminalHandler.logger.statusText, contains('Switched operating system to windows'));
|
||||
expect(terminalHandler.logger.statusText, contains('Switched operating system to iOS'));
|
||||
});
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user