@@ -93,70 +93,207 @@ Future<void> applyDefinitions({
93
93
final YamlNode rootNode = document.contents;
94
94
95
95
final YamlNode definitions = rootNode.value['definitions' ];
96
- final YamlMap colors = definitions.value['colors' ];
96
+ final YamlMap ? colors = definitions.value['colors' ];
97
+ final YamlMap ? sizes = definitions.value['sizes' ];
98
+ final YamlMap ? flags = definitions.value['flags' ];
97
99
98
- final Map colorsMap = Map .fromEntries (colors.entries);
100
+ final Map colorsMap = Map .fromEntries (colors? .entries ?? {});
101
+ final Map sizesMap = Map .fromEntries (sizes? .entries ?? {});
102
+ final Map flagsMap = Map .fromEntries (flags? .entries ?? {});
99
103
100
104
for (var file in configFiles) {
101
- var contents = file.readAsStringSync ();
105
+ writeDefsToFile (file, colorsMap, 'colors' , true );
106
+ writeDefsToFile (file, sizesMap, 'sizes' );
107
+ writeDefsToFile (file, flagsMap, 'flags' );
108
+ }
109
+ }
110
+
111
+ void writeDefsToFile (
112
+ File file,
113
+ Map defs,
114
+ String node, [
115
+ bool quoteWrap = false ,
116
+ ]) {
117
+ if (defs.isEmpty) {
118
+ return ;
119
+ }
120
+
121
+ var contents = file.readAsStringSync ();
122
+
123
+ List <String > lines = contents.split ('\n ' );
124
+
125
+ var configurationNodeIdx = lines.indexWhere ((l) {
126
+ return l.trim ().startsWith ('configuration' );
127
+ });
128
+
129
+ var lookupNodeIdx = lines.indexWhere ((l) {
130
+ return l.contains ('$node :' ) && ! l.trim ().startsWith ('#' );
131
+ });
132
+
133
+ if (lookupNodeIdx < 0 || lookupNodeIdx > configurationNodeIdx) {
134
+ return ;
135
+ }
102
136
103
- List <String > lines = contents.split ('\n ' );
137
+ var nodeStartIdx = lookupNodeIdx + 1 ;
138
+ var nodeEndIdx = nodeStartIdx;
104
139
105
- var colorNodeIdx = lines. indexWhere ((l) => l. contains ( 'colors:' )) ;
140
+ var searchIdx = nodeStartIdx + 1 ;
106
141
107
- if (colorNodeIdx < 0 ) {
108
- continue ;
142
+ while (searchIdx < lines.length && lines[searchIdx].startsWith (' ' )) {
143
+ nodeEndIdx++ ;
144
+ searchIdx++ ;
145
+ }
146
+
147
+ List <String > newDefs = [];
148
+
149
+ String ? getCurrentLineIfExists (String key) {
150
+ var line = lines
151
+ .sublist (nodeStartIdx, nodeEndIdx + 1 )
152
+ .firstWhereOrNull ((l) => l.startsWith ('$key :' ));
153
+
154
+ if (line == null ) {
155
+ return null ;
156
+ }
157
+
158
+ var lineIdx = lines.indexOf (line);
159
+
160
+ if (lineIdx == - 1 ) {
161
+ return null ;
109
162
}
110
163
111
- var colorStartIdx = colorNodeIdx + 1 ;
112
- var colorEndIdx = colorStartIdx;
164
+ var outOfBounds = lineIdx < nodeStartIdx || lineIdx > nodeEndIdx;
165
+
166
+ return outOfBounds ? null : line;
167
+ }
168
+
169
+ void updateLineIfNotEqual ({
170
+ required String oldLine,
171
+ required String newLine,
172
+ }) {
173
+ var lineIdx = lines.indexOf (oldLine);
113
174
114
- var searchIdx = colorStartIdx + 1 ;
175
+ if (lineIdx > - 1 ) {
176
+ var oldLine = lines[lineIdx];
115
177
116
- while (searchIdx < lines.length && lines[searchIdx].startsWith (' ' )) {
117
- colorEndIdx++ ;
118
- searchIdx++ ;
178
+ if (oldLine != newLine) {
179
+ lines[lineIdx] = newLine;
180
+ print ('Updated ${node .capitalized } Def -> $newLine ' );
181
+ } else {
182
+ print ('Did not update def, no change: $newLine ' );
183
+ }
119
184
}
185
+ }
186
+
187
+ List <String > expandDefMap (
188
+ Map map,
189
+ List <String > result,
190
+ List <String > keys,
191
+ int depth,
192
+ bool quoteWrap,
193
+ ) {
194
+ List <String > result = [];
195
+
196
+ for (var entry in map.entries) {
197
+ List <String > _keys = List .from (keys);
198
+ _keys.add (entry.key);
120
199
121
- List <String > newDefs = [];
200
+ if (entry.value is Map ) {
201
+ var padding = List .filled (2 * depth, ' ' ).join ();
122
202
123
- for (var entry in colorsMap.entries) {
124
- var line = lines.sublist (colorStartIdx, colorEndIdx + 1 ).firstWhereOrNull ((l) => l.trim ().startsWith ('${entry .key }:' ));
203
+ var oldLine = getCurrentLineIfExists ('$padding ${entry .key }' );
125
204
126
- if (line == null ) {
127
- newDefs.add (' ${entry .key }: &${entry .key } "${entry .value }"' );
205
+ if (oldLine != null ) {
206
+ continue ;
207
+ }
208
+
209
+ result.add ('$padding ${entry .key }:' );
210
+
211
+ result.addAll (
212
+ expandDefMap (
213
+ entry.value,
214
+ result,
215
+ _keys,
216
+ depth + 1 ,
217
+ quoteWrap,
218
+ ),
219
+ );
128
220
} else {
129
- var lineIdx = lines.indexOf (line);
221
+ var padding = List .filled (2 * depth, ' ' ).join ();
222
+ var value = quoteWrap ? '"${entry .value }"' : entry.value;
223
+
224
+ var oldLine = getCurrentLineIfExists ('$padding ${entry .key }' );
130
225
131
- if (lineIdx > - 1 ) {
132
- var oldLine = lines[lineIdx];
133
- var newLine = ' ${entry .key }: &${entry .key } "${entry .value }"' ;
226
+ var keyPart = '&${_keys .join ('.' ).canonicalize }' ;
227
+ var newLine = '$padding ${entry .key }: $keyPart $value ' ;
134
228
135
- if (oldLine != newLine) {
136
- lines[lineIdx] = ' ${entry .key }: &${entry .key } "${entry .value }"' ;
137
- print ('Update Color Def -> $line -> ${lines [lineIdx ]}' );
138
- }
229
+ if (oldLine == null ) {
230
+ result.add (newLine);
231
+ } else {
232
+ updateLineIfNotEqual (
233
+ oldLine: oldLine,
234
+ newLine: newLine,
235
+ );
139
236
}
140
237
}
141
238
}
142
239
143
- if (newDefs.isNotEmpty) {
144
- for (var def in newDefs) {
145
- lines.insert (colorEndIdx, def);
146
- colorEndIdx++ ;
147
- print ('Insert Color Def -> $def ' );
240
+ return result;
241
+ }
242
+
243
+ for (var entry in defs.entries) {
244
+ var line = getCurrentLineIfExists (' ${entry .key }' );
245
+
246
+ if (line == null || entry.value is Map ) {
247
+ if (entry.value is Map ) {
248
+ var oldLine = getCurrentLineIfExists (' ${entry .key }' );
249
+ var newLine = ' ${entry .key }:' ;
250
+
251
+ if (oldLine == null ) {
252
+ newDefs.add (newLine);
253
+ } else {
254
+ updateLineIfNotEqual (
255
+ oldLine: oldLine,
256
+ newLine: newLine,
257
+ );
258
+ }
259
+
260
+ newDefs.addAll (
261
+ expandDefMap (
262
+ entry.value,
263
+ [],
264
+ [node, entry.key],
265
+ 3 ,
266
+ quoteWrap,
267
+ ),
268
+ );
269
+ } else {
270
+ var value = quoteWrap ? '"${entry .value }"' : entry.value;
271
+ newDefs.add (' ${entry .key }: &${entry .key } $value ' );
148
272
}
273
+ } else {
274
+ var value = quoteWrap ? '"${entry .value }"' : entry.value;
275
+ updateLineIfNotEqual (
276
+ oldLine: line,
277
+ newLine: ' ${entry .key }: &${entry .key } $value ' ,
278
+ );
149
279
}
280
+ }
150
281
151
- var newDoc = lines.join ('\n ' );
152
-
153
- try {
154
- var parsed = YamlEditor (newDoc).toString ();
155
- file.writeAsStringSync (parsed, flush: true );
156
- } catch (e) {
157
- file.writeAsStringSync (newDoc, flush: true );
282
+ if (newDefs.isNotEmpty) {
283
+ lines.insertAll (nodeStartIdx, newDefs);
284
+ for (var def in newDefs) {
285
+ print ('Insert ${node .capitalized } Def -> $def ' );
158
286
}
159
287
}
288
+
289
+ var newDoc = lines.join ('\n ' );
290
+
291
+ try {
292
+ var parsed = YamlEditor (newDoc).toString ();
293
+ file.writeAsStringSync (parsed, flush: true );
294
+ } catch (e) {
295
+ file.writeAsStringSync (newDoc, flush: true );
296
+ }
160
297
}
161
298
162
299
Future <void > configure ({
@@ -270,7 +407,7 @@ Future<void> generateConfigurations({
270
407
var builtContent = await () async {
271
408
try {
272
409
return DartFormatter ().format (await result.write ());
273
- } catch (e) {
410
+ } catch (e) {
274
411
print (e);
275
412
return await result.write ();
276
413
}
0 commit comments