From bab8870ec2450ff757112ef81805cef9a79dc888 Mon Sep 17 00:00:00 2001 From: robmllze <86869208+robmllze@users.noreply.github.com> Date: Mon, 11 Nov 2024 17:10:14 +1100 Subject: [PATCH] breaking: Improve generator output --- CHANGELOG.md | 5 +++++ lib/src/_core_utils/generator_converger.dart | 10 ++++------ lib/src/_dart_utils/dart_loose_type_mappers.dart | 11 ++++------- lib/src/app.dart | 11 ++++------- lib/src/generate.dart | 14 +++++++------- lib/src/generate_ai_models.dart | 2 +- pubspec.yaml | 13 +++++++------ test/test_decompose_dart_collection_type.dart | 11 +++++++++++ 8 files changed, 43 insertions(+), 34 deletions(-) create mode 100644 test/test_decompose_dart_collection_type.dart diff --git a/CHANGELOG.md b/CHANGELOG.md index ac0bdb2..9a780ff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## [0.8.0] + +- Released @ 10/2024 (UTC) +- breaking: Improve generator output + ## [0.7.3] - Released @ 10/2024 (UTC) diff --git a/lib/src/_core_utils/generator_converger.dart b/lib/src/_core_utils/generator_converger.dart index 9875eb9..eccf285 100644 --- a/lib/src/_core_utils/generator_converger.dart +++ b/lib/src/_core_utils/generator_converger.dart @@ -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); @@ -56,5 +55,4 @@ final generatorConverger = _GeneratorConverger( // ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ -typedef _GeneratorConverger - = GeneratorConverger, Enum, String>; +typedef _GeneratorConverger = GeneratorConverger, Enum, String>; diff --git a/lib/src/_dart_utils/dart_loose_type_mappers.dart b/lib/src/_dart_utils/dart_loose_type_mappers.dart index 7b54d41..163bef1 100644 --- a/lib/src/_dart_utils/dart_loose_type_mappers.dart +++ b/lib/src/_dart_utils/dart_loose_type_mappers.dart @@ -39,19 +39,16 @@ class DartLooseTypeMappers extends TypeMappers { // Standard. // --------------------------------------------------------------------- r'^(Map)\??$': (e) { - return 'letMapOrNull(${e.name})?.map((${e.args}) => MapEntry(${e.hashes},),).nonNulls.nullIfEmpty'; + return 'letMapOrNull(${e.name})?.map((${e.args}) => MapEntry(${e.hashes},),).nonNulls.nullIfEmpty?.unmodifiable'; }, r'^(Iterable)\??$': (e) { return 'letIterableOrNull(${e.name})?.map((${e.args}) => ${e.hashes},).nonNulls.nullIfEmpty'; }, r'^(List)\??$': (e) { - return 'letListOrNull(${e.name})?.map((${e.args}) => ${e.hashes},).nonNulls.nullIfEmpty?.toList()'; + return 'letListOrNull(${e.name})?.map((${e.args}) => ${e.hashes},).nonNulls.nullIfEmpty?.toList().unmodifiable'; }, r'^(Set)\??$': (e) { - return 'letSetOrNull(${e.name})?.map((${e.args}) => ${e.hashes},).nonNulls.nullIfEmpty?.toSet()'; - }, - r'^(Queue)\??$': (e) { - return '(){ final a = letIterableOrNull(${e.name})?.map((${e.args}) => ${e.hashes},).nonNulls.nullIfEmpty; return a != null ? Queue.of(a): null; }()'; + return 'letSetOrNull(${e.name})?.map((${e.args}) => ${e.hashes},).nonNulls.nullIfEmpty?.toSet().unmodifiable'; }, }); @@ -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()'; diff --git a/lib/src/app.dart b/lib/src/app.dart index 50e50f1..4334e55 100644 --- a/lib/src/app.dart +++ b/lib/src/app.dart @@ -24,8 +24,7 @@ import 'generate.dart'; Future runGenerateDartModelsApp(List 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( @@ -60,8 +59,7 @@ Future runGenerateDartModelsApp(List 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', @@ -74,8 +72,7 @@ Future runGenerateDartModelsApp(List 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', @@ -109,7 +106,7 @@ Future runGenerateDartModelsApp(List 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 {}, ); diff --git a/lib/src/generate.dart b/lib/src/generate.dart index 5161f46..709bdfa 100644 --- a/lib/src/generate.dart +++ b/lib/src/generate.dart @@ -23,7 +23,7 @@ import 'generate_ai_models.dart'; Future generateDartModelsFromAnnotations({ required Set rootDirPaths, Set subDirPaths = const {}, - Set pathPatterns = const {}, + Set matchPatterns = const {}, required String templateFilePath, String? fallbackDartSdkPath, String? gemeniApiKey, @@ -44,10 +44,10 @@ Future generateDartModelsFromAnnotations({ ), ], dirPathGroups: { - CombinedPaths( + MatchedPathPowerset( rootDirPaths, subPaths: subDirPaths, - pathPatterns: pathPatterns, + matchPatterns: matchPatterns, ), }, ); @@ -55,7 +55,7 @@ Future generateDartModelsFromAnnotations({ final template = templateFilePath.isNotEmpty ? extractCodeFromMarkdown( - await loadFileFromPathOrUrl(templateFilePath), + (await FileSystemUtility.i.readFileFromPathOrUrl(templateFilePath))!, ).trim() : ''; @@ -63,15 +63,15 @@ Future generateDartModelsFromAnnotations({ // 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. diff --git a/lib/src/generate_ai_models.dart b/lib/src/generate_ai_models.dart index 529cc38..1e70ba0 100644 --- a/lib/src/generate_ai_models.dart +++ b/lib/src/generate_ai_models.dart @@ -66,7 +66,7 @@ Future generateAIModels({ fileName, ); - await writeFile(filePath, output); + await FileSystemUtility.i.writeLocalFile(filePath, output); // Log a success. printGreen( diff --git a/pubspec.yaml b/pubspec.yaml index 668f537..64352f5 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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 @@ -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 @@ -48,6 +48,7 @@ dependencies: dev_dependencies: lints: ^5.0.0 + test: ^1.25.8 ## ----------------------------------------------------------------------------- diff --git a/test/test_decompose_dart_collection_type.dart b/test/test_decompose_dart_collection_type.dart new file mode 100644 index 0000000..b9b0c89 --- /dev/null +++ b/test/test_decompose_dart_collection_type.dart @@ -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'); + expect(result.toString(), '([List, List, String])'); + }); + }); +}