From 29453812133ff1f13e6dc1637addddf4bc641ebc Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Mon, 16 Aug 2021 17:55:45 +0200 Subject: [PATCH] Avoid analyzer_plugin imports outside of plugin --- extras/migrations_example/pubspec.yaml | 2 +- .../lib/src/backends/common/driver.dart | 20 ---------- .../lib/src/backends/plugin/plugin.dart | 30 +++++++++++++- moor_generator/lib/src/cli/cli.dart | 2 - .../cli/commands => tool}/debug_plugin.dart | 39 +++++++------------ 5 files changed, 42 insertions(+), 51 deletions(-) rename moor_generator/{lib/src/cli/commands => tool}/debug_plugin.dart (79%) diff --git a/extras/migrations_example/pubspec.yaml b/extras/migrations_example/pubspec.yaml index 5800d62c..70a85b3d 100644 --- a/extras/migrations_example/pubspec.yaml +++ b/extras/migrations_example/pubspec.yaml @@ -10,7 +10,7 @@ dependencies: dev_dependencies: moor_generator: - build_runner: ^1.10.1 + build_runner: ^2.0.0 test: ^1.15.4 dependency_overrides: diff --git a/moor_generator/lib/src/backends/common/driver.dart b/moor_generator/lib/src/backends/common/driver.dart index 91b7075a..3b0877b3 100644 --- a/moor_generator/lib/src/backends/common/driver.dart +++ b/moor_generator/lib/src/backends/common/driver.dart @@ -11,14 +11,11 @@ import 'package:moor/src/utils/synchronized.dart'; import 'package:moor_generator/src/analyzer/options.dart'; import 'package:moor_generator/src/analyzer/runner/file_graph.dart'; import 'package:moor_generator/src/analyzer/session.dart'; -import 'package:moor_generator/src/services/ide/moor_ide.dart'; import 'package:moor_generator/src/utils/options_reader.dart' as options; import '../standalone.dart'; class MoorDriver { - MoorIde ide; - final ResourceProvider _resourceProvider; final Lock lock = Lock(); @@ -43,7 +40,6 @@ class MoorDriver { // Options will be loaded later. session = MoorSession(backend, options: options); - ide = MoorIde(session, _DriverBasedFileManagement(this)); } bool _ownsFile(String path) => @@ -129,19 +125,3 @@ class MoorDriver { }); } } - -class _DriverBasedFileManagement implements IdeFileManagement { - final MoorDriver driver; - - _DriverBasedFileManagement(this.driver); - - @override - Uri fsPathToUri(String path) { - return driver._resourceProvider.pathContext.toUri(path); - } - - @override - Future waitUntilParsed(String path) { - return driver.waitFileParsed(path); - } -} diff --git a/moor_generator/lib/src/backends/plugin/plugin.dart b/moor_generator/lib/src/backends/plugin/plugin.dart index 77898835..36e1df6b 100644 --- a/moor_generator/lib/src/backends/plugin/plugin.dart +++ b/moor_generator/lib/src/backends/plugin/plugin.dart @@ -21,6 +21,7 @@ import 'package:moor_generator/src/backends/plugin/services/folding.dart'; import 'package:moor_generator/src/backends/plugin/services/navigation.dart'; import 'package:moor_generator/src/backends/plugin/services/outline.dart'; import 'package:moor_generator/src/backends/plugin/services/requests.dart'; +import 'package:moor_generator/src/services/ide/moor_ide.dart'; import 'logger.dart'; @@ -164,7 +165,7 @@ class MoorPlugin extends ServerPlugin return; } - final highlights = await driver.ide.highlight(path); + final highlights = await driver.ideServices.highlight(path); channel.sendNotification( plugin.AnalysisHighlightsParams(path, highlights).toNotification()); @@ -199,7 +200,7 @@ class MoorPlugin extends ServerPlugin Future handleEditGetAssists( plugin.EditGetAssistsParams parameters) async { final driver = _moorDriverOrFail(parameters.file); - final results = await driver.ide + final results = await driver.ideServices .assists(parameters.file, parameters.offset, parameters.length); return plugin.EditGetAssistsResult(results); @@ -221,3 +222,28 @@ class MoorPlugin extends ServerPlugin file, parameters.length, parameters.offset, resourceProvider); } } + +final _ideExpando = Expando(); + +extension on MoorDriver { + MoorIde get ideServices { + return _ideExpando[this] ??= + MoorIde(session, _DriverBasedFileManagement(this)); + } +} + +class _DriverBasedFileManagement implements IdeFileManagement { + final MoorDriver driver; + + _DriverBasedFileManagement(this.driver); + + @override + Uri fsPathToUri(String path) { + return driver.backend.provider.pathContext.toUri(path); + } + + @override + Future waitUntilParsed(String path) { + return driver.waitFileParsed(path); + } +} diff --git a/moor_generator/lib/src/cli/cli.dart b/moor_generator/lib/src/cli/cli.dart index 028259c1..b21e4d3b 100644 --- a/moor_generator/lib/src/cli/cli.dart +++ b/moor_generator/lib/src/cli/cli.dart @@ -9,7 +9,6 @@ import 'package:moor_generator/src/backends/common/driver.dart'; import 'package:moor_generator/src/cli/project.dart'; import 'commands/analyze.dart'; -import 'commands/debug_plugin.dart'; import 'commands/identify_databases.dart'; import 'commands/schema.dart'; import 'logging.dart'; @@ -38,7 +37,6 @@ class MoorCli { ) ..addCommand(AnalyzeCommand(this)) ..addCommand(IdentifyDatabases(this)) - ..addCommand(DebugPluginCommand(this)) ..addCommand(SchemaCommand(this)); _runner.argParser diff --git a/moor_generator/lib/src/cli/commands/debug_plugin.dart b/moor_generator/tool/debug_plugin.dart similarity index 79% rename from moor_generator/lib/src/cli/commands/debug_plugin.dart rename to moor_generator/tool/debug_plugin.dart index 1d96c861..79699572 100644 --- a/moor_generator/lib/src/cli/commands/debug_plugin.dart +++ b/moor_generator/tool/debug_plugin.dart @@ -1,44 +1,31 @@ -//@dart=2.9 +// @dart=2.9 import 'dart:async'; import 'dart:convert'; import 'dart:io'; import 'package:analyzer_plugin/channel/channel.dart'; import 'package:analyzer_plugin/protocol/protocol.dart'; +import 'package:args/args.dart'; import 'package:moor_generator/src/backends/plugin/plugin.dart'; -import '../cli.dart'; - -class DebugPluginCommand extends MoorCommand { - DebugPluginCommand(MoorCli cli) : super(cli) { - argParser.addOption( +void main(List args) { + final parser = ArgParser() + ..addOption( 'port', abbr: 'p', help: 'The port to use when starting the websocket server', defaultsTo: '9999', ); + final results = parser.parse(args); + + final port = int.tryParse(results['port'] as String); + if (port == null) { + print('Port must be an int'); + print(parser.usage); + return; } - @override - String get name => 'debug-plugin'; - - @override - String get description => 'Start the analyzer plugin on a websocket server'; - - @override - bool get hidden => true; - - @override - void run() { - final port = int.tryParse(argResults['port'] as String); - if (port == null) { - print('Port must be an int'); - printUsage(); - return; - } - - MoorPlugin.forProduction().start(_WebSocketPluginServer(port: port)); - } + MoorPlugin.forProduction().start(_WebSocketPluginServer(port: port)); } class _WebSocketPluginServer implements PluginCommunicationChannel {