145 lines
4.5 KiB
Dart
145 lines
4.5 KiB
Dart
import 'dart:io';
|
|
import 'utils.dart';
|
|
|
|
/// 自动生成外部使用的图标封装文件
|
|
///
|
|
/// 从 iconfont.json 文件读取图标信息,生成 FontIcons 封装类
|
|
/// 生成位置:项目根目录的 icons.dart
|
|
void main() {
|
|
final iconData = readIconFontJson();
|
|
final glyphs = getGlyphs(iconData);
|
|
final definedIcons = getDefinedIconNames();
|
|
|
|
_generateWrapperFile(glyphs, definedIcons);
|
|
|
|
print('✅ 外部图标封装文件生成成功: \x1B[32mgenerate/icons.dart\x1B[0m');
|
|
print('📊 共生成 ${definedIcons.length} 个图标常量');
|
|
}
|
|
|
|
/// 生成 icons.dart 文件
|
|
void _generateWrapperFile(List<dynamic> glyphs, Set<String> definedIcons) {
|
|
final buffer = StringBuffer();
|
|
|
|
// 文件头部
|
|
buffer.writeln("// ignore_for_file: constant_identifier_names");
|
|
buffer.writeln();
|
|
buffer.writeln("import 'package:flutter/material.dart';");
|
|
buffer.writeln("import 'package:yx_icon_fonts/yx_icon_fonts.dart';");
|
|
buffer.writeln();
|
|
buffer.writeln("class FontIcons {");
|
|
buffer.writeln(" // 私有构造函数,防止实例化");
|
|
buffer.writeln(" FontIcons._();");
|
|
buffer.writeln();
|
|
|
|
// 按类别分组图标
|
|
final categories = _categorizeIcons(glyphs, definedIcons);
|
|
|
|
// 定义类别显示顺序
|
|
final categoryOrder = [
|
|
'其他图标',
|
|
'编辑相关图标',
|
|
'删除相关图标',
|
|
'个人中心相关图标',
|
|
'消息相关图标',
|
|
'箭头相关图标',
|
|
'特殊字符图标',
|
|
'团队和用户相关图标',
|
|
'功能图标',
|
|
'退出相关图标',
|
|
'更多和菜单相关图标',
|
|
'复制相关图标',
|
|
'日历相关图标',
|
|
'分享相关图标',
|
|
'添加相关图标',
|
|
'键盘相关图标',
|
|
];
|
|
|
|
// 按顺序生成图标
|
|
for (final category in categoryOrder) {
|
|
if (categories.containsKey(category) && categories[category]!.isNotEmpty) {
|
|
buffer.writeln(" // $category");
|
|
for (final iconName in categories[category]!) {
|
|
buffer.writeln(" static const IconData $iconName = YXIconFonts.$iconName;");
|
|
}
|
|
buffer.writeln();
|
|
}
|
|
}
|
|
|
|
buffer.writeln("}");
|
|
|
|
// 确保 generate 目录存在
|
|
final dir = Directory('generate');
|
|
if (!dir.existsSync()) {
|
|
dir.createSync(recursive: true);
|
|
}
|
|
|
|
// 写入文件
|
|
final file = File('generate/icons.dart');
|
|
file.writeAsStringSync(buffer.toString());
|
|
}
|
|
|
|
/// 将图标按类别分组
|
|
Map<String, List<String>> _categorizeIcons(List<dynamic> glyphs, Set<String> definedIcons) {
|
|
final categories = <String, List<String>>{};
|
|
|
|
for (final glyph in glyphs) {
|
|
final name = glyph['font_class'] as String;
|
|
final camelCaseName = toLegalDartName(name);
|
|
|
|
// 跳过未定义的图标
|
|
if (!definedIcons.contains(camelCaseName)) continue;
|
|
|
|
// 确定类别
|
|
String category = _getCategory(name);
|
|
|
|
categories.putIfAbsent(category, () => []);
|
|
categories[category]!.add(camelCaseName);
|
|
}
|
|
|
|
return categories;
|
|
}
|
|
|
|
/// 根据图标名称确定类别
|
|
String _getCategory(String name) {
|
|
final lowerName = name.toLowerCase();
|
|
|
|
if (lowerName.contains('edit')) {
|
|
return '编辑相关图标';
|
|
} else if (lowerName.contains('delete')) {
|
|
return '删除相关图标';
|
|
} else if (lowerName.contains('me_')) {
|
|
return '个人中心相关图标';
|
|
} else if (lowerName.contains('msg')) {
|
|
return '消息相关图标';
|
|
} else if (lowerName.contains('arrow')) {
|
|
return '箭头相关图标';
|
|
} else if (lowerName == '24_-' || lowerName.contains('subtract')) {
|
|
return '特殊字符图标';
|
|
} else if (lowerName.contains('team') || lowerName.contains('student')) {
|
|
return '团队和用户相关图标';
|
|
} else if (lowerName.contains('filter') ||
|
|
lowerName.contains('question') ||
|
|
lowerName.contains('onlysee') ||
|
|
lowerName.contains('hint') ||
|
|
lowerName.contains('switch')) {
|
|
return '功能图标';
|
|
} else if (lowerName.contains('quit')) {
|
|
return '退出相关图标';
|
|
} else if (lowerName.contains('more')) {
|
|
return '更多和菜单相关图标';
|
|
} else if (lowerName.contains('copy')) {
|
|
return '复制相关图标';
|
|
} else if (lowerName.contains('calendar')) {
|
|
return '日历相关图标';
|
|
} else if (lowerName.contains('share')) {
|
|
return '分享相关图标';
|
|
} else if (lowerName.contains('add') || lowerName.contains('+') || lowerName.contains('plus')) {
|
|
return '添加相关图标';
|
|
} else if (lowerName.contains('keyboard')) {
|
|
return '键盘相关图标';
|
|
} else {
|
|
return '其他图标';
|
|
}
|
|
}
|
|
|