mirror of https://github.com/AMT-Cheif/drift.git
Use same generator session in same build step
This commit is contained in:
parent
59f408229b
commit
75d4463085
|
@ -3,6 +3,7 @@ import 'dart:async';
|
|||
import 'package:moor_generator/src/analyzer/runner/file_graph.dart';
|
||||
import 'package:moor_generator/src/analyzer/runner/task.dart';
|
||||
import 'package:moor_generator/src/backends/backend.dart';
|
||||
import 'package:moor_generator/src/backends/build/moor_builder.dart';
|
||||
import 'package:path/path.dart' as p;
|
||||
|
||||
const _fileEndings = {
|
||||
|
@ -14,11 +15,12 @@ const _fileEndings = {
|
|||
class MoorSession {
|
||||
final FileGraph fileGraph = FileGraph();
|
||||
final Backend backend;
|
||||
final MoorOptions options;
|
||||
|
||||
final _completedTasks = StreamController<Task>.broadcast();
|
||||
final _changedFiles = StreamController<List<FoundFile>>.broadcast();
|
||||
|
||||
MoorSession(this.backend);
|
||||
MoorSession(this.backend, {this.options = const MoorOptions()});
|
||||
|
||||
/// Stream that emits a [Task] that has been completed.
|
||||
Stream<Task> get completedTasks => _completedTasks.stream;
|
||||
|
|
|
@ -1,20 +1,12 @@
|
|||
import 'package:analyzer/dart/ast/ast.dart';
|
||||
import 'package:analyzer/dart/element/element.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
import 'package:moor_generator/src/analyzer/session.dart';
|
||||
|
||||
/// A backend for the moor generator.
|
||||
///
|
||||
/// Currently, we only have a backend based on the build package, but we can
|
||||
/// extend this to a backend for an analyzer plugin or a standalone tool.
|
||||
abstract class Backend {
|
||||
MoorSession _session;
|
||||
MoorSession get session => _session;
|
||||
|
||||
Backend() {
|
||||
_session = MoorSession(this);
|
||||
}
|
||||
|
||||
/// Resolves an [import] statement from the context of a [base] uri. This
|
||||
/// should support both relative and `package:` imports.
|
||||
Uri resolve(Uri base, String import);
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import 'package:build/build.dart';
|
||||
import 'package:moor_generator/src/analyzer/runner/results.dart';
|
||||
import 'package:moor_generator/src/analyzer/session.dart';
|
||||
import 'package:moor_generator/src/backends/build/build_backend.dart';
|
||||
import 'package:moor_generator/src/backends/build/generators/dao_generator.dart';
|
||||
import 'package:moor_generator/src/backends/build/generators/moor_generator.dart';
|
||||
|
@ -11,6 +12,9 @@ part 'options.dart';
|
|||
class MoorBuilder extends SharedPartBuilder {
|
||||
final MoorOptions options;
|
||||
|
||||
final BuildBackend _backend = BuildBackend();
|
||||
final Expando<MoorSession> _sessions = Expando();
|
||||
|
||||
MoorBuilder._(List<Generator> generators, String name, this.options)
|
||||
: super(generators, name);
|
||||
|
||||
|
@ -33,10 +37,17 @@ class MoorBuilder extends SharedPartBuilder {
|
|||
|
||||
Writer createWriter() => Writer(options);
|
||||
|
||||
MoorSession _getSession(BuildStep step) {
|
||||
if (_sessions[step] != null) {
|
||||
return _sessions[step];
|
||||
} else {
|
||||
return _sessions[step] = MoorSession(_backend, options: options);
|
||||
}
|
||||
}
|
||||
|
||||
Future<ParsedDartFile> analyzeDartFile(BuildStep step) async {
|
||||
final backend = BuildBackend();
|
||||
final backendTask = backend.createTask(step);
|
||||
final session = backend.session;
|
||||
final session = _getSession(step);
|
||||
final backendTask = _backend.createTask(step);
|
||||
|
||||
final input = session.registerFile(step.inputId.uri);
|
||||
final task = session.startTask(backendTask);
|
||||
|
|
|
@ -31,7 +31,7 @@ class MoorDriver implements AnalysisDriverGeneric {
|
|||
this.contentOverlay, this._resourceProvider) {
|
||||
_scheduler.add(this);
|
||||
final backend = PluginBackend(this);
|
||||
session = backend.session;
|
||||
session = MoorSession(backend);
|
||||
|
||||
_fileChangeSubscription =
|
||||
session.changedFiles.listen(_tracker.notifyFilesChanged);
|
||||
|
|
|
@ -2,6 +2,7 @@ import 'package:moor_generator/src/analyzer/sql_queries/meta/declarations.dart';
|
|||
import 'package:moor_generator/src/backends/build/moor_builder.dart';
|
||||
import 'package:moor_generator/src/model/used_type_converter.dart';
|
||||
|
||||
/// The column types in sql.
|
||||
enum ColumnType { integer, text, boolean, datetime, blob, real }
|
||||
|
||||
/// Name of a column. Contains additional info on whether the name was chosen
|
||||
|
|
|
@ -5,6 +5,8 @@ import 'package:moor_generator/src/writer/utils/memoized_getter.dart';
|
|||
import 'package:moor_generator/src/writer/writer.dart';
|
||||
import 'package:recase/recase.dart';
|
||||
|
||||
/// Generates the Dart code put into a `.g.dart` file when running the
|
||||
/// generator.
|
||||
class DatabaseWriter {
|
||||
final SpecifiedDatabase db;
|
||||
final Scope scope;
|
||||
|
|
|
@ -17,6 +17,7 @@ class Writer {
|
|||
_root = Scope(parent: null, writer: this);
|
||||
}
|
||||
|
||||
/// Returns the code generated by this [Writer].
|
||||
String writeGenerated() => _leafNodes(_root).join();
|
||||
|
||||
Iterable<StringBuffer> _leafNodes(Scope scope) sync* {
|
||||
|
|
|
@ -30,7 +30,7 @@ CREATE TABLE bars (
|
|||
);
|
||||
''',
|
||||
});
|
||||
session = backend.session;
|
||||
session = MoorSession(backend);
|
||||
});
|
||||
|
||||
tearDownAll(() {
|
||||
|
|
|
@ -66,7 +66,7 @@ class ProgrammingLanguages extends Table {
|
|||
''',
|
||||
},
|
||||
);
|
||||
session = backend.session;
|
||||
session = MoorSession(backend);
|
||||
});
|
||||
|
||||
tearDownAll(() {
|
||||
|
|
|
@ -38,7 +38,7 @@ WITH alias(first, second) AS (SELECT * FROM foo) SELECT * FROM alias;
|
|||
''',
|
||||
},
|
||||
);
|
||||
session = backend.session;
|
||||
session = MoorSession(backend);
|
||||
});
|
||||
|
||||
setUp(() async {
|
||||
|
|
Loading…
Reference in New Issue