Skip to content

Commit 166a166

Browse files
ci: bump version to v0.14.0
1 parent 3fdceba commit 166a166

File tree

2 files changed

+152
-114
lines changed

2 files changed

+152
-114
lines changed

lib/src/generate_dart_models.dart

Lines changed: 148 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,13 @@ Future<void> generateDartModels(
7777
exit(ExitCodes.FAILURE.code);
7878
}
7979
final analysisContextCollection = createDartAnalysisContextCollection(
80-
{
81-
inputPath,
82-
},
80+
{inputPath},
8381
dartSdk, //
8482
);
8583
_print(Log.printWhite, 'Reading template at: $templatePathOrUrl...');
86-
final result = await MdTemplateUtility.i.readTemplateFromPathOrUrl(templatePathOrUrl).value;
84+
final result = await MdTemplateUtility.i
85+
.readTemplateFromPathOrUrl(templatePathOrUrl)
86+
.value;
8787
if (result.isErr()) {
8888
_print(Log.printRed, ' Failed to read template!');
8989
exit(ExitCodes.FAILURE.code);
@@ -155,70 +155,86 @@ bool _isAllowedFileName(String e) {
155155

156156
final _interpolator = TemplateInterpolator<ClassInsight<GenerateDartModel>>({
157157
'___DESCRIPTION___': (insight) {
158-
return insight.annotation.description ?? 'Generated class for [${insight.className}].';
158+
return insight.annotation.description ??
159+
'Generated class for [${insight.className}].';
159160
},
160161
'___SUPER_CLASS_NAME___': (insight) {
161-
return insight.annotation.shouldInherit == true ? insight.className : 'Model';
162+
return insight.annotation.shouldInherit == true
163+
? insight.className
164+
: 'Model';
162165
},
163166
'___CLASS_FILE_NAME___': (insight) {
164167
return insight.fileName;
165168
},
166169
'___CLASS_NAME___': (insight) {
167-
return insight.annotation.className ?? insight.className.replaceFirst(RegExp(r'^[_$]+'), '');
170+
return insight.annotation.className ??
171+
insight.className.replaceFirst(RegExp(r'^[_$]+'), '');
168172
},
169173
'___SUPER_CONSTRUCTOR___': (insight) {
170174
return insight.annotation.shouldInherit == true
171175
? insight.annotation.inheritanceConstructor?.nullIfEmpty != null
172-
? ': super.${insight.annotation.inheritanceConstructor}()'
173-
: ''
176+
? ': super.${insight.annotation.inheritanceConstructor}()'
177+
: ''
174178
: '';
175179
},
176180
'___FIELD_DECLARATIONS___': (insight) {
177-
return insight.fields.map((e) {
178-
final description = e.description;
179-
final t = e.fieldType!;
180-
final r = stripSpecialSyntaxFromFieldType(t);
181-
final f = e.fieldName;
182-
return [
183-
' /// ${description ?? 'No description provided.'}',
184-
'final $r? $f;',
185-
'',
186-
].join('\n');
187-
}).join('\n');
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');
188194
},
189195
'___PARAMS___': (insight) {
190-
return insight.fields.map((e) {
191-
final n = e.nullable;
192-
final f = e.fieldName;
193-
return '${n ? '' : 'required'} this.$f,';
194-
}).join('\n');
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');
195203
},
196204
'___PERMISSIVE_PARAMS___': (insight) {
197-
return insight.fields.map((e) {
198-
final f = e.fieldName;
199-
return 'this.$f,';
200-
}).join('\n');
205+
return insight.fields
206+
.map((e) {
207+
final f = e.fieldName;
208+
return 'this.$f,';
209+
})
210+
.join('\n');
201211
},
202212
'___STRICT_PARAMS___': (insight) {
203-
return insight.fields.map((e) {
204-
final t = e.fieldType!;
205-
final r = stripSpecialSyntaxFromFieldType(t);
206-
final f = e.fieldName;
207-
return '$r? $f,';
208-
}).join('\n');
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');
209221
},
210222
'___STRICT_PARAM_ASSERTIONS___': (insight) {
211-
return insight.fields.map((e) {
212-
final n = e.nullable;
213-
final f = e.fieldName;
214-
return n ? '' : 'assert($f != null);';
215-
}).join('\n');
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');
216230
},
217231
'___STRICT_ARGS___': (insight) {
218-
return insight.fields.map((e) {
219-
final f = e.fieldName;
220-
return '$f: $f,';
221-
}).join('\n');
232+
return insight.fields
233+
.map((e) {
234+
final f = e.fieldName;
235+
return '$f: $f,';
236+
})
237+
.join('\n');
222238
},
223239
'___FROM_JSON_OR_NULL_PARAMS___': (insight) {
224240
final fields = insight.fields.toList();
@@ -238,7 +254,8 @@ final _interpolator = TemplateInterpolator<ClassInsight<GenerateDartModel>>({
238254
final j = fields.map((a) {
239255
final ff = fields
240256
.where(
241-
(b) => a.fieldPath!.join('.').startsWith('${b.fieldPath!.join('.')}.'),
257+
(b) =>
258+
a.fieldPath!.join('.').startsWith('${b.fieldPath!.join('.')}.'),
242259
)
243260
.toList();
244261
ff.sort((a, b) => b.fieldName!.compareTo(a.fieldName!));
@@ -247,22 +264,26 @@ final _interpolator = TemplateInterpolator<ClassInsight<GenerateDartModel>>({
247264
return j.join('\n');
248265
},
249266
'___FROM_JSON_OR_NULL_ARGS___': (insight) {
250-
return insight.fields.map((e) {
251-
final f = e.fieldName;
252-
return '$f: $f,';
253-
}).join('\n');
267+
return insight.fields
268+
.map((e) {
269+
final f = e.fieldName;
270+
return '$f: $f,';
271+
})
272+
.join('\n');
254273
},
255274
'___TO_JSON_PARAMS___': (insight) {
256-
return insight.fields.map((e) {
257-
final f = e.fieldName!;
258-
final f0 = '${f}0';
259-
final x = e.fieldTypeCode!;
260-
final s = stripSpecialSyntaxFromFieldType(x);
261-
final a = DartTypeCodeMapper(
262-
DartLooseTypeMappers.instance.toMappers,
263-
).map(fieldName: f, fieldTypeCode: s);
264-
return 'final $f0 = $a;';
265-
}).join('\n');
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');
266287
},
267288
'___TO_JSON_ARGS___': (insight) {
268289
final fields = insight.fields.toList();
@@ -277,15 +298,16 @@ final _interpolator = TemplateInterpolator<ClassInsight<GenerateDartModel>>({
277298
.toList();
278299
fields.removeWhere((e) => parents.contains(e));
279300
final stringCaseType = insight.stringCaseType;
280-
final entries = fields
281-
.map(
282-
(e) => MapEntry(
283-
stringCaseType.convertAll(e.fieldPath!).join('.'),
284-
'${e.fieldName}0',
285-
),
286-
)
287-
.toList()
288-
..sort((a, b) => b.key.compareTo(a.key));
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));
289311

290312
var buffer = <String, dynamic>{};
291313

@@ -313,59 +335,72 @@ final _interpolator = TemplateInterpolator<ClassInsight<GenerateDartModel>>({
313335
return test.replaceAll('#:', '');
314336
},
315337
'___GETTERS___': (insight) {
316-
return insight.fields.map((e) {
317-
final f = e.fieldName;
318-
final x = e.fieldTypeCode!;
319-
final s = stripSpecialSyntaxFromFieldType(x);
320-
final n = e.nullable;
321-
return [
322-
' /// Returns the value of the [$f] field.',
323-
' /// If the field is nullable, the return value may be null; otherwise, it',
324-
' /// will always return a non-null value.',
325-
"@pragma('vm:prefer-inline')",
326-
'$s get $f\$ => $f${n ? '' : '!'};',
327-
'',
328-
].join('\n');
329-
}).join('\n');
338+
return insight.fields
339+
.map((e) {
340+
final f = e.fieldName;
341+
final x = e.fieldTypeCode!;
342+
final s = stripSpecialSyntaxFromFieldType(x);
343+
final n = e.nullable;
344+
return [
345+
' /// Returns the value of the [$f] field.',
346+
' /// If the field is nullable, the return value may be null; otherwise, it',
347+
' /// will always return a non-null value.',
348+
"@pragma('vm:prefer-inline')",
349+
'$s get $f\$ => $f${n ? '' : '!'};',
350+
'',
351+
].join('\n');
352+
})
353+
.join('\n');
330354
},
331355
'___FIELD_NAMES___': (insight) {
332-
return insight.fields.map((e) {
333-
final className =
334-
insight.annotation.className ?? insight.className.replaceFirst(RegExp(r'^[_$]+'), '');
335-
final f = e.fieldName;
336-
final c = insight.stringCaseType.convert(e.fieldName!);
337-
return [
338-
' /// The field name of [$className.$f].',
339-
"static const $f = '$c';",
340-
'',
341-
].join('\n');
342-
}).join('\n');
356+
return insight.fields
357+
.map((e) {
358+
final className =
359+
insight.annotation.className ??
360+
insight.className.replaceFirst(RegExp(r'^[_$]+'), '');
361+
final f = e.fieldName;
362+
final c = insight.stringCaseType.convert(e.fieldName!);
363+
return [
364+
' /// The field name of [$className.$f].',
365+
"static const $f = '$c';",
366+
'',
367+
].join('\n');
368+
})
369+
.join('\n');
343370
},
344371
'___COPY_WITH_PARAMS___': (insight) {
345-
return insight.fields.map((e) {
346-
final f = e.fieldName;
347-
final t = e.fieldType!;
348-
final r = stripSpecialSyntaxFromFieldType(t);
349-
return '$r? $f,';
350-
}).join('\n');
372+
return insight.fields
373+
.map((e) {
374+
final f = e.fieldName;
375+
final t = e.fieldType!;
376+
final r = stripSpecialSyntaxFromFieldType(t);
377+
return '$r? $f,';
378+
})
379+
.join('\n');
351380
},
352381
'___COPY_WITH_ARGS___': (insight) {
353-
return insight.fields.map((e) {
354-
final f = e.fieldName;
355-
return '$f: $f ?? this.$f,';
356-
}).join('\n');
382+
return insight.fields
383+
.map((e) {
384+
final f = e.fieldName;
385+
return '$f: $f ?? this.$f,';
386+
})
387+
.join('\n');
357388
},
358389
'___COPY_WITHOUT_PARAMS___': (insight) {
359-
return insight.fields.map((e) {
360-
final f = e.fieldName;
361-
return 'bool $f = true,';
362-
}).join('\n');
390+
return insight.fields
391+
.map((e) {
392+
final f = e.fieldName;
393+
return 'bool $f = true,';
394+
})
395+
.join('\n');
363396
},
364397
'___COPY_WITHOUT_ARGS___': (insight) {
365-
return insight.fields.map((e) {
366-
final f = e.fieldName;
367-
return '$f: $f ? this.$f: null,';
368-
}).join('\n');
398+
return insight.fields
399+
.map((e) {
400+
final f = e.fieldName;
401+
return '$f: $f ? this.$f: null,';
402+
})
403+
.join('\n');
369404
},
370405
});
371406

@@ -379,6 +414,7 @@ extension _ClassInsightExtension on ClassInsight<GenerateDartModel> {
379414
}
380415

381416
StringCaseType get stringCaseType {
382-
return StringCaseType.values.valueOf(annotation.keyStringCase) ?? StringCaseType.CAMEL_CASE;
417+
return StringCaseType.values.valueOf(annotation.keyStringCase) ??
418+
StringCaseType.CAMEL_CASE;
383419
}
384420
}

lib/src/generate_dart_models_gemeni.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ Future<void> generateDartModelsGemeni(List<String> args) async {
3030
);
3131
final GEMENI_API_KEY = const OptionParam(
3232
name: 'api-key',
33-
help: 'Get your Gemeni API key here https://ai.google.dev/gemini-api/docs/api-key.',
33+
help:
34+
'Get your Gemeni API key here https://ai.google.dev/gemini-api/docs/api-key.',
3435
);
3536
final GEMENI_MODEL = const OptionParam(
3637
name: 'model',
@@ -44,7 +45,8 @@ Future<void> generateDartModelsGemeni(List<String> args) async {
4445
);
4546
final LANG = const OptionParam(
4647
name: 'lang',
47-
help: 'The programming language to generate the data model for, e.g. "dart" or "ts"',
48+
help:
49+
'The programming language to generate the data model for, e.g. "dart" or "ts"',
4850
defaultsTo: 'ts',
4951
);
5052
final parser = CliParser(

0 commit comments

Comments
 (0)