mirror of
https://github.com/flutter/flutter.git
synced 2025-06-03 00:51:18 +00:00

This adds a keycode generator that incorporates input from the Chromium and Android source trees, as well as some local tables, to generate static constants for the LogicalKeyboardKey and PhysicalKeyboardKey classes, as well as mappings from each of the platforms we support so far (currently only Android and Fuchsia). This code generator parses the input files, generates an intermediate data structure (`key_data.json`) that is checked in, and then generates the Dart sources for these classes and some static maps that will also be checked in (but are not included in this PR). The idea is that these codes don't change often, and so we don't need to generate them on every build, but we would like to be able to update them easily in the future if new data becomes available. If the existing data disappears or becomes unusable, we can maintain the checked-in data structure by hand if necessary, and still be able to generate the code. This PR only contains the code generator, not the classes themselves. In another follow-on PR, I'll run the generator and check in the output of the generator.
112 lines
2.3 KiB
Dart
112 lines
2.3 KiB
Dart
// Copyright 2019 The Chromium 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:io' hide Platform;
|
|
|
|
import 'package:path/path.dart' as path;
|
|
import 'package:platform/platform.dart' show LocalPlatform;
|
|
|
|
/// The location of the Flutter root directory, based on the known location of
|
|
/// this script.
|
|
final Directory flutterRoot = Directory(path.dirname(const LocalPlatform().script.toFilePath())).parent.parent.parent.parent;
|
|
|
|
/// Converts `FOO_BAR` to `fooBar`.
|
|
String shoutingToLowerCamel(String shouting) {
|
|
final RegExp initialLetter = RegExp(r'_([^_])([^_]*)');
|
|
final String snake = shouting.toLowerCase();
|
|
final String result = snake.replaceAllMapped(initialLetter, (Match match) {
|
|
return match.group(1).toUpperCase() + match.group(2).toLowerCase();
|
|
});
|
|
return result;
|
|
}
|
|
|
|
/// Converts 'FooBar' to 'fooBar'.
|
|
String upperCamelToLowerCamel(String upperCamel) {
|
|
return upperCamel.substring(0, 1).toLowerCase() + upperCamel.substring(1);
|
|
}
|
|
|
|
/// Converts 'fooBar' to 'FooBar'.
|
|
String lowerCamelToUpperCamel(String lowerCamel) {
|
|
return lowerCamel.substring(0, 1).toUpperCase() + lowerCamel.substring(1);
|
|
}
|
|
|
|
/// A list of Dart reserved words.
|
|
///
|
|
/// Since these are Dart reserved words, we can't use them as-is for enum names.
|
|
const List<String> kDartReservedWords = <String>[
|
|
'abstract',
|
|
'as',
|
|
'assert',
|
|
'async',
|
|
'await',
|
|
'break',
|
|
'case',
|
|
'catch',
|
|
'class',
|
|
'const',
|
|
'continue',
|
|
'covariant',
|
|
'default',
|
|
'deferred',
|
|
'do',
|
|
'dynamic',
|
|
'else',
|
|
'enum',
|
|
'export',
|
|
'extends',
|
|
'external',
|
|
'factory',
|
|
'false',
|
|
'final',
|
|
'finally',
|
|
'for',
|
|
'Function',
|
|
'get',
|
|
'hide',
|
|
'if',
|
|
'implements',
|
|
'import',
|
|
'in',
|
|
'interface',
|
|
'is',
|
|
'library',
|
|
'mixin',
|
|
'new',
|
|
'null',
|
|
'on',
|
|
'operator',
|
|
'part',
|
|
'rethrow',
|
|
'return',
|
|
'set',
|
|
'show',
|
|
'static',
|
|
'super',
|
|
'switch',
|
|
'sync',
|
|
'this',
|
|
'throw',
|
|
'true',
|
|
'try',
|
|
'typedef',
|
|
'var',
|
|
'void',
|
|
'while',
|
|
'with',
|
|
'yield',
|
|
];
|
|
|
|
/// Converts an integer into a hex string with the given number of digits.
|
|
String toHex(int value, {int digits = 8}) {
|
|
if (value == null) {
|
|
return 'null';
|
|
}
|
|
return '0x${value.toRadixString(16).padLeft(digits, '0')}';
|
|
}
|
|
|
|
/// Parses an integer from a hex string.
|
|
int getHex(String input) {
|
|
return int.parse(input, radix: 16);
|
|
}
|