From c0c1810bcf15c2ace4db5bda6e3fee106a9142c0 Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Wed, 7 Dec 2022 10:55:20 +0100 Subject: [PATCH] Don't generate empty drift accessors --- drift_dev/lib/src/analysis/driver/state.dart | 30 +++++++++++++------ .../lib/src/backends/build/drift_builder.dart | 18 +++++------ drift_dev/lib/src/writer/database_writer.dart | 10 +++++-- .../lib/src/writer/drift_accessor_writer.dart | 3 +- drift_dev/lib/src/writer/modules.dart | 15 ++++++---- examples/modular/lib/database.drift.dart | 2 -- examples/modular/lib/src/posts.drift.dart | 7 ----- examples/modular/lib/src/search.drift.dart | 1 - .../modular/lib/src/user_queries.drift.dart | 1 - examples/modular/lib/src/users.drift.dart | 5 ---- 10 files changed, 49 insertions(+), 43 deletions(-) diff --git a/drift_dev/lib/src/analysis/driver/state.dart b/drift_dev/lib/src/analysis/driver/state.dart index 812df221..8237730d 100644 --- a/drift_dev/lib/src/analysis/driver/state.dart +++ b/drift_dev/lib/src/analysis/driver/state.dart @@ -3,10 +3,12 @@ import 'package:meta/meta.dart'; import 'package:path/path.dart' show url; import 'package:sqlparser/sqlparser.dart' hide AnalysisError; +import '../resolver/intermediate_state.dart'; import '../results/database.dart'; import '../results/element.dart'; import '../results/file_results.dart'; import '../results/query.dart'; +import 'driver.dart'; import 'error.dart'; class FileState { @@ -18,6 +20,8 @@ class FileState { final Map analysis = {}; FileAnalysisResult? fileAnalysis; + bool? _needsModularAccessor; + FileState(this.ownUri); String get extension => url.extension(ownUri.path); @@ -27,15 +31,6 @@ class FileState { return analyzedElements.any((e) => e is BaseDriftAccessor); } - /// Whether an accessor class making queries and imports available should be - /// written for this file if modular analysis is enabled. - bool get hasModularDriftAccessor { - final hasImports = discovery?.importDependencies.isNotEmpty == true; - final hasQuery = analyzedElements.any((e) => e is DefinedSqlQuery); - - return hasImports || hasQuery; - } - /// All analyzed [DriftElement]s found in this library. @visibleForTesting Iterable get analyzedElements { @@ -66,6 +61,23 @@ class FileState { bool elementIsAnalyzed(DriftElementId id) { return analysis[id]?.isUpToDate == true; } + + bool get _definesQuery { + return analyzedElements.any((e) => e is DefinedSqlQuery); + } + + /// Whether an accessor class making queries and imports available should be + /// written for this file if modular analysis is enabled. + /// + /// This is the case if this accessor defines queries or if it transitively + /// imports a modular accessor. + bool needsModularAccessor(DriftAnalysisDriver driver) { + if (_needsModularAccessor != null) return _needsModularAccessor!; + + return _needsModularAccessor = driver.cache.crawl(this).any((e) { + return e._definesQuery; + }); + } } abstract class DiscoveredFileState { diff --git a/drift_dev/lib/src/backends/build/drift_builder.dart b/drift_dev/lib/src/backends/build/drift_builder.dart index 01cd1c5c..548d03c2 100644 --- a/drift_dev/lib/src/backends/build/drift_builder.dart +++ b/drift_dev/lib/src/backends/build/drift_builder.dart @@ -262,7 +262,8 @@ class _DriftBuildRun { } else if (result is DriftDatabase) { final resolved = entrypointState.fileAnalysis!.resolvedDatabases[result.id]!; - final input = DatabaseGenerationInput(result, resolved, const {}); + final input = + DatabaseGenerationInput(result, resolved, const {}, driver); DatabaseWriter(input, writer.child()).write(); // Also write stubs for known custom functions so that the user can @@ -271,14 +272,13 @@ class _DriftBuildRun { } else if (result is DatabaseAccessor) { final resolved = entrypointState.fileAnalysis!.resolvedDatabases[result.id]!; - final input = AccessorGenerationInput(result, resolved, const {}); + final input = + AccessorGenerationInput(result, resolved, const {}, driver); AccessorWriter(input, writer.child()).write(); } } - if (entrypointState.hasModularDriftAccessor) { - ModularAccessorWriter(writer.child(), entrypointState).write(); - } + ModularAccessorWriter(writer.child(), entrypointState, driver).write(); } Future _generateMonolithic(FileState entrypointState) async { @@ -320,12 +320,12 @@ class _DriftBuildRun { .map((k, v) => MapEntry(k, mappedQueries[v] ?? v)); if (result is DriftDatabase) { - final input = - DatabaseGenerationInput(result, resolved, importedQueries); + final input = DatabaseGenerationInput( + result, resolved, importedQueries, driver); DatabaseWriter(input, writer.child()).write(); } else if (result is DatabaseAccessor) { - final input = - AccessorGenerationInput(result, resolved, importedQueries); + final input = AccessorGenerationInput( + result, resolved, importedQueries, driver); AccessorWriter(input, writer.child()).write(); } } diff --git a/drift_dev/lib/src/writer/database_writer.dart b/drift_dev/lib/src/writer/database_writer.dart index e7aa8366..d5c49bdb 100644 --- a/drift_dev/lib/src/writer/database_writer.dart +++ b/drift_dev/lib/src/writer/database_writer.dart @@ -4,6 +4,7 @@ import 'package:drift/src/runtime/executor/stream_queries.dart'; import 'package:drift_dev/src/writer/utils/memoized_getter.dart'; import 'package:recase/recase.dart'; +import '../analysis/driver/driver.dart'; import '../analysis/results/file_results.dart'; import '../analysis/results/results.dart'; import '../services/find_stream_update_rules.dart'; @@ -17,7 +18,7 @@ import 'writer.dart'; /// Generates the Dart code put into a `.g.dart` file when running the /// generator. class DatabaseWriter { - DatabaseGenerationInput input; + final DatabaseGenerationInput input; final Scope scope; DriftDatabase get db => input.accessor; @@ -141,7 +142,8 @@ class DatabaseWriter { // Also write implicit DAOs for modular imports if (scope.generationOptions.isModular) { for (final import in input.resolvedAccessor.knownImports) { - dbScope.writeGetterForIncludedDriftFile(import, isAccessor: false); + dbScope.writeGetterForIncludedDriftFile(import, input.driver, + isAccessor: false); } } @@ -238,8 +240,10 @@ class GenerationInput { final T accessor; final ResolvedDatabaseAccessor resolvedAccessor; final Map importedQueries; + final DriftAnalysisDriver driver; - GenerationInput(this.accessor, this.resolvedAccessor, this.importedQueries); + GenerationInput( + this.accessor, this.resolvedAccessor, this.importedQueries, this.driver); /// All locally-defined and imported [SqlQuery] elements that are regular /// queries (so no query with [QueryMode.atCreate]). diff --git a/drift_dev/lib/src/writer/drift_accessor_writer.dart b/drift_dev/lib/src/writer/drift_accessor_writer.dart index 1350d836..0e88ae16 100644 --- a/drift_dev/lib/src/writer/drift_accessor_writer.dart +++ b/drift_dev/lib/src/writer/drift_accessor_writer.dart @@ -38,7 +38,8 @@ class AccessorWriter { if (scope.generationOptions.isModular) { for (final import in input.resolvedAccessor.knownImports) { - classScope.writeGetterForIncludedDriftFile(import, isAccessor: true); + classScope.writeGetterForIncludedDriftFile(import, input.driver, + isAccessor: true); } } diff --git a/drift_dev/lib/src/writer/modules.dart b/drift_dev/lib/src/writer/modules.dart index 0ae94d3e..c521b009 100644 --- a/drift_dev/lib/src/writer/modules.dart +++ b/drift_dev/lib/src/writer/modules.dart @@ -1,6 +1,7 @@ import 'package:path/path.dart' show url; import 'package:recase/recase.dart'; +import '../analysis/driver/driver.dart'; import '../analysis/driver/state.dart'; import '../analysis/results/results.dart'; import '../utils/string_escaper.dart'; @@ -19,11 +20,12 @@ import 'writer.dart'; class ModularAccessorWriter { final Scope scope; final FileState file; + final DriftAnalysisDriver driver; - ModularAccessorWriter(this.scope, this.file); + ModularAccessorWriter(this.scope, this.file, this.driver); void write() { - if (!file.hasModularDriftAccessor) return; + if (!file.needsModularAccessor(driver)) return; final className = scope.modularAccessor(file.ownUri); final generatedDatabase = scope.drift('GeneratedDatabase'); @@ -69,7 +71,9 @@ class ModularAccessorWriter { // Also make imports available final imports = file.discovery?.importDependencies ?? const []; for (final import in imports) { - if (url.extension(import.path) == '.drift') { + final file = driver.cache.knownFiles[import]; + + if (file != null && file.needsModularAccessor(driver)) { final moduleClass = restOfClass.modularAccessor(import); final getterName = ReCase(moduleClass.toString()).camelCase; @@ -93,11 +97,12 @@ class ModularAccessorWriter { } extension WriteImplicitDaoGetter on Scope { - void writeGetterForIncludedDriftFile(FileState import, + void writeGetterForIncludedDriftFile( + FileState import, DriftAnalysisDriver driver, {required bool isAccessor}) { assert(generationOptions.isModular); - if (import.hasModularDriftAccessor) { + if (import.needsModularAccessor(driver)) { final type = modularAccessor(import.ownUri); final getter = ReCase(type.toString()).camelCase; diff --git a/examples/modular/lib/database.drift.dart b/examples/modular/lib/database.drift.dart index 578dd598..f054a60c 100644 --- a/examples/modular/lib/database.drift.dart +++ b/examples/modular/lib/database.drift.dart @@ -19,8 +19,6 @@ abstract class $Database extends i0.GeneratedDatabase { late final i4.MyAccessor myAccessor = i4.MyAccessor(this as i5.Database); i6.UserQueriesDrift get userQueriesDrift => i7.ReadDatabaseContainer(this) .accessor(i6.UserQueriesDrift.new); - i2.PostsDrift get postsDrift => - i7.ReadDatabaseContainer(this).accessor(i2.PostsDrift.new); i3.SearchDrift get searchDrift => i7.ReadDatabaseContainer(this) .accessor(i3.SearchDrift.new); @override diff --git a/examples/modular/lib/src/posts.drift.dart b/examples/modular/lib/src/posts.drift.dart index 7b007d0a..ff703073 100644 --- a/examples/modular/lib/src/posts.drift.dart +++ b/examples/modular/lib/src/posts.drift.dart @@ -1,8 +1,6 @@ // ignore_for_file: type=lint import 'package:drift/drift.dart' as i0; import 'package:modular/src/posts.drift.dart' as i1; -import 'package:drift/internal/modular.dart' as i2; -import 'package:modular/src/users.drift.dart' as i3; class Post extends i0.DataClass implements i0.Insertable { final int id; @@ -396,8 +394,3 @@ class Likes extends i0.Table with i0.TableInfo { @override bool get dontWriteConstraints => true; } - -class PostsDrift extends i2.ModularAccessor { - PostsDrift(i0.GeneratedDatabase db) : super(db); - i3.UsersDrift get usersDrift => this.accessor(i3.UsersDrift.new); -} diff --git a/examples/modular/lib/src/search.drift.dart b/examples/modular/lib/src/search.drift.dart index 5a23ccda..9c4cc35d 100644 --- a/examples/modular/lib/src/search.drift.dart +++ b/examples/modular/lib/src/search.drift.dart @@ -220,5 +220,4 @@ class SearchDrift extends i2.ModularAccessor { i1.SearchInPosts get searchInPosts => this.resultSet('search_in_posts'); i3.Posts get posts => this.resultSet('posts'); - i3.PostsDrift get postsDrift => this.accessor(i3.PostsDrift.new); } diff --git a/examples/modular/lib/src/user_queries.drift.dart b/examples/modular/lib/src/user_queries.drift.dart index f27a065a..8fe27dbd 100644 --- a/examples/modular/lib/src/user_queries.drift.dart +++ b/examples/modular/lib/src/user_queries.drift.dart @@ -42,7 +42,6 @@ class UserQueriesDrift extends i1.ModularAccessor { i2.PopularUsers get popularUsers => this.resultSet('popular_users'); i2.Follows get follows => this.resultSet('follows'); - i2.UsersDrift get usersDrift => this.accessor(i2.UsersDrift.new); } typedef FindUsers$predicate = i0.Expression Function(i2.Users users); diff --git a/examples/modular/lib/src/users.drift.dart b/examples/modular/lib/src/users.drift.dart index ba76b25e..b8b6a297 100644 --- a/examples/modular/lib/src/users.drift.dart +++ b/examples/modular/lib/src/users.drift.dart @@ -2,7 +2,6 @@ import 'package:drift/drift.dart' as i0; import 'package:modular/src/users.drift.dart' as i1; import 'package:modular/src/preferences.dart' as i2; -import 'package:drift/internal/modular.dart' as i3; class User extends i0.DataClass implements i0.Insertable { final int id; @@ -571,7 +570,3 @@ class PopularUsers extends i0.ViewInfo @override Set get readTables => const {'users', 'follows'}; } - -class UsersDrift extends i3.ModularAccessor { - UsersDrift(i0.GeneratedDatabase db) : super(db); -}