forked from bdlukaa/fluent_ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dictionary_generator.dart
177 lines (152 loc) · 5.39 KB
/
dictionary_generator.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// ignore_for_file: avoid_print
import 'dart:io';
import 'icon_generator.dart' show pathSeparator;
import 'resource_dictionary.dart';
const String fileHeader = """
// GENERATED FILE, DO NOT EDIT
// This generates the values from https://github.com/microsoft/microsoft-ui-xaml/blob/main/dev/CommonStyles/Common_themeresources_any.xaml
// ignore_for_file: constant_identifier_names
import 'package:flutter/widgets.dart' show Color, ColorProperty;
import 'package:flutter/foundation.dart';
""";
const String header = '''
/// A dictionary of colors used by all the components.
///
/// Use `ResourceDictionary.dark` or `ResourceDictionary.light` to get colors
/// adapted to dark and light mode, respectively
///
/// See also:
///
/// * <https://github.com/microsoft/microsoft-ui-xaml/blob/main/dev/CommonStyles/Common_themeresources_any.xaml>
class ResourceDictionary with Diagnosticable {
''';
/// How to generate the resources:
/// - Go on `https://github.com/microsoft/microsoft-ui-xaml/blob/main/dev/CommonStyles/Common_themeresources_any.xaml`
/// - Copy the colors under <Default> and paste them on [defaultResourceDirectionary]
/// - Copy the colors under <Light> and paste them on [lightResourceDictionary]
/// - Run the generator with `dart bin/dictionary_generator.dart.dart` while being
/// on the `lib/` directory as PWD
/// - Enjoy
///
/// This only support the <Color> tag
void main(List<String> args) async {
if (Directory.current.path.split(pathSeparator).last == 'bin') {
print('Run the generator from the lib/ folder');
return;
}
final dartFileBuffer = StringBuffer(fileHeader);
// Generates
void generateFor(String dictionary, String constructor) {
dartFileBuffer
..writeln('\n // Colors adapted for $constructor mode')
..writeln(' const ResourceDictionary.$constructor({');
for (final resourceLine in dictionary.split('\n')) {
if (resourceLine.trim().isEmpty) continue;
final resourceName = () {
final split = resourceLine.trim().split('"');
return split[1];
}();
var resourceHex = () {
final result = resourceLine
.replaceAll('<Color x:Key="$resourceName">', '')
.replaceAll('</Color>', '')
.trim();
return result;
}()
.replaceAll('#', '')
.toLowerCase();
if (resourceHex.length == 6) {
resourceHex = 'FF$resourceHex';
}
dartFileBuffer.writeln(
' this.${resourceName.lowercaseFirst()} = const Color(0x$resourceHex),',
);
}
dartFileBuffer.writeln(' });');
}
// generates abstract dictionary
dartFileBuffer.writeln(header);
for (final resourceLine in defaultResourceDirectionary.split('\n')) {
if (resourceLine.trim().isEmpty) continue;
final resourceName = () {
final split = resourceLine.trim().split('"');
return split[1];
}();
dartFileBuffer.writeln(
' final Color ${resourceName.lowercaseFirst()};',
);
}
dartFileBuffer.writeln('\n const ResourceDictionary.raw({');
for (final resourceLine in defaultResourceDirectionary.split('\n')) {
if (resourceLine.trim().isEmpty) continue;
final resourceName = () {
final split = resourceLine.trim().split('"');
return split[1];
}()
.lowercaseFirst();
dartFileBuffer.writeln(
' required this.$resourceName,',
);
}
dartFileBuffer.writeln(' });');
// generate default dictionary
generateFor(defaultResourceDirectionary, 'dark');
// generate light resource dictionary
generateFor(lightResourceDictionary, 'light');
// generate lerp method
dartFileBuffer
..writeln(
'\n static ResourceDictionary lerp(ResourceDictionary a, ResourceDictionary b, double t,) {')
..writeln(' return ResourceDictionary.raw(');
for (final resourceLine in defaultResourceDirectionary.split('\n')) {
if (resourceLine.trim().isEmpty) continue;
final resourceName = () {
final split = resourceLine.trim().split('"');
return split[1];
}()
.lowercaseFirst();
dartFileBuffer.writeln(
' $resourceName: Color.lerp(a.$resourceName, b.$resourceName, t,)!,',
);
}
dartFileBuffer
..writeln(' );')
..writeln(' }')
// generate debugFillProperties method
..writeln(
'\n @override\n void debugFillProperties(DiagnosticPropertiesBuilder properties) {')
..writeln(' super.debugFillProperties(properties);')
..writeln('properties');
for (final resourceLine in defaultResourceDirectionary.split('\n')) {
if (resourceLine.trim().isEmpty) continue;
final resourceName = () {
final split = resourceLine.trim().split('"');
return split[1];
}()
.lowercaseFirst();
dartFileBuffer.writeln(
" ..add(ColorProperty('${resourceName.lowercaseFirst()}', $resourceName))",
);
}
dartFileBuffer
..writeln(';')
..writeln(' }')
..writeln('}');
final outputFile = File('lib/src/styles/color_resources.dart');
final formatProcess = await Process.start(
'flutter',
['format', outputFile.path],
runInShell: true,
);
stdout.addStream(formatProcess.stdout);
await outputFile.writeAsString(dartFileBuffer.toString());
}
extension StringExtension on String {
/// Results this string with the first char uppercased
///
/// January -> january
String lowercaseFirst() {
final first = substring(0, 1);
return first.toLowerCase() + substring(1);
}
}