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

Add option to specify class name (#109865)

parent 360ac5bc
...@@ -18,6 +18,7 @@ const String _iconsTemplatePathOption = 'icons-template'; ...@@ -18,6 +18,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 _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';
...@@ -25,6 +26,7 @@ const String _defaultIconsPath = 'packages/flutter/lib/src/material/icons.dart'; ...@@ -25,6 +26,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 _defaultClassName = 'Icons';
const String _defaultDemoFilePath = '/tmp/new_icons_demo.dart'; const String _defaultDemoFilePath = '/tmp/new_icons_demo.dart';
const String _beginGeneratedMark = '// BEGIN GENERATED ICONS'; const String _beginGeneratedMark = '// BEGIN GENERATED ICONS';
...@@ -211,6 +213,7 @@ void main(List<String> args) { ...@@ -211,6 +213,7 @@ void main(List<String> args) {
iconsTemplateContents, iconsTemplateContents,
newTokenPairMap, newTokenPairMap,
argResults[_fontFamilyOption] as String, argResults[_fontFamilyOption] as String,
argResults[_classNameOption] as String,
argResults[_enforceSafetyChecks] as bool, argResults[_enforceSafetyChecks] as bool,
); );
...@@ -245,6 +248,9 @@ ArgResults _handleArguments(List<String> args) { ...@@ -245,6 +248,9 @@ 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')
..addOption(_classNameOption,
defaultsTo: _defaultClassName,
help: 'The containing class for all icons')
..addFlag(_enforceSafetyChecks, ..addFlag(_enforceSafetyChecks,
defaultsTo: true, defaultsTo: true,
help: 'Whether to exit if safety checks fail (e.g. codepoints are missing or unstable') help: 'Whether to exit if safety checks fail (e.g. codepoints are missing or unstable')
...@@ -280,10 +286,12 @@ String _regenerateIconsFile( ...@@ -280,10 +286,12 @@ String _regenerateIconsFile(
String templateFileContents, String templateFileContents,
Map<String, String> tokenPairMap, Map<String, String> tokenPairMap,
String fontFamily, String fontFamily,
String className,
bool enforceSafetyChecks, bool enforceSafetyChecks,
) { ) {
final List<Icon> newIcons = tokenPairMap.entries final List<Icon> newIcons = tokenPairMap.entries
.map((MapEntry<String, String> entry) => Icon(entry, fontFamily: fontFamily)) .map((MapEntry<String, String> entry) =>
Icon(entry, fontFamily: fontFamily, className: className))
.toList(); .toList();
newIcons.sort((Icon a, Icon b) => a._compareTo(b)); newIcons.sort((Icon a, Icon b) => a._compareTo(b));
...@@ -309,7 +317,8 @@ String _regenerateIconsFile( ...@@ -309,7 +317,8 @@ String _regenerateIconsFile(
final Icon iOSIcon = newIcons.firstWhere( final Icon iOSIcon = newIcons.firstWhere(
(Icon icon) => icon.id == '${ids[1]}$style', (Icon icon) => icon.id == '${ids[1]}$style',
orElse: () => throw ids[1]); orElse: () => throw ids[1]);
platformAdaptiveDeclarations.add(Icon.platformAdaptiveDeclaration('$flutterId$style', agnosticIcon, iOSIcon), platformAdaptiveDeclarations.add(
agnosticIcon.platformAdaptiveDeclaration('$flutterId$style', iOSIcon),
); );
} catch (e) { } catch (e) {
if (style == '') { if (style == '') {
...@@ -433,7 +442,10 @@ void _generateIconDemo(File demoFilePath, Map<String, String> tokenPairMap) { ...@@ -433,7 +442,10 @@ void _generateIconDemo(File demoFilePath, Map<String, String> tokenPairMap) {
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 = _defaultFontFamily}) { Icon(MapEntry<String, String> tokenPair, {
this.fontFamily = _defaultFontFamily,
this.className = _defaultClassName,
}) {
id = tokenPair.key; id = tokenPair.key;
hexCodepoint = tokenPair.value; hexCodepoint = tokenPair.value;
...@@ -485,6 +497,7 @@ class Icon { ...@@ -485,6 +497,7 @@ class Icon {
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 htmlSuffix = ''; // The suffix for the 'material-icons' HTML class.
String fontFamily; // The IconData font family. String fontFamily; // The IconData font family.
String className; // The containing class.
String get name => shortId.replaceAll('_', ' ').trim(); String get name => shortId.replaceAll('_', ' ').trim();
...@@ -493,7 +506,7 @@ class Icon { ...@@ -493,7 +506,7 @@ 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 usage => 'Icon($className.$flutterId),';
String get mirroredInRTL => _iconsMirroredWhenRTL.contains(shortId) String get mirroredInRTL => _iconsMirroredWhenRTL.contains(shortId)
? ', matchTextDirection: true' ? ', matchTextDirection: true'
...@@ -508,10 +521,10 @@ class Icon { ...@@ -508,10 +521,10 @@ class Icon {
$declaration $declaration
'''; ''';
static String platformAdaptiveDeclaration(String fullFlutterId, Icon agnosticIcon, Icon iOSIcon) => ''' String platformAdaptiveDeclaration(String fullFlutterId, Icon iOSIcon) => '''
/// Platform-adaptive icon for ${agnosticIcon.dartDoc} and ${iOSIcon.dartDoc}.; /// Platform-adaptive icon for $dartDoc and ${iOSIcon.dartDoc}.;
IconData get $fullFlutterId => !_isCupertino() ? Icons.${agnosticIcon.flutterId} : Icons.${iOSIcon.flutterId}; IconData get $fullFlutterId => !_isCupertino() ? $className.$flutterId : $className.${iOSIcon.flutterId};
'''; ''';
@override @override
......
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