Unverified Commit bacc03da authored by Pierre-Louis's avatar Pierre-Louis Committed by GitHub

Refactor update_icons (#68708)

* refactor update_icons

* fix trailing space

* address feedback

* Rename to _iconsMirroredWhenRTL
parent b9195d80
...@@ -3,7 +3,7 @@ description: Various repository development tools for flutter. ...@@ -3,7 +3,7 @@ description: Various repository development tools for flutter.
environment: environment:
# The pub client defaults to an <2.0.0 sdk constraint which we need to explicitly overwrite. # 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: dependencies:
archive: 2.0.13 archive: 2.0.13
......
...@@ -74,7 +74,7 @@ const Map<String, String> _identifierRewrites = <String, String>{ ...@@ -74,7 +74,7 @@ const Map<String, String> _identifierRewrites = <String, String>{
'class': 'class_', 'class': 'class_',
}; };
const Set<String> _mirroredIcons = <String>{ const Set<String> _iconsMirroredWhenRTL = <String>{
// This list is obtained from: // This list is obtained from:
// http://google.github.io/material-design-icons/#icons-in-rtl // http://google.github.io/material-design-icons/#icons-in-rtl
'arrow_back', 'arrow_back',
...@@ -197,10 +197,16 @@ void main(List<String> args) { ...@@ -197,10 +197,16 @@ void main(List<String> args) {
ArgResults _handleArguments(List<String> args) { ArgResults _handleArguments(List<String> args) {
final ArgParser argParser = ArgParser() final ArgParser argParser = ArgParser()
..addOption(_newCodepointsPathOption, defaultsTo: _defaultNewCodepointsPath) ..addOption(_newCodepointsPathOption, defaultsTo: _defaultNewCodepointsPath, help: 'Location of the new codepoints directory')
..addOption(_oldCodepointsPathOption, defaultsTo: _defaultOldCodepointsPath) ..addOption(_oldCodepointsPathOption, defaultsTo: _defaultOldCodepointsPath, help: 'Location of the existing codepoints directory')
..addOption(_iconsClassPathOption, defaultsTo: _defaultIconsPath) ..addOption(_iconsClassPathOption, defaultsTo: _defaultIconsPath, help: 'Location of the material icons file')
..addFlag(_dryRunOption, defaultsTo: false); ..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); return argParser.parse(args);
} }
...@@ -236,7 +242,7 @@ String regenerateIconsFile(String iconData, Map<String, String> tokenPairMap) { ...@@ -236,7 +242,7 @@ String regenerateIconsFile(String iconData, Map<String, String> tokenPairMap) {
final String iconDeclarationsString = <String>[ final String iconDeclarationsString = <String>[
for (MapEntry<String, String> entry in tokenPairMap.entries) for (MapEntry<String, String> entry in tokenPairMap.entries)
_generateDeclaration(entry) _Icon(entry).fullDeclaration
].join(); ].join();
buf.write(iconDeclarationsString); buf.write(iconDeclarationsString);
...@@ -262,52 +268,76 @@ Error: New codepoints file does not contain all the existing codepoints.\n ...@@ -262,52 +268,76 @@ Error: New codepoints file does not contain all the existing codepoints.\n
} }
} }
String _generateDeclaration(MapEntry<String, String> tokenPair) { enum IconStyle {
final String description = tokenPair.key.replaceAll('_', ' '); regular,
outlined,
String styleSuffix = ''; rounded,
String webFontKey = tokenPair.key; sharp,
}
// 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 extension IconStyleSuffix on IconStyle {
// icon rendered next to its key name. Unfortunately, unlike Flutter, this // The suffix for the 'material-icons' HTML class.
// html needs to use a different web font for each style. We read the style's String suffix() {
// suffix from the key for Flutter's icons font, add the corresponding style's switch (this) {
// suffix to the class we pass into html, and then remove the suffix from the case IconStyle.outlined: return '-outlined';
// icon key. The keys needed for the individual web fonts do not use a suffix case IconStyle.rounded: return '-round';
// to denote style. case IconStyle.sharp: return '-sharp';
if (webFontKey.endsWith('_outlined') && webFontKey!='insert_chart_outlined') { default: return '';
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', ''); class _Icon {
// Parse tokenPair (e.g. {"6_ft_apart_outlined": "e004"}).
_Icon(MapEntry<String, String> 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<String, String> rewritePair in _identifierRewrites.entries) {
if (id.startsWith(rewritePair.key)) {
flutterId = id.replaceFirst(rewritePair.key, _identifierRewrites[rewritePair.key]);
}
}
} }
final String identifier = _generateIdentifier(tokenPair.key); // e.g. 5g, 5g_outlined, 5g_rounded, 5g_sharp
final String rtl = _mirroredIcons.contains(tokenPair.key) String id;
? ', matchTextDirection: true' // e.g. 5g
: ''; String shortId;
// e.g. five_g
String flutterId;
// e.g. IconStyle.outlined
IconStyle style;
// e.g. e547
String hexCodepoint;
return ''' // TODO(guidezpl): will be fixed in a future PR to be shortId instead of id
String get mirroredInRTL => _iconsMirroredWhenRTL.contains(id) ? ', matchTextDirection: true' : '';
/// <i class="material-icons$styleSuffix md-36">$webFontKey</i> &#x2014; material icon named "$description". String get name => id.replaceAll('_', ' ');
static const IconData $identifier = IconData(0x${tokenPair.value}, fontFamily: 'MaterialIcons'$rtl);
''';
}
String _generateIdentifier(String rawIdentifier) { String get dartDoc =>
for (final MapEntry<String, String> rewritePair in _identifierRewrites.entries) { '/// <i class="material-icons${style.suffix()} md-36">$shortId</i> &#x2014; material icon named "$name".';
if (rawIdentifier.startsWith(rewritePair.key)) {
return rawIdentifier.replaceFirst(rewritePair.key, _identifierRewrites[rewritePair.key]); String get declaration =>
} "static const IconData $flutterId = IconData(0x$hexCodepoint, fontFamily: 'MaterialIcons'$mirroredInRTL);";
}
return rawIdentifier; String get fullDeclaration => '''\n $dartDoc\n $declaration\n''';
} }
// Replace the old codepoints file with the new. // Replace the old codepoints file with the new.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment