-
Notifications
You must be signed in to change notification settings - Fork 229
/
generate_font.dart
81 lines (64 loc) · 1.99 KB
/
generate_font.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import 'dart:convert';
import 'dart:io';
import 'package:recase/recase.dart';
void main(List<String> arguments) {
var file = new File(arguments.first);
if (!file.existsSync()) {
print('Cannot find the file "${arguments.first}".');
}
var content = file.readAsStringSync();
Map<String, dynamic> icons = json.decode(content);
Map<String, String> iconDefinitions = {};
for (String iconName in icons.keys) {
var icon = icons[iconName];
var unicode = icon['unicode'];
List<String> styles = (icon['styles'] as List).cast<String>();
if (styles.length > 1) {
if (styles.contains('regular')) {
styles.remove('regular');
iconDefinitions[iconName] = generateIconDefinition(
iconName,
'regular',
unicode,
);
}
for (String style in styles) {
String name = '${style}_$iconName';
iconDefinitions[name] = generateIconDefinition(
name,
styles.first,
unicode,
);
}
} else {
iconDefinitions[iconName] = generateIconDefinition(
iconName,
styles.first,
unicode,
);
}
}
List<String> generatedOutput = [
'library font_awesome_flutter;',
'',
"import 'package:flutter/widgets.dart';",
"import 'package:font_awesome_flutter/icon_data.dart';",
'',
'// THIS FILE IS AUTOMATICALLY GENERATED!',
'',
'class FontAwesomeIcons {',
];
generatedOutput.addAll(iconDefinitions.values);
generatedOutput.add('}');
File output = new File('lib/font_awesome_flutter.dart');
output.writeAsStringSync(generatedOutput.join('\n'));
}
String generateIconDefinition(String iconName, String style, String unicode) {
style = '${style[0].toUpperCase()}${style.substring(1)}';
String iconDataSource = 'IconData$style';
if (iconName == '500px') {
iconName = 'fiveHundredPx';
}
iconName = new ReCase(iconName).camelCase;
return 'static const IconData $iconName = const $iconDataSource(0x$unicode);';
}