Skip to content

Commit 0a93742

Browse files
author
cgiuliani
committed
feat: allow size and flag defs
1 parent be030ba commit 0a93742

File tree

3 files changed

+179
-42
lines changed

3 files changed

+179
-42
lines changed

Diff for: configurator/bin/configurator.dart

+176-39
Original file line numberDiff line numberDiff line change
@@ -93,70 +93,207 @@ Future<void> applyDefinitions({
9393
final YamlNode rootNode = document.contents;
9494

9595
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'];
9799

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 ?? {});
99103

100104
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+
}
102136

103-
List<String> lines = contents.split('\n');
137+
var nodeStartIdx = lookupNodeIdx + 1;
138+
var nodeEndIdx = nodeStartIdx;
104139

105-
var colorNodeIdx = lines.indexWhere((l) => l.contains('colors:'));
140+
var searchIdx = nodeStartIdx + 1;
106141

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;
109162
}
110163

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);
113174

114-
var searchIdx = colorStartIdx + 1;
175+
if (lineIdx > -1) {
176+
var oldLine = lines[lineIdx];
115177

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+
}
119184
}
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);
120199

121-
List<String> newDefs = [];
200+
if (entry.value is Map) {
201+
var padding = List.filled(2 * depth, ' ').join();
122202

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}');
125204

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+
);
128220
} 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}');
130225

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';
134228

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+
);
139236
}
140237
}
141238
}
142239

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');
148272
}
273+
} else {
274+
var value = quoteWrap ? '"${entry.value}"' : entry.value;
275+
updateLineIfNotEqual(
276+
oldLine: line,
277+
newLine: ' ${entry.key}: &${entry.key} $value',
278+
);
149279
}
280+
}
150281

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');
158286
}
159287
}
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+
}
160297
}
161298

162299
Future<void> configure({
@@ -270,7 +407,7 @@ Future<void> generateConfigurations({
270407
var builtContent = await () async {
271408
try {
272409
return DartFormatter().format(await result.write());
273-
} catch(e) {
410+
} catch (e) {
274411
print(e);
275412
return await result.write();
276413
}

Diff for: configurator/pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: configurator
22
description: A new Flutter package project.
3-
version: 0.0.25
3+
version: 0.0.26
44
homepage:
55

66
environment:

Diff for: configurator_flutter/pubspec.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: configurator_flutter
22
description: A new Flutter package project.
3-
version: 0.0.25
3+
version: 0.0.26
44
homepage:
55

66
environment:
@@ -29,7 +29,7 @@ dependencies:
2929
git:
3030
url: [email protected]:camrongiuliani/configurator.git
3131
path: configurator
32-
ref: 0.0.25
32+
ref: 0.0.26
3333

3434
dev_dependencies:
3535
flutter_test:

0 commit comments

Comments
 (0)