Hardware keyboard: codegen (#73440)

Rewrites tools/gen_keycodes, the script that generates key mappings across the framework and the engine.
This commit is contained in:
Tong Mu
2021-04-30 21:06:03 -07:00
committed by GitHub
parent 0cb0767bd7
commit 9956a35a5a
65 changed files with 23391 additions and 14836 deletions

View File

@@ -3,15 +3,14 @@
// found in the LICENSE file.
import 'dart:io';
import 'package:path/path.dart' as path;
import 'package:gen_keycodes/logical_key_data.dart';
import 'key_data.dart';
import 'utils.dart';
import 'physical_key_data.dart';
String _injectDictionary(String template, Map<String, String> dictionary) {
String result = template;
for (final String key in dictionary.keys) {
result = result.replaceAll('@@@$key@@@', dictionary[key]);
result = result.replaceAll('@@@$key@@@', dictionary[key] ?? '@@@$key@@@');
}
return result;
}
@@ -26,7 +25,7 @@ String _injectDictionary(String template, Map<String, String> dictionary) {
/// Subclasses must implement [templatePath] and [mappings].
abstract class BaseCodeGenerator {
/// Create a code generator while providing [keyData] to be used in [mappings].
BaseCodeGenerator(this.keyData);
BaseCodeGenerator(this.keyData, this.logicalData);
/// Absolute path to the template file that this file is generated on.
String get templatePath;
@@ -42,30 +41,20 @@ abstract class BaseCodeGenerator {
}
/// The database of keys loaded from disk.
final KeyData keyData;
final PhysicalKeyData keyData;
final LogicalKeyData logicalData;
}
/// A code generator which also defines platform-based behavior.
abstract class PlatformCodeGenerator extends BaseCodeGenerator {
PlatformCodeGenerator(KeyData keyData) : super(keyData);
// Used by platform code generators.
List<Key> get numpadKeyData {
return keyData.data.where((Key entry) {
return entry.constantName.startsWith('numpad') && entry.keyLabel != null;
}).toList();
}
// Used by platform code generators.
List<Key> get functionKeyData {
final RegExp functionKeyRe = RegExp(r'^f[0-9]+$');
return keyData.data.where((Key entry) {
return functionKeyRe.hasMatch(entry.constantName);
}).toList();
}
PlatformCodeGenerator(PhysicalKeyData keyData, LogicalKeyData logicalData)
: super(keyData, logicalData);
/// Absolute path to the output file.
///
/// How this value will be used is based on the callee.
String outputPath(String platform) => path.join(flutterRoot.path, '..', path.join('engine', 'src', 'flutter', 'shell', 'platform', platform, 'keycodes', 'keyboard_map_$platform.h'));
String outputPath(String platform);
static String engineRoot = '';
}