Add shared analyzer option (closes #2688)

This commit is contained in:
Simon Binder 2023-10-23 18:35:29 +02:00
parent bf8dff0887
commit ca84c194bd
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
9 changed files with 75 additions and 5 deletions

View File

@ -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
```

View File

@ -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

View File

@ -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.

View File

@ -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:

View File

@ -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

View File

@ -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<void> run() async {

View File

@ -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<String, dynamic> _$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,
};

View File

@ -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

View File

@ -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/