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

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
parent bc0ec857
...@@ -40,4 +40,22 @@ void main() { ...@@ -40,4 +40,22 @@ void main() {
test('no double underscores', () { test('no double underscores', () {
expect(Icon.generateFlutterId('abc__123'), 'abc_123'); expect(Icon.generateFlutterId('abc__123'), 'abc_123');
}); });
test('usage string is correct', () {
expect(
Icon(const MapEntry<String, String>('abc', '')).usage,
'Icon(Icons.abc),',
);
});
test('usage string is correct with replacement', () {
expect(
Icon(const MapEntry<String, String>('123', '')).usage,
'Icon(Icons.onetwothree),',
);
expect(
Icon(const MapEntry<String, String>('123_rounded', '')).usage,
'Icon(Icons.onetwothree_rounded),',
);
});
} }
...@@ -25,6 +25,7 @@ const String _defaultIconsPath = 'packages/flutter/lib/src/material/icons.dart'; ...@@ -25,6 +25,7 @@ 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 String _defaultDemoFilePath = '/tmp/new_icons_demo.dart';
const String _beginGeneratedMark = '// BEGIN GENERATED ICONS'; const String _beginGeneratedMark = '// BEGIN GENERATED ICONS';
const String _endGeneratedMark = '// END GENERATED ICONS'; const String _endGeneratedMark = '// END GENERATED ICONS';
...@@ -216,7 +217,12 @@ void main(List<String> args) { ...@@ -216,7 +217,12 @@ void main(List<String> args) {
stdout.write(newIconsContents); stdout.write(newIconsContents);
} else { } else {
iconsFile.writeAsStringSync(newIconsContents); iconsFile.writeAsStringSync(newIconsContents);
_regenerateCodepointsFile(oldCodepointsFile, newTokenPairMap);
final SplayTreeMap<String, String> sortedNewTokenPairMap = SplayTreeMap<String, String>.of(newTokenPairMap);
_regenerateCodepointsFile(oldCodepointsFile, sortedNewTokenPairMap);
sortedNewTokenPairMap.removeWhere((String key, String value) => oldTokenPairMap.containsKey(key));
_generateIconDemo(File(_defaultDemoFilePath), sortedNewTokenPairMap);
} }
} }
...@@ -276,7 +282,7 @@ String _regenerateIconsFile( ...@@ -276,7 +282,7 @@ String _regenerateIconsFile(
bool enforceSafetyChecks, bool enforceSafetyChecks,
) { ) {
final List<Icon> newIcons = tokenPairMap.entries final List<Icon> newIcons = tokenPairMap.entries
.map((MapEntry<String, String> entry) => Icon(entry, fontFamily)) .map((MapEntry<String, String> entry) => Icon(entry, fontFamily: fontFamily))
.toList(); .toList();
newIcons.sort((Icon a, Icon b) => a._compareTo(b)); newIcons.sort((Icon a, Icon b) => a._compareTo(b));
...@@ -379,18 +385,54 @@ bool testIsStable(Map<String, String> newCodepoints, Map<String, String> oldCode ...@@ -379,18 +385,54 @@ bool testIsStable(Map<String, String> newCodepoints, Map<String, String> oldCode
} }
} }
void _regenerateCodepointsFile(File oldCodepointsFile, Map<String, String> newTokenPairMap) { void _regenerateCodepointsFile(File oldCodepointsFile, Map<String, String> tokenPairMap) {
stderr.writeln('Regenerating old codepoints file ${oldCodepointsFile.path}'); stderr.writeln('Regenerating old codepoints file ${oldCodepointsFile.path}');
final StringBuffer buf = StringBuffer(); final StringBuffer buf = StringBuffer();
final SplayTreeMap<String, String> sortedNewTokenPairMap = SplayTreeMap<String, String>.of(newTokenPairMap); tokenPairMap.forEach((String key, String value) => buf.writeln('$key $value'));
sortedNewTokenPairMap.forEach((String key, String value) => buf.writeln('$key $value'));
oldCodepointsFile.writeAsStringSync(buf.toString()); oldCodepointsFile.writeAsStringSync(buf.toString());
} }
void _generateIconDemo(File demoFilePath, Map<String, String> 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<String, String> 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 { 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, this.fontFamily) { Icon(MapEntry<String, String> tokenPair, {this.fontFamily = _defaultFontFamily}) {
id = tokenPair.key; id = tokenPair.key;
hexCodepoint = tokenPair.value; hexCodepoint = tokenPair.value;
...@@ -450,6 +492,8 @@ class Icon { ...@@ -450,6 +492,8 @@ class Icon {
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$htmlSuffix md-36">$shortId</i> &#x2014; $family icon named "$name"$style';
String get usage => 'Icon(Icons.$flutterId),';
String get mirroredInRTL => _iconsMirroredWhenRTL.contains(shortId) String get mirroredInRTL => _iconsMirroredWhenRTL.contains(shortId)
? ', matchTextDirection: true' ? ', matchTextDirection: true'
: ''; : '';
......
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