From ca84c194bd214f17306965a288c161633904ae98 Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Mon, 23 Oct 2023 18:35:29 +0200 Subject: [PATCH] Add shared analyzer option (closes #2688) --- .../Generation options/in_other_builders.md | 52 ++++++++++++++++++- docs/pages/docs/Generation options/index.md | 3 ++ drift_dev/CHANGELOG.md | 4 ++ drift_dev/build.yaml | 1 + drift_dev/lib/src/analysis/options.dart | 5 ++ .../lib/src/backends/build/drift_builder.dart | 6 ++- .../lib/src/generated/analysis/options.g.dart | 5 ++ drift_dev/pubspec.yaml | 2 +- sqlparser/pubspec.yaml | 2 +- 9 files changed, 75 insertions(+), 5 deletions(-) diff --git a/docs/pages/docs/Generation options/in_other_builders.md b/docs/pages/docs/Generation options/in_other_builders.md index 418ba85b..4653fcda 100644 --- a/docs/pages/docs/Generation options/in_other_builders.md +++ b/docs/pages/docs/Generation options/in_other_builders.md @@ -15,8 +15,21 @@ targets: drift: auto_apply_builders: false builders: + drift_dev:analyzer: + enabled: true + options: &options + # Drift build options, as per https://drift.simonbinder.eu/docs/advanced-features/builder_options/ + store_date_time_values_as_text: true + named_parameters: true + sql: + dialect: sqlite + options: + version: "3.39" + modules: [fts5] drift_dev:modular: enabled: true + # We use yaml anchors to give the two builders the same options + options: *options $default: dependencies: @@ -27,7 +40,6 @@ targets: # its own target instead. drift_dev: enabled: false - ``` With modular generation, you'll have to replace the `part` statement in the database file with an @@ -59,3 +71,41 @@ and use the non-shared generator instead. Finally, we need to the build system to run drift first, and all the other builders otherwise. This is why we split the builders up into multiple targets. The first target will only run drift, the second target has a dependency on the first one and will run all the other builders. + +## Using `drift_dev:not_shared` + +For complex build setups like those requiring other builders to see drift code, the `drift_dev:modular` +builder is recommended. +However, enabling the modular builder requires other code modifications like replacing `part` statements +with imports. A simpler change may be the `not_shared` builder offered by `drift_dev`. It works like the +default setup, except that it emits a `.drift.dart` part file instead of a shared `.g.dart` file - so you +only have to change a single `part` statement to migrate. + +To enable this builder, also enable the `drift_dev:analyzer` builder and the `has_separate_analyzer` +option: + +```yaml +targets: + drift: + auto_apply_builders: false + builders: + drift_dev:analyzer: + enabled: true + options: &options + has_separate_analyzer: true # always enable this option when using `not_shared` + # remaining options... + drift_dev:not_shared: + enabled: true + # We use yaml anchors to give the two builders the same options + options: *options + + $default: + dependencies: + # run drift's builder first + - ":drift" + builders: + # This builder is enabled by default, but we're using the modular builder in + # its own target instead. + drift_dev: + enabled: false +``` diff --git a/docs/pages/docs/Generation options/index.md b/docs/pages/docs/Generation options/index.md index b2d77e09..ef04b4a5 100644 --- a/docs/pages/docs/Generation options/index.md +++ b/docs/pages/docs/Generation options/index.md @@ -77,6 +77,9 @@ At the moment, drift supports these options: The possible values are `preserve`, `camelCase`, `CONSTANT_CASE`, `snake_case`, `PascalCase`, `lowercase` and `UPPERCASE` (default: `snake_case`). * `write_to_columns_mixins`: Whether the `toColumns` method should be written as a mixin instead of being added directly to the data class. This is useful when using [existing row classes]({{ '../custom_row_classes.md' | pageUrl }}), as the mixin is generated for those as well. +* `has_separate_analyzer`: This option is only relevant when using the `drift_dev:not_shared` builder, which needs to use a less efficient + analysis implementation than the other builders by default. After also applying `drift_dev:analyzer` to the same build target, this option + can be enabled to speed up builds. This option has no effect with the default or the modular builder. * `fatal_warnings`: When enabled (defaults to `false`), warnings found by `drift_dev` in the build process (like syntax errors in SQL queries or unresolved references in your Dart tables) will cause the build to fail. * `preamble`: This option is useful when using drift [as a standalone part builder](#using-drift-classes-in-other-builders) or when running a diff --git a/drift_dev/CHANGELOG.md b/drift_dev/CHANGELOG.md index c2608f37..df13dbef 100644 --- a/drift_dev/CHANGELOG.md +++ b/drift_dev/CHANGELOG.md @@ -1,3 +1,7 @@ +## 2.13.1-dev + +- Add `has_separate_analyzer` option to optimize builds using the `not_shared` builder. + ## 2.13.0 - Fix indices not being created for Dart tables from different files. diff --git a/drift_dev/build.yaml b/drift_dev/build.yaml index 0fd1d4f9..d3194109 100644 --- a/drift_dev/build.yaml +++ b/drift_dev/build.yaml @@ -62,6 +62,7 @@ builders: auto_apply: none required_inputs: [".drift_prep.json"] applies_builders: [":preparing_builder"] + runs_before: [":not_shared"] post_process_builders: cleanup: diff --git a/drift_dev/lib/src/analysis/options.dart b/drift_dev/lib/src/analysis/options.dart index bc7fba4d..f4b36be5 100644 --- a/drift_dev/lib/src/analysis/options.dart +++ b/drift_dev/lib/src/analysis/options.dart @@ -102,6 +102,9 @@ class DriftOptions { @JsonKey(name: 'write_to_columns_mixins', defaultValue: false) final bool writeToColumnsMixins; + @JsonKey(name: 'has_separate_analyzer', defaultValue: false) + final bool hasDriftAnalyzer; + final String? preamble; @JsonKey(name: 'fatal_warnings', defaultValue: false) @@ -131,6 +134,7 @@ class DriftOptions { this.preamble, this.writeToColumnsMixins = false, this.fatalWarnings = false, + this.hasDriftAnalyzer = false, }); DriftOptions({ @@ -155,6 +159,7 @@ class DriftOptions { required this.writeToColumnsMixins, required this.fatalWarnings, required this.preamble, + required this.hasDriftAnalyzer, this.dialect, }) { // ignore: deprecated_member_use_from_same_package diff --git a/drift_dev/lib/src/backends/build/drift_builder.dart b/drift_dev/lib/src/backends/build/drift_builder.dart index 37a6b925..bf47bdf7 100644 --- a/drift_dev/lib/src/backends/build/drift_builder.dart +++ b/drift_dev/lib/src/backends/build/drift_builder.dart @@ -140,8 +140,10 @@ class _DriftBuildRun { // The discovery and analyzer builders will have emitted IR for // every relevant file in a previous build step that this builder // has a dependency on. - findsResolvedElementsReliably: !mode.embeddedAnalyzer, - findsLocalElementsReliably: !mode.embeddedAnalyzer, + findsResolvedElementsReliably: + !mode.embeddedAnalyzer || options.hasDriftAnalyzer, + findsLocalElementsReliably: + !mode.embeddedAnalyzer || options.hasDriftAnalyzer, ); Future run() async { diff --git a/drift_dev/lib/src/generated/analysis/options.g.dart b/drift_dev/lib/src/generated/analysis/options.g.dart index c9a0974d..74576168 100644 --- a/drift_dev/lib/src/generated/analysis/options.g.dart +++ b/drift_dev/lib/src/generated/analysis/options.g.dart @@ -33,6 +33,7 @@ DriftOptions _$DriftOptionsFromJson(Map json) => $checkedCreate( 'store_date_time_values_as_text', 'case_from_dart_to_sql', 'write_to_columns_mixins', + 'has_separate_analyzer', 'preamble', 'fatal_warnings' ], @@ -91,6 +92,8 @@ DriftOptions _$DriftOptionsFromJson(Map json) => $checkedCreate( fatalWarnings: $checkedConvert('fatal_warnings', (v) => v as bool? ?? false), preamble: $checkedConvert('preamble', (v) => v as String?), + hasDriftAnalyzer: $checkedConvert( + 'has_separate_analyzer', (v) => v as bool? ?? false), dialect: $checkedConvert('sql', (v) => v == null ? null : DialectOptions.fromJson(v as Map)), ); @@ -120,6 +123,7 @@ DriftOptions _$DriftOptionsFromJson(Map json) => $checkedCreate( 'caseFromDartToSql': 'case_from_dart_to_sql', 'writeToColumnsMixins': 'write_to_columns_mixins', 'fatalWarnings': 'fatal_warnings', + 'hasDriftAnalyzer': 'has_separate_analyzer', 'dialect': 'sql' }, ); @@ -153,6 +157,7 @@ Map _$DriftOptionsToJson(DriftOptions instance) => 'case_from_dart_to_sql': _$CaseFromDartToSqlEnumMap[instance.caseFromDartToSql]!, 'write_to_columns_mixins': instance.writeToColumnsMixins, + 'has_separate_analyzer': instance.hasDriftAnalyzer, 'preamble': instance.preamble, 'fatal_warnings': instance.fatalWarnings, }; diff --git a/drift_dev/pubspec.yaml b/drift_dev/pubspec.yaml index a32f2afa..67b5a501 100644 --- a/drift_dev/pubspec.yaml +++ b/drift_dev/pubspec.yaml @@ -1,6 +1,6 @@ name: drift_dev description: Dev-dependency for users of drift. Contains the generator and development tools. -version: 2.13.0 +version: 2.13.1-dev repository: https://github.com/simolus3/drift homepage: https://drift.simonbinder.eu/ issue_tracker: https://github.com/simolus3/drift/issues diff --git a/sqlparser/pubspec.yaml b/sqlparser/pubspec.yaml index d3a674d2..07077868 100644 --- a/sqlparser/pubspec.yaml +++ b/sqlparser/pubspec.yaml @@ -1,6 +1,6 @@ name: sqlparser description: Parses sqlite statements and performs static analysis on them -version: 0.32.0 +version: 0.32.1-dev homepage: https://github.com/simolus3/drift/tree/develop/sqlparser repository: https://github.com/simolus3/drift #homepage: https://drift.simonbinder.eu/