Skip to content

Commit

Permalink
breaking: Improve generator output
Browse files Browse the repository at this point in the history
  • Loading branch information
robmllze committed Nov 11, 2024
1 parent b80f878 commit bab8870
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 34 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## [0.8.0]

- Released @ 10/2024 (UTC)
- breaking: Improve generator output

## [0.7.3]

- Released @ 10/2024 (UTC)
Expand Down
10 changes: 4 additions & 6 deletions lib/src/_core_utils/generator_converger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,14 @@ final generatorConverger = _GeneratorConverger(
// Determine the output file name.
final outputFileName = [
replacement.insight.className.toLowerSnakeCase(),
Lang.DART.genExt,
CommonLang.DART.genExt,
].join();

// Determine the output file path.
final outputFilePath =
p.join(replacement.insight.dirPath, outputFileName);
final outputFilePath = p.join(replacement.insight.dirPath, outputFileName);

// Write the generated Dart file.
await writeFile(outputFilePath, output);
FileSystemUtility.i.writeLocalFile(outputFilePath, output);

// Fix the generated Dart file.
await fixDartFile(outputFilePath);
Expand All @@ -56,5 +55,4 @@ final generatorConverger = _GeneratorConverger(

// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░

typedef _GeneratorConverger
= GeneratorConverger<ClassInsight<GenerateDartModel>, Enum, String>;
typedef _GeneratorConverger = GeneratorConverger<ClassInsight<GenerateDartModel>, Enum, String>;
11 changes: 4 additions & 7 deletions lib/src/_dart_utils/dart_loose_type_mappers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,16 @@ class DartLooseTypeMappers extends TypeMappers {
// Standard.
// ---------------------------------------------------------------------
r'^(Map)\??$': (e) {
return 'letMapOrNull<dynamic, dynamic>(${e.name})?.map((${e.args}) => MapEntry(${e.hashes},),).nonNulls.nullIfEmpty';
return 'letMapOrNull<dynamic, dynamic>(${e.name})?.map((${e.args}) => MapEntry(${e.hashes},),).nonNulls.nullIfEmpty?.unmodifiable';
},
r'^(Iterable)\??$': (e) {
return 'letIterableOrNull<dynamic>(${e.name})?.map((${e.args}) => ${e.hashes},).nonNulls.nullIfEmpty';
},
r'^(List)\??$': (e) {
return 'letListOrNull<dynamic>(${e.name})?.map((${e.args}) => ${e.hashes},).nonNulls.nullIfEmpty?.toList()';
return 'letListOrNull<dynamic>(${e.name})?.map((${e.args}) => ${e.hashes},).nonNulls.nullIfEmpty?.toList().unmodifiable';
},
r'^(Set)\??$': (e) {
return 'letSetOrNull<dynamic>(${e.name})?.map((${e.args}) => ${e.hashes},).nonNulls.nullIfEmpty?.toSet()';
},
r'^(Queue)\??$': (e) {
return '(){ final a = letIterableOrNull<dynamic>(${e.name})?.map((${e.args}) => ${e.hashes},).nonNulls.nullIfEmpty; return a != null ? Queue.of(a): null; }()';
return 'letSetOrNull<dynamic>(${e.name})?.map((${e.args}) => ${e.hashes},).nonNulls.nullIfEmpty?.toSet().unmodifiable';
},
});

Expand All @@ -65,7 +62,7 @@ class DartLooseTypeMappers extends TypeMappers {
// Standard.
// ---------------------------------------------------------------------
r'^(Map)\??$': (e) {
return '${e.name}?.map((${e.args}) => MapEntry(${e.hashes},),).nonNulls.nullIfEmpty';
return '${e.name}?.map((${e.args}) => MapEntry(${e.hashes},),).nonNulls.nullIfEmpty?';
},
r'^(Iterable|List|Set|Queue)\??$': (e) {
return '${e.name}?.map((${e.args}) => ${e.hashes},).nonNulls.nullIfEmpty?.toList()';
Expand Down
11 changes: 4 additions & 7 deletions lib/src/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ import 'generate.dart';
Future<void> runGenerateDartModelsApp(List<String> args) async {
await runCommandLineApp(
title: 'Generate Dart Models',
description:
'A command line app for generating Dart models from annotations',
description: 'A command line app for generating Dart models from annotations',
args: args,
parser: ArgParser()
..addFlag(
Expand Down Expand Up @@ -60,8 +59,7 @@ Future<void> runGenerateDartModelsApp(List<String> args) async {
)
..addOption(
'gemeni_api_key',
help:
'Obtain your API key here https://ai.google.dev/gemini-api/docs/api-key.',
help: 'Obtain your API key here https://ai.google.dev/gemini-api/docs/api-key.',
)
..addOption(
'gemeni_model',
Expand All @@ -74,8 +72,7 @@ Future<void> runGenerateDartModelsApp(List<String> args) async {
)
..addOption(
'generate_for_languages',
help:
'Programming languages to generate additional models for using AI, separated by `&`.',
help: 'Programming languages to generate additional models for using AI, separated by `&`.',
)
..addOption(
'ai_output',
Expand Down Expand Up @@ -109,7 +106,7 @@ Future<void> runGenerateDartModelsApp(List<String> args) async {
aiOutput: args.aiOutput!,
rootDirPaths: args.rootPaths!,
subDirPaths: args.subPaths ?? const {},
pathPatterns: args.pathPatterns ?? const {},
matchPatterns: args.pathPatterns ?? const {},
templateFilePath: args.templateFilePath!,
generateForLanguages: args.generateForLanguages ?? const {},
);
Expand Down
14 changes: 7 additions & 7 deletions lib/src/generate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import 'generate_ai_models.dart';
Future<void> generateDartModelsFromAnnotations({
required Set<String> rootDirPaths,
Set<String> subDirPaths = const {},
Set<String> pathPatterns = const {},
Set<String> matchPatterns = const {},
required String templateFilePath,
String? fallbackDartSdkPath,
String? gemeniApiKey,
Expand All @@ -44,34 +44,34 @@ Future<void> generateDartModelsFromAnnotations({
),
],
dirPathGroups: {
CombinedPaths(
MatchedPathPowerset(
rootDirPaths,
subPaths: subDirPaths,
pathPatterns: pathPatterns,
matchPatterns: matchPatterns,
),
},
);
final sourceFileExplorerResults = await sourceFileExporer.explore();

final template = templateFilePath.isNotEmpty
? extractCodeFromMarkdown(
await loadFileFromPathOrUrl(templateFilePath),
(await FileSystemUtility.i.readFileFromPathOrUrl(templateFilePath))!,
).trim()
: '';

// ---------------------------------------------------------------------------

// Create context for the Dart analyzer.
final analysisContextCollection = createDartAnalysisContextCollection(
sourceFileExporer.dirPathGroups.first.paths,
sourceFileExporer.dirPathGroups.first.output,
fallbackDartSdkPath,
);

final sequentual = Sequential();

// For each file...
for (final filePathResult in sourceFileExplorerResults.filePathResults
.where((e) => e.category == _Categories.DART)) {
for (final filePathResult
in sourceFileExplorerResults.filePathResults.where((e) => e.category == _Categories.DART)) {
final filePath = filePathResult.path;

// Extract insights from the file.
Expand Down
2 changes: 1 addition & 1 deletion lib/src/generate_ai_models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Future<void> generateAIModels({
fileName,
);

await writeFile(filePath, output);
await FileSystemUtility.i.writeLocalFile(filePath, output);

// Log a success.
printGreen(
Expand Down
13 changes: 7 additions & 6 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

name: df_generate_dart_models
description: A tool to generate index/exports files for all Dart files in a directory.
version: 0.7.3
version: 0.8.0
repository: https://github.com/robmllze/df_generate_dart_models
funding:
- https://www.buymeacoffee.com/robmllze
Expand All @@ -31,13 +31,13 @@ environment:
## -----------------------------------------------------------------------------

dependencies:
df_collection: ^0.6.0
df_config: ^0.5.0
df_gen_core: ^0.3.2
df_generate_dart_models_core: ^0.6.4
df_collection: ^0.8.0
df_config: ^0.5.1
df_gen_core: ^0.4.1
df_generate_dart_models_core: ^0.6.8
df_log: ^0.2.4
df_string: ^0.2.2
df_type: ^0.6.1
df_type: ^0.6.2

analyzer: ^6.8.0
args: ^2.5.0
Expand All @@ -48,6 +48,7 @@ dependencies:

dev_dependencies:
lints: ^5.0.0
test: ^1.25.8

## -----------------------------------------------------------------------------

Expand Down
11 changes: 11 additions & 0 deletions test/test_decompose_dart_collection_type.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import 'package:df_generate_dart_models/src/_dart_utils/decompose_dart_collection_type.dart';
import 'package:test/test.dart';

void main() {
group('decomposeDartCollectionType', () {
test('1', () {
final result = decomposeDartCollectionType('List<String>');
expect(result.toString(), '([List<String>, List, String])');
});
});
}

0 comments on commit bab8870

Please sign in to comment.