From 7855132341412214167c72189611e0c7c076fa30 Mon Sep 17 00:00:00 2001 From: Pierre-Louis <6655696+guidezpl@users.noreply.github.com> Date: Tue, 5 Apr 2022 10:56:23 +0000 Subject: [PATCH] Generate a demo for new icons when updating icons (#101190) * add code to generate new icon demo and make font family optional * remove trailing spaces * Update update_icons_test.dart * remove trailing space --- dev/tools/test/update_icons_test.dart | 18 +++++++++ dev/tools/update_icons.dart | 56 ++++++++++++++++++++++++--- 2 files changed, 68 insertions(+), 6 deletions(-) diff --git a/dev/tools/test/update_icons_test.dart b/dev/tools/test/update_icons_test.dart index 0eb4b615ca..4d6f2f5147 100644 --- a/dev/tools/test/update_icons_test.dart +++ b/dev/tools/test/update_icons_test.dart @@ -40,4 +40,22 @@ void main() { test('no double underscores', () { expect(Icon.generateFlutterId('abc__123'), 'abc_123'); }); + + test('usage string is correct', () { + expect( + Icon(const MapEntry('abc', '')).usage, + 'Icon(Icons.abc),', + ); + }); + + test('usage string is correct with replacement', () { + expect( + Icon(const MapEntry('123', '')).usage, + 'Icon(Icons.onetwothree),', + ); + expect( + Icon(const MapEntry('123_rounded', '')).usage, + 'Icon(Icons.onetwothree_rounded),', + ); + }); } diff --git a/dev/tools/update_icons.dart b/dev/tools/update_icons.dart index e8ba5c1df9..073345bf75 100644 --- a/dev/tools/update_icons.dart +++ b/dev/tools/update_icons.dart @@ -25,6 +25,7 @@ const String _defaultIconsPath = 'packages/flutter/lib/src/material/icons.dart'; const String _defaultNewCodepointsPath = 'codepoints'; const String _defaultOldCodepointsPath = 'bin/cache/artifacts/material_fonts/codepoints'; const String _defaultFontFamily = 'MaterialIcons'; +const String _defaultDemoFilePath = '/tmp/new_icons_demo.dart'; const String _beginGeneratedMark = '// BEGIN GENERATED ICONS'; const String _endGeneratedMark = '// END GENERATED ICONS'; @@ -216,7 +217,12 @@ void main(List args) { stdout.write(newIconsContents); } else { iconsFile.writeAsStringSync(newIconsContents); - _regenerateCodepointsFile(oldCodepointsFile, newTokenPairMap); + + final SplayTreeMap sortedNewTokenPairMap = SplayTreeMap.of(newTokenPairMap); + _regenerateCodepointsFile(oldCodepointsFile, sortedNewTokenPairMap); + + sortedNewTokenPairMap.removeWhere((String key, String value) => oldTokenPairMap.containsKey(key)); + _generateIconDemo(File(_defaultDemoFilePath), sortedNewTokenPairMap); } } @@ -276,7 +282,7 @@ String _regenerateIconsFile( bool enforceSafetyChecks, ) { final List newIcons = tokenPairMap.entries - .map((MapEntry entry) => Icon(entry, fontFamily)) + .map((MapEntry entry) => Icon(entry, fontFamily: fontFamily)) .toList(); newIcons.sort((Icon a, Icon b) => a._compareTo(b)); @@ -379,18 +385,54 @@ bool testIsStable(Map newCodepoints, Map oldCode } } -void _regenerateCodepointsFile(File oldCodepointsFile, Map newTokenPairMap) { +void _regenerateCodepointsFile(File oldCodepointsFile, Map tokenPairMap) { stderr.writeln('Regenerating old codepoints file ${oldCodepointsFile.path}'); final StringBuffer buf = StringBuffer(); - final SplayTreeMap sortedNewTokenPairMap = SplayTreeMap.of(newTokenPairMap); - sortedNewTokenPairMap.forEach((String key, String value) => buf.writeln('$key $value')); + tokenPairMap.forEach((String key, String value) => buf.writeln('$key $value')); oldCodepointsFile.writeAsStringSync(buf.toString()); } +void _generateIconDemo(File demoFilePath, Map tokenPairMap) { + if (tokenPairMap.isEmpty) { + stderr.writeln('No new icons, skipping generating icon demo'); + return; + } + stderr.writeln('Generating icon demo at $_defaultDemoFilePath'); + + final StringBuffer newIconUsages = StringBuffer(); + for (final MapEntry entry in tokenPairMap.entries) { + newIconUsages.writeln(Icon(entry).usage); + } + final String demoFileContents = ''' + import 'package:flutter/material.dart'; + + void main() => runApp(const IconDemo()); + + class IconDemo extends StatelessWidget { + const IconDemo({ Key? key }) : super(key: key); + + @override + Widget build(BuildContext context) { + return MaterialApp( + debugShowCheckedModeBanner: false, + home: Scaffold( + body: Wrap( + children: const [ + ${newIconUsages.toString()} + ], + ), + ), + ); + } + } + '''; + demoFilePath.writeAsStringSync(demoFileContents); +} + class Icon { // Parse tokenPair (e.g. {"6_ft_apart_outlined": "e004"}). - Icon(MapEntry tokenPair, this.fontFamily) { + Icon(MapEntry tokenPair, {this.fontFamily = _defaultFontFamily}) { id = tokenPair.key; hexCodepoint = tokenPair.value; @@ -450,6 +492,8 @@ class Icon { String get dartDoc => '$shortId — $family icon named "$name"$style'; + String get usage => 'Icon(Icons.$flutterId),'; + String get mirroredInRTL => _iconsMirroredWhenRTL.contains(shortId) ? ', matchTextDirection: true' : '';