From 27ea49137d02d3e9b5f156c2a1343b70375201a2 Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Tue, 16 Aug 2022 16:05:08 +0200 Subject: [PATCH] Properly read options (#1992) --- drift_dev/CHANGELOG.md | 4 + drift_dev/lib/src/analyzer/options.dart | 2 +- drift_dev/lib/src/utils/options_reader.dart | 20 ++-- drift_dev/test/utils/options_reader_test.dart | 103 ++++++++++++++++++ 4 files changed, 118 insertions(+), 11 deletions(-) create mode 100644 drift_dev/test/utils/options_reader_test.dart diff --git a/drift_dev/CHANGELOG.md b/drift_dev/CHANGELOG.md index 99fe8aeb..8b13c215 100644 --- a/drift_dev/CHANGELOG.md +++ b/drift_dev/CHANGELOG.md @@ -1,3 +1,7 @@ +## 2.0.1 + +- Recognize options for applied `not_shared` builder when exporting schemas. + ## 2.0.0 - Removes the following build options, which are always turned on now: diff --git a/drift_dev/lib/src/analyzer/options.dart b/drift_dev/lib/src/analyzer/options.dart index b94653b0..13b6edf0 100644 --- a/drift_dev/lib/src/analyzer/options.dart +++ b/drift_dev/lib/src/analyzer/options.dart @@ -93,7 +93,7 @@ class DriftOptions { this.overrideHashAndEqualsInResultSets = false, this.skipVerificationCode = false, this.useDataClassNameForCompanions = false, - this.useColumnNameAsJsonKeyWhenDefinedInMoorFile = false, + this.useColumnNameAsJsonKeyWhenDefinedInMoorFile = true, this.generateConnectConstructor = false, this.dataClassToCompanions = true, this.generateMutableClasses = false, diff --git a/drift_dev/lib/src/utils/options_reader.dart b/drift_dev/lib/src/utils/options_reader.dart index db4b2782..ede23d12 100644 --- a/drift_dev/lib/src/utils/options_reader.dart +++ b/drift_dev/lib/src/utils/options_reader.dart @@ -1,4 +1,5 @@ import 'package:build_config/build_config.dart'; +import 'package:collection/collection.dart'; import 'package:drift_dev/src/analyzer/options.dart'; Future fromRootDir(String path) async { @@ -7,14 +8,13 @@ Future fromRootDir(String path) async { } DriftOptions readOptionsFromConfig(BuildConfig config) { - final options = config.buildTargets.values - .map((t) { - return t.builders['moor_generator:moor_generator']?.options ?? - t.builders['drift_dev:drift_dev']?.options; - }) - .whereType() - .map((json) => DriftOptions.fromJson(json)); - - final iterator = options.iterator; - return iterator.moveNext() ? iterator.current : const DriftOptions.defaults(); + return config.buildTargets.values + .map((t) { + return t.builders['drift_dev:drift_dev']?.options ?? + t.builders['drift_dev:not_shared']?.options; + }) + .whereType() + .map((json) => DriftOptions.fromJson(json)) + .firstOrNull ?? + DriftOptions.defaults(); } diff --git a/drift_dev/test/utils/options_reader_test.dart b/drift_dev/test/utils/options_reader_test.dart new file mode 100644 index 00000000..20ac7694 --- /dev/null +++ b/drift_dev/test/utils/options_reader_test.dart @@ -0,0 +1,103 @@ +import 'package:build_config/build_config.dart'; +import 'package:drift_dev/src/utils/options_reader.dart'; +import 'package:test/test.dart'; + +void main() { + test('reads options from build.yaml file', () { + final config = BuildConfig.parse('a', ['drift_dev'], r''' +targets: + $default: + auto_apply_builders: false + builders: + drift_dev:preparing_builder: + enabled: true + drift_dev:drift_dev: + enabled: true + options: + scoped_dart_components: false + json_serializable: + enabled: true + sources: + - lib/** + - test/generated/** +'''); + + final options = readOptionsFromConfig(config); + expect(options.scopedDartComponents, isFalse); + }); + + test('supports reading from non-default target', () { + final config = BuildConfig.parse('a', ['drift_dev'], r''' +targets: + $default: + dependencies: [:source_gen] + source_gen: + auto_apply_builders: false + builders: + drift_dev:preparing_builder: + enabled: true + drift_dev:drift_dev: + enabled: true + options: + scoped_dart_components: false + json_serializable: + enabled: true + sources: + - lib/** + - test/generated/** +'''); + + final options = readOptionsFromConfig(config); + expect(options.scopedDartComponents, isFalse); + }); + + test('supports reading for not_shared builder', () { + final config = BuildConfig.parse('a', ['drift_dev'], r''' +targets: + $default: + dependencies: [:source_gen] + source_gen: + auto_apply_builders: false + builders: + drift_dev:preparing_builder: + enabled: true + drift_dev:not_shared: + enabled: true + options: + scoped_dart_components: false + json_serializable: + enabled: true + sources: + - lib/** + - test/generated/** +'''); + + final options = readOptionsFromConfig(config); + expect(options.scopedDartComponents, isFalse); + }); + + test('still works with | syntax', () { + final config = BuildConfig.parse('a', ['drift_dev'], r''' +targets: + $default: + dependencies: [:source_gen] + source_gen: + auto_apply_builders: false + builders: + drift_dev|preparing_builder: + enabled: true + drift_dev|not_shared: + enabled: true + options: + scoped_dart_components: false + json_serializable: + enabled: true + sources: + - lib/** + - test/generated/** +'''); + + final options = readOptionsFromConfig(config); + expect(options.scopedDartComponents, isFalse); + }); +}