From bacc03daf961ce794fc2d5750010839fada592eb Mon Sep 17 00:00:00 2001 From: Pierre-Louis <6655696+guidezpl@users.noreply.github.com> Date: Mon, 26 Oct 2020 16:35:58 +0100 Subject: [PATCH] Refactor update_icons (#68708) * refactor update_icons * fix trailing space * address feedback * Rename to _iconsMirroredWhenRTL --- dev/tools/pubspec.yaml | 2 +- dev/tools/update_icons.dart | 124 ++++++++++++++++++++++-------------- 2 files changed, 78 insertions(+), 48 deletions(-) diff --git a/dev/tools/pubspec.yaml b/dev/tools/pubspec.yaml index 3531256967..67245fab54 100644 --- a/dev/tools/pubspec.yaml +++ b/dev/tools/pubspec.yaml @@ -3,7 +3,7 @@ description: Various repository development tools for flutter. environment: # The pub client defaults to an <2.0.0 sdk constraint which we need to explicitly overwrite. - sdk: ">=2.2.2 <3.0.0" + sdk: ">=2.6.0 <3.0.0" dependencies: archive: 2.0.13 diff --git a/dev/tools/update_icons.dart b/dev/tools/update_icons.dart index 2377043715..41a5c4c7bd 100644 --- a/dev/tools/update_icons.dart +++ b/dev/tools/update_icons.dart @@ -74,7 +74,7 @@ const Map _identifierRewrites = { 'class': 'class_', }; -const Set _mirroredIcons = { +const Set _iconsMirroredWhenRTL = { // This list is obtained from: // http://google.github.io/material-design-icons/#icons-in-rtl 'arrow_back', @@ -197,10 +197,16 @@ void main(List args) { ArgResults _handleArguments(List args) { final ArgParser argParser = ArgParser() - ..addOption(_newCodepointsPathOption, defaultsTo: _defaultNewCodepointsPath) - ..addOption(_oldCodepointsPathOption, defaultsTo: _defaultOldCodepointsPath) - ..addOption(_iconsClassPathOption, defaultsTo: _defaultIconsPath) + ..addOption(_newCodepointsPathOption, defaultsTo: _defaultNewCodepointsPath, help: 'Location of the new codepoints directory') + ..addOption(_oldCodepointsPathOption, defaultsTo: _defaultOldCodepointsPath, help: 'Location of the existing codepoints directory') + ..addOption(_iconsClassPathOption, defaultsTo: _defaultIconsPath, help: 'Location of the material icons file') ..addFlag(_dryRunOption, defaultsTo: false); + argParser.addFlag('help', abbr: 'h', negatable: false, callback: (bool help) { + if (help) { + print(argParser.usage); + exit(1); + } + }); return argParser.parse(args); } @@ -236,7 +242,7 @@ String regenerateIconsFile(String iconData, Map tokenPairMap) { final String iconDeclarationsString = [ for (MapEntry entry in tokenPairMap.entries) - _generateDeclaration(entry) + _Icon(entry).fullDeclaration ].join(); buf.write(iconDeclarationsString); @@ -262,52 +268,76 @@ Error: New codepoints file does not contain all the existing codepoints.\n } } -String _generateDeclaration(MapEntry tokenPair) { - final String description = tokenPair.key.replaceAll('_', ' '); - - String styleSuffix = ''; - String webFontKey = tokenPair.key; - - // The first line of each generated declaration includes a comment of html. - // DartDocs reads that to make the listings in our api docs that shows the - // icon rendered next to its key name. Unfortunately, unlike Flutter, this - // html needs to use a different web font for each style. We read the style's - // suffix from the key for Flutter's icons font, add the corresponding style's - // suffix to the class we pass into html, and then remove the suffix from the - // icon key. The keys needed for the individual web fonts do not use a suffix - // to denote style. - if (webFontKey.endsWith('_outlined') && webFontKey!='insert_chart_outlined') { - styleSuffix = '-outlined'; - webFontKey = webFontKey.replaceAll('_outlined', ''); - } - if (webFontKey.endsWith('_rounded')) { - styleSuffix = '-round'; - webFontKey = webFontKey.replaceAll('_rounded', ''); - } - if (webFontKey.endsWith('_sharp')) { - styleSuffix = '-sharp'; - webFontKey = webFontKey.replaceAll('_sharp', ''); - } - - final String identifier = _generateIdentifier(tokenPair.key); - final String rtl = _mirroredIcons.contains(tokenPair.key) - ? ', matchTextDirection: true' - : ''; - - return ''' - - /// $webFontKey — material icon named "$description". - static const IconData $identifier = IconData(0x${tokenPair.value}, fontFamily: 'MaterialIcons'$rtl); -'''; +enum IconStyle { + regular, + outlined, + rounded, + sharp, } -String _generateIdentifier(String rawIdentifier) { - for (final MapEntry rewritePair in _identifierRewrites.entries) { - if (rawIdentifier.startsWith(rewritePair.key)) { - return rawIdentifier.replaceFirst(rewritePair.key, _identifierRewrites[rewritePair.key]); +extension IconStyleSuffix on IconStyle { + // The suffix for the 'material-icons' HTML class. + String suffix() { + switch (this) { + case IconStyle.outlined: return '-outlined'; + case IconStyle.rounded: return '-round'; + case IconStyle.sharp: return '-sharp'; + default: return ''; } } - return rawIdentifier; +} + +class _Icon { + // Parse tokenPair (e.g. {"6_ft_apart_outlined": "e004"}). + _Icon(MapEntry tokenPair) { + id = tokenPair.key; + hexCodepoint = tokenPair.value; + + if (id.endsWith('_outlined') && id!='insert_chart_outlined') { + style = IconStyle.outlined; + shortId = id.replaceAll('_outlined', ''); + } else if (id.endsWith('_rounded')) { + style = IconStyle.rounded; + shortId = id.replaceAll('_rounded', ''); + } else if (id.endsWith('_sharp')) { + style = IconStyle.sharp; + shortId = id.replaceAll('_sharp', ''); + } else { + style = IconStyle.regular; + shortId = id; + } + + flutterId = id; + for (final MapEntry rewritePair in _identifierRewrites.entries) { + if (id.startsWith(rewritePair.key)) { + flutterId = id.replaceFirst(rewritePair.key, _identifierRewrites[rewritePair.key]); + } + } + } + + // e.g. 5g, 5g_outlined, 5g_rounded, 5g_sharp + String id; + // e.g. 5g + String shortId; + // e.g. five_g + String flutterId; + // e.g. IconStyle.outlined + IconStyle style; + // e.g. e547 + String hexCodepoint; + + // TODO(guidezpl): will be fixed in a future PR to be shortId instead of id + String get mirroredInRTL => _iconsMirroredWhenRTL.contains(id) ? ', matchTextDirection: true' : ''; + + String get name => id.replaceAll('_', ' '); + + String get dartDoc => + '/// $shortId — material icon named "$name".'; + + String get declaration => + "static const IconData $flutterId = IconData(0x$hexCodepoint, fontFamily: 'MaterialIcons'$mirroredInRTL);"; + + String get fullDeclaration => '''\n $dartDoc\n $declaration\n'''; } // Replace the old codepoints file with the new.