Skip to content

Commit 3ce2b12

Browse files
committed
update
1 parent 5393744 commit 3ce2b12

File tree

1 file changed

+125
-156
lines changed

1 file changed

+125
-156
lines changed

lib/src/gen_models_app.dart

Lines changed: 125 additions & 156 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,14 @@ Future<void> genModelsApp(
7777
);
7878
exit(ExitCodes.FAILURE.code);
7979
}
80-
final analysisContextCollection = createDartAnalysisContextCollection({
81-
inputPath,
82-
}, dartSdk);
80+
final analysisContextCollection = createDartAnalysisContextCollection(
81+
{
82+
inputPath,
83+
},
84+
dartSdk,
85+
);
8386
_print(Log.printWhite, 'Reading template at: $templatePathOrUrl...');
84-
final result = await MdTemplateUtility.i
85-
.readTemplateFromPathOrUrl(templatePathOrUrl)
86-
.value;
87+
final result = await MdTemplateUtility.i.readTemplateFromPathOrUrl(templatePathOrUrl).value;
8788
if (result.isErr()) {
8889
_print(Log.printRed, ' Failed to read template!');
8990
exit(ExitCodes.FAILURE.code);
@@ -155,86 +156,70 @@ bool _isAllowedFileName(String e) {
155156

156157
final _interpolator = TemplateInterpolator<ClassInsight<GenerateDartModel>>({
157158
'___DESCRIPTION___': (insight) {
158-
return insight.annotation.description ??
159-
'Generated class for [${insight.className}].';
159+
return insight.annotation.description ?? 'Generated class for [${insight.className}].';
160160
},
161161
'___SUPER_CLASS_NAME___': (insight) {
162-
return insight.annotation.shouldInherit == true
163-
? insight.className
164-
: 'Model';
162+
return insight.annotation.shouldInherit == true ? insight.className : 'Model';
165163
},
166164
'___CLASS_FILE_NAME___': (insight) {
167165
return insight.fileName;
168166
},
169167
'___CLASS_NAME___': (insight) {
170-
return insight.annotation.className ??
171-
insight.className.replaceFirst(RegExp(r'^[_$]+'), '');
168+
return insight.annotation.className ?? insight.className.replaceFirst(RegExp(r'^[_$]+'), '');
172169
},
173170
'___SUPER_CONSTRUCTOR___': (insight) {
174171
return insight.annotation.shouldInherit == true
175172
? insight.annotation.inheritanceConstructor?.nullIfEmpty != null
176-
? ': super.${insight.annotation.inheritanceConstructor}()'
177-
: ''
173+
? ': super.${insight.annotation.inheritanceConstructor}()'
174+
: ''
178175
: '';
179176
},
180177
'___FIELD_DECLARATIONS___': (insight) {
181-
return insight.fields
182-
.map((e) {
183-
final description = e.description;
184-
final t = e.fieldType!;
185-
final r = stripSpecialSyntaxFromFieldType(t);
186-
final f = e.fieldName;
187-
return [
188-
' /// ${description ?? 'No description provided.'}',
189-
'final $r? $f;',
190-
'',
191-
].join('\n');
192-
})
193-
.join('\n');
178+
return insight.fields.map((e) {
179+
final description = e.description;
180+
final t = e.fieldType!;
181+
final r = stripSpecialSyntaxFromFieldType(t);
182+
final f = e.fieldName;
183+
return [
184+
' /// ${description ?? 'No description provided.'}',
185+
'final $r? $f;',
186+
'',
187+
].join('\n');
188+
}).join('\n');
194189
},
195190
'___PARAMS___': (insight) {
196-
return insight.fields
197-
.map((e) {
198-
final n = e.nullable;
199-
final f = e.fieldName;
200-
return '${n ? '' : 'required'} this.$f,';
201-
})
202-
.join('\n');
191+
return insight.fields.map((e) {
192+
final n = e.nullable;
193+
final f = e.fieldName;
194+
return '${n ? '' : 'required'} this.$f,';
195+
}).join('\n');
203196
},
204197
'___PERMISSIVE_PARAMS___': (insight) {
205-
return insight.fields
206-
.map((e) {
207-
final f = e.fieldName;
208-
return 'this.$f,';
209-
})
210-
.join('\n');
198+
return insight.fields.map((e) {
199+
final f = e.fieldName;
200+
return 'this.$f,';
201+
}).join('\n');
211202
},
212203
'___STRICT_PARAMS___': (insight) {
213-
return insight.fields
214-
.map((e) {
215-
final t = e.fieldType!;
216-
final r = stripSpecialSyntaxFromFieldType(t);
217-
final f = e.fieldName;
218-
return '$r? $f,';
219-
})
220-
.join('\n');
204+
return insight.fields.map((e) {
205+
final t = e.fieldType!;
206+
final r = stripSpecialSyntaxFromFieldType(t);
207+
final f = e.fieldName;
208+
return '$r? $f,';
209+
}).join('\n');
221210
},
222211
'___STRICT_PARAM_ASSERTIONS___': (insight) {
223-
return insight.fields
224-
.map((e) {
225-
final n = e.nullable;
226-
final f = e.fieldName;
227-
return n ? '' : 'assert($f != null);';
228-
})
229-
.join('\n');
212+
return insight.fields.map((e) {
213+
final n = e.nullable;
214+
final f = e.fieldName;
215+
return n ? '' : 'assert($f != null);';
216+
}).join('\n');
230217
},
231218
'___STRICT_ARGS___': (insight) {
232-
return insight.fields
233-
.map((e) {
234-
final f = e.fieldName;
235-
return '$f: $f,';
236-
})
237-
.join('\n');
219+
return insight.fields.map((e) {
220+
final f = e.fieldName;
221+
return '$f: $f,';
222+
}).join('\n');
238223
},
239224
'___FROM_JSON_OR_NULL_PARAMS___': (insight) {
240225
final fields = insight.fields.toList();
@@ -254,8 +239,7 @@ final _interpolator = TemplateInterpolator<ClassInsight<GenerateDartModel>>({
254239
final j = fields.map((a) {
255240
final ff = fields
256241
.where(
257-
(b) =>
258-
a.fieldPath!.join('.').startsWith('${b.fieldPath!.join('.')}.'),
242+
(b) => a.fieldPath!.join('.').startsWith('${b.fieldPath!.join('.')}.'),
259243
)
260244
.toList();
261245
ff.sort((a, b) => b.fieldName!.compareTo(a.fieldName!));
@@ -264,26 +248,22 @@ final _interpolator = TemplateInterpolator<ClassInsight<GenerateDartModel>>({
264248
return j.join('\n');
265249
},
266250
'___FROM_JSON_OR_NULL_ARGS___': (insight) {
267-
return insight.fields
268-
.map((e) {
269-
final f = e.fieldName;
270-
return '$f: $f,';
271-
})
272-
.join('\n');
251+
return insight.fields.map((e) {
252+
final f = e.fieldName;
253+
return '$f: $f,';
254+
}).join('\n');
273255
},
274256
'___TO_JSON_PARAMS___': (insight) {
275-
return insight.fields
276-
.map((e) {
277-
final f = e.fieldName!;
278-
final f0 = '${f}0';
279-
final x = e.fieldTypeCode!;
280-
final s = stripSpecialSyntaxFromFieldType(x);
281-
final a = DartTypeCodeMapper(
282-
DartLooseTypeMappers.instance.toMappers,
283-
).map(fieldName: f, fieldTypeCode: s);
284-
return 'final $f0 = $a;';
285-
})
286-
.join('\n');
257+
return insight.fields.map((e) {
258+
final f = e.fieldName!;
259+
final f0 = '${f}0';
260+
final x = e.fieldTypeCode!;
261+
final s = stripSpecialSyntaxFromFieldType(x);
262+
final a = DartTypeCodeMapper(
263+
DartLooseTypeMappers.instance.toMappers,
264+
).map(fieldName: f, fieldTypeCode: s);
265+
return 'final $f0 = $a;';
266+
}).join('\n');
287267
},
288268
'___TO_JSON_ARGS___': (insight) {
289269
final fields = insight.fields.toList();
@@ -298,16 +278,15 @@ final _interpolator = TemplateInterpolator<ClassInsight<GenerateDartModel>>({
298278
.toList();
299279
fields.removeWhere((e) => parents.contains(e));
300280
final stringCaseType = insight.stringCaseType;
301-
final entries =
302-
fields
303-
.map(
304-
(e) => MapEntry(
305-
stringCaseType.convertAll(e.fieldPath!).join('.'),
306-
'${e.fieldName}0',
307-
),
308-
)
309-
.toList()
310-
..sort((a, b) => b.key.compareTo(a.key));
281+
final entries = fields
282+
.map(
283+
(e) => MapEntry(
284+
stringCaseType.convertAll(e.fieldPath!).join('.'),
285+
'${e.fieldName}0',
286+
),
287+
)
288+
.toList()
289+
..sort((a, b) => b.key.compareTo(a.key));
311290

312291
var buffer = <String, dynamic>{};
313292

@@ -320,83 +299,74 @@ final _interpolator = TemplateInterpolator<ClassInsight<GenerateDartModel>>({
320299
}
321300

322301
for (final parent in parents) {
323-
traverseMap(buffer, [
324-
...parent.fieldPath!.map(
325-
(e) => "'${insight.stringCaseType.convert(e)}'",
326-
),
327-
'#',
328-
], newValue: '...?${parent.fieldName}0,');
302+
traverseMap(
303+
buffer,
304+
[
305+
...parent.fieldPath!.map(
306+
(e) => "'${insight.stringCaseType.convert(e)}'",
307+
),
308+
'#',
309+
],
310+
newValue: '...?${parent.fieldName}0,',
311+
);
329312
}
330313
final test = buffer.entries.map((e) => '${e.key}: ${e.value},').join();
331314
return test.replaceAll('#:', '');
332315
},
333316
'___GETTERS___': (insight) {
334-
return insight.fields
335-
.map((e) {
336-
final f = e.fieldName;
337-
final x = e.fieldTypeCode!;
338-
final s = stripSpecialSyntaxFromFieldType(x);
339-
final n = e.nullable;
340-
return [
341-
' /// Returns the value of the [$f] field.',
342-
' /// If the field is nullable, the return value may be null; otherwise, it',
343-
' /// will always return a non-null value.',
344-
"@pragma('vm:prefer-inline')",
345-
'$s get $f\$ => $f${n ? '' : '!'};',
346-
'',
347-
].join('\n');
348-
})
349-
.join('\n');
317+
return insight.fields.map((e) {
318+
final f = e.fieldName;
319+
final x = e.fieldTypeCode!;
320+
final s = stripSpecialSyntaxFromFieldType(x);
321+
final n = e.nullable;
322+
return [
323+
' /// Returns the value of the [$f] field.',
324+
' /// If the field is nullable, the return value may be null; otherwise, it',
325+
' /// will always return a non-null value.',
326+
"@pragma('vm:prefer-inline')",
327+
'$s get $f\$ => $f${n ? '' : '!'};',
328+
'',
329+
].join('\n');
330+
}).join('\n');
350331
},
351332
'___FIELD_NAMES___': (insight) {
352-
return insight.fields
353-
.map((e) {
354-
final className =
355-
insight.annotation.className ??
356-
insight.className.replaceFirst(RegExp(r'^[_$]+'), '');
357-
final f = e.fieldName;
358-
final c = insight.stringCaseType.convert(e.fieldName!);
359-
return [
360-
' /// The field name of [$className.$f].',
361-
"static const $f = '$c';",
362-
'',
363-
].join('\n');
364-
})
365-
.join('\n');
333+
return insight.fields.map((e) {
334+
final className =
335+
insight.annotation.className ?? insight.className.replaceFirst(RegExp(r'^[_$]+'), '');
336+
final f = e.fieldName;
337+
final c = insight.stringCaseType.convert(e.fieldName!);
338+
return [
339+
' /// The field name of [$className.$f].',
340+
"static const $f = '$c';",
341+
'',
342+
].join('\n');
343+
}).join('\n');
366344
},
367345
'___COPY_WITH_PARAMS___': (insight) {
368-
return insight.fields
369-
.map((e) {
370-
final f = e.fieldName;
371-
final t = e.fieldType!;
372-
final r = stripSpecialSyntaxFromFieldType(t);
373-
return '$r? $f,';
374-
})
375-
.join('\n');
346+
return insight.fields.map((e) {
347+
final f = e.fieldName;
348+
final t = e.fieldType!;
349+
final r = stripSpecialSyntaxFromFieldType(t);
350+
return '$r? $f,';
351+
}).join('\n');
376352
},
377353
'___COPY_WITH_ARGS___': (insight) {
378-
return insight.fields
379-
.map((e) {
380-
final f = e.fieldName;
381-
return '$f: $f ?? this.$f,';
382-
})
383-
.join('\n');
354+
return insight.fields.map((e) {
355+
final f = e.fieldName;
356+
return '$f: $f ?? this.$f,';
357+
}).join('\n');
384358
},
385359
'___COPY_WITHOUT_PARAMS___': (insight) {
386-
return insight.fields
387-
.map((e) {
388-
final f = e.fieldName;
389-
return 'bool $f = true,';
390-
})
391-
.join('\n');
360+
return insight.fields.map((e) {
361+
final f = e.fieldName;
362+
return 'bool $f = true,';
363+
}).join('\n');
392364
},
393365
'___COPY_WITHOUT_ARGS___': (insight) {
394-
return insight.fields
395-
.map((e) {
396-
final f = e.fieldName;
397-
return '$f: $f ? this.$f: null,';
398-
})
399-
.join('\n');
366+
return insight.fields.map((e) {
367+
final f = e.fieldName;
368+
return '$f: $f ? this.$f: null,';
369+
}).join('\n');
400370
},
401371
});
402372

@@ -410,7 +380,6 @@ extension _ClassInsightExtension on ClassInsight<GenerateDartModel> {
410380
}
411381

412382
StringCaseType get stringCaseType {
413-
return StringCaseType.values.valueOf(annotation.keyStringCase) ??
414-
StringCaseType.CAMEL_CASE;
383+
return StringCaseType.values.valueOf(annotation.keyStringCase) ?? StringCaseType.CAMEL_CASE;
415384
}
416385
}

0 commit comments

Comments
 (0)