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

Refactor icon update script (#122392)

* x

* docs
parent 376d8baa
...@@ -17,6 +17,9 @@ Map<String, String> codepointsC = <String, String>{ ...@@ -17,6 +17,9 @@ Map<String, String> codepointsC = <String, String>{
'airplane': '111', 'airplane': '111',
'train': '444', 'train': '444',
}; };
Map<String, String> codepointsUnderscore = <String, String>{
'airplane__123': '111',
};
void main() { void main() {
group('safety checks', () { group('safety checks', () {
...@@ -38,7 +41,7 @@ void main() { ...@@ -38,7 +41,7 @@ void main() {
}); });
test('no double underscores', () { test('no double underscores', () {
expect(Icon.generateFlutterId('abc__123'), 'abc_123'); expect(Icon(codepointsUnderscore.entries.first), 'abc_123');
}); });
test('usage string is correct', () { test('usage string is correct', () {
......
...@@ -2,8 +2,10 @@ ...@@ -2,8 +2,10 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// Regenerates the material icons file. // Regenerates a Dart file with a class containing IconData constants.
// See https://github.com/flutter/flutter/wiki/Updating-Material-Design-Fonts-&-Icons // See https://github.com/flutter/flutter/wiki/Updating-Material-Design-Fonts-&-Icons
// Should be idempotent with:
// dart dev/tools/update_icons.dart --new-codepoints bin/cache/artifacts/material_fonts/codepoints
import 'dart:collection'; import 'dart:collection';
import 'dart:convert' show LineSplitter; import 'dart:convert' show LineSplitter;
...@@ -18,6 +20,7 @@ const String _iconsTemplatePathOption = 'icons-template'; ...@@ -18,6 +20,7 @@ const String _iconsTemplatePathOption = 'icons-template';
const String _newCodepointsPathOption = 'new-codepoints'; const String _newCodepointsPathOption = 'new-codepoints';
const String _oldCodepointsPathOption = 'old-codepoints'; const String _oldCodepointsPathOption = 'old-codepoints';
const String _fontFamilyOption = 'font-family'; const String _fontFamilyOption = 'font-family';
const String _possibleStyleSuffixesOption = 'style-suffixes';
const String _classNameOption = 'class-name'; const String _classNameOption = 'class-name';
const String _enforceSafetyChecks = 'enforce-safety-checks'; const String _enforceSafetyChecks = 'enforce-safety-checks';
const String _dryRunOption = 'dry-run'; const String _dryRunOption = 'dry-run';
...@@ -26,6 +29,11 @@ const String _defaultIconsPath = 'packages/flutter/lib/src/material/icons.dart'; ...@@ -26,6 +29,11 @@ const String _defaultIconsPath = 'packages/flutter/lib/src/material/icons.dart';
const String _defaultNewCodepointsPath = 'codepoints'; const String _defaultNewCodepointsPath = 'codepoints';
const String _defaultOldCodepointsPath = 'bin/cache/artifacts/material_fonts/codepoints'; const String _defaultOldCodepointsPath = 'bin/cache/artifacts/material_fonts/codepoints';
const String _defaultFontFamily = 'MaterialIcons'; const String _defaultFontFamily = 'MaterialIcons';
const List<String> _defaultPossibleStyleSuffixes = <String>[
'_outlined',
'_rounded',
'_sharp',
];
const String _defaultClassName = 'Icons'; const String _defaultClassName = 'Icons';
const String _defaultDemoFilePath = '/tmp/new_icons_demo.dart'; const String _defaultDemoFilePath = '/tmp/new_icons_demo.dart';
...@@ -91,7 +99,7 @@ const Map<String, String> _identifierExactRewrites = <String, String>{ ...@@ -91,7 +99,7 @@ const Map<String, String> _identifierExactRewrites = <String, String>{
const Set<String> _iconsMirroredWhenRTL = <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 // https://developers.google.com/fonts/docs/material_icons#which_icons_should_be_mirrored_for_rtl
'arrow_back', 'arrow_back',
'arrow_back_ios', 'arrow_back_ios',
'arrow_forward', 'arrow_forward',
...@@ -248,6 +256,10 @@ ArgResults _handleArguments(List<String> args) { ...@@ -248,6 +256,10 @@ ArgResults _handleArguments(List<String> args) {
..addOption(_fontFamilyOption, ..addOption(_fontFamilyOption,
defaultsTo: _defaultFontFamily, defaultsTo: _defaultFontFamily,
help: 'The font family to use for the IconData constants') help: 'The font family to use for the IconData constants')
..addMultiOption(_possibleStyleSuffixesOption,
defaultsTo: _defaultPossibleStyleSuffixes,
help: 'A comma-separated list of suffixes (typically an optional '
'family + a style) e.g. _outlined, _monoline_filled')
..addOption(_classNameOption, ..addOption(_classNameOption,
defaultsTo: _defaultClassName, defaultsTo: _defaultClassName,
help: 'The containing class for all icons') help: 'The containing class for all icons')
...@@ -444,67 +456,60 @@ class Icon { ...@@ -444,67 +456,60 @@ class Icon {
// Parse tokenPair (e.g. {"6_ft_apart_outlined": "e004"}). // Parse tokenPair (e.g. {"6_ft_apart_outlined": "e004"}).
Icon(MapEntry<String, String> tokenPair, { Icon(MapEntry<String, String> tokenPair, {
this.fontFamily = _defaultFontFamily, this.fontFamily = _defaultFontFamily,
this.possibleStyleSuffixes = _defaultPossibleStyleSuffixes,
this.className = _defaultClassName, this.className = _defaultClassName,
}) { }) {
id = tokenPair.key; id = tokenPair.key;
hexCodepoint = tokenPair.value; hexCodepoint = tokenPair.value;
// Determine family and htmlSuffix. // Determine family and HTML class suffix for Dartdoc.
if (id.endsWith('_gm_outlined')) { if (id.endsWith('_gm_outlined')) {
family = 'GM'; dartdocFamily = 'GM';
htmlSuffix = '-outlined'; dartdocHtmlSuffix = '-outlined';
} else if (id.endsWith('_gm_filled')) { } else if (id.endsWith('_gm_filled')) {
family = 'GM'; dartdocFamily = 'GM';
htmlSuffix = '-filled'; dartdocHtmlSuffix = '-filled';
} else if (id.endsWith('_monoline_outlined')) { } else if (id.endsWith('_monoline_outlined')) {
family = 'Monoline'; dartdocFamily = 'Monoline';
htmlSuffix = '-outlined'; dartdocHtmlSuffix = '-outlined';
} else if (id.endsWith('_monoline_filled')) { } else if (id.endsWith('_monoline_filled')) {
family = 'Monoline'; dartdocFamily = 'Monoline';
htmlSuffix = '-filled'; dartdocHtmlSuffix = '-filled';
} else { } else {
family = 'material'; dartdocFamily = 'material';
if (id.endsWith('_baseline')) { if (id.endsWith('_baseline')) {
id = _removeLast(id, '_baseline'); id = _removeLast(id, '_baseline');
htmlSuffix = ''; dartdocHtmlSuffix = '';
} else if (id.endsWith('_outlined')) { } else if (id.endsWith('_outlined')) {
htmlSuffix = '-outlined'; dartdocHtmlSuffix = '-outlined';
} else if (id.endsWith('_rounded')) { } else if (id.endsWith('_rounded')) {
htmlSuffix = '-round'; dartdocHtmlSuffix = '-round';
} else if (id.endsWith('_sharp')) { } else if (id.endsWith('_sharp')) {
htmlSuffix = '-sharp'; dartdocHtmlSuffix = '-sharp';
} }
} }
shortId = _generateShortId(id); _generateShortId();
flutterId = generateFlutterId(id); _generateFlutterId();
} }
static const List<String> _idSuffixes = <String>[
'_gm_outlined',
'_gm_filled',
'_monoline_outlined',
'_monoline_filled',
'_outlined',
'_rounded',
'_sharp',
];
late String id; // e.g. 5g, 5g_outlined, 5g_rounded, 5g_sharp late String id; // e.g. 5g, 5g_outlined, 5g_rounded, 5g_sharp
late String shortId; // e.g. 5g late String shortId; // e.g. 5g
late String flutterId; // e.g. five_g, five_g_outlined, five_g_rounded, five_g_sharp late String flutterId; // e.g. five_g, five_g_outlined, five_g_rounded, five_g_sharp
late String family; // e.g. material
late String hexCodepoint; // e.g. e547 late String hexCodepoint; // e.g. e547
late String htmlSuffix = ''; // The suffix for the 'material-icons' HTML class. late String dartdocFamily; // e.g. material
late String dartdocHtmlSuffix = ''; // The suffix for the 'material-icons' HTML class.
String fontFamily; // The IconData font family. String fontFamily; // The IconData font family.
List<String> possibleStyleSuffixes; // A list of possible suffixes e.g. _outlined, _monoline_filled.
String className; // The containing class. String className; // The containing class.
String get name => shortId.replaceAll('_', ' ').trim(); String get name => shortId.replaceAll('_', ' ').trim();
String get style => htmlSuffix == '' ? '' : ' (${htmlSuffix.replaceFirst('-', '')})'; String get style => dartdocHtmlSuffix == '' ? '' : ' (${dartdocHtmlSuffix.replaceFirst('-', '')})';
String get dartDoc => String get dartDoc =>
'<i class="material-icons$htmlSuffix md-36">$shortId</i> &#x2014; $family icon named "$name"$style'; '<i class="material-icons$dartdocHtmlSuffix md-36">$shortId</i> &#x2014; $dartdocFamily icon named "$name"$style';
String get usage => 'Icon($className.$flutterId),'; String get usage => 'Icon($className.$flutterId),';
...@@ -543,23 +548,22 @@ class Icon { ...@@ -543,23 +548,22 @@ class Icon {
return string.replaceAll(RegExp('$toReplace\$'), ''); return string.replaceAll(RegExp('$toReplace\$'), '');
} }
static String _generateShortId(String id) { /// See [shortId].
String shortId = id; void _generateShortId() {
for (final String styleSuffix in _idSuffixes) { shortId = id;
for (final String styleSuffix in possibleStyleSuffixes) {
shortId = _removeLast(shortId, styleSuffix); shortId = _removeLast(shortId, styleSuffix);
if (shortId != id) { if (shortId != id) {
break; break;
} }
} }
return shortId;
} }
/// Given some icon's raw id, returns a valid Dart icon identifier /// See [flutterId].
static String generateFlutterId(String id) { void _generateFlutterId() {
String flutterId = id; flutterId = id;
// Exact identifier rewrites. // Exact identifier rewrites.
for (final MapEntry<String, String> rewritePair in _identifierExactRewrites.entries) { for (final MapEntry<String, String> rewritePair in _identifierExactRewrites.entries) {
final String shortId = Icon._generateShortId(id);
if (shortId == rewritePair.key) { if (shortId == rewritePair.key) {
flutterId = id.replaceFirst( flutterId = id.replaceFirst(
rewritePair.key, rewritePair.key,
...@@ -579,7 +583,5 @@ class Icon { ...@@ -579,7 +583,5 @@ class Icon {
// Prevent double underscores. // Prevent double underscores.
flutterId = flutterId.replaceAll('__', '_'); flutterId = flutterId.replaceAll('__', '_');
return flutterId;
} }
} }
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