Move builders and analyzer plugin into subfolder

This commit is contained in:
Simon Binder 2019-12-14 21:49:51 +01:00
parent 8661e0a28a
commit c69b7cb283
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
5 changed files with 100 additions and 98 deletions

View File

@ -1,6 +1,6 @@
builders:
moor_generator:
import: "package:moor_generator/moor_generator.dart"
import: "package:moor_generator/integrations/build.dart"
builder_factories: ["moorBuilder"]
build_extensions: {".dart": [".moor.g.part"]}
auto_apply: dependents

View File

@ -0,0 +1,4 @@
import 'package:build/build.dart';
import 'package:moor_generator/src/backends/build/moor_builder.dart';
Builder moorBuilder(BuilderOptions options) => MoorBuilder(options);

View File

@ -0,0 +1,92 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:isolate';
import 'package:analyzer/file_system/physical_file_system.dart';
import 'package:analyzer_plugin/channel/channel.dart';
import 'package:analyzer_plugin/protocol/protocol.dart';
import 'package:analyzer_plugin/starter.dart';
import 'package:moor_generator/src/backends/plugin/plugin.dart';
void start(List<String> args, SendPort sendPort) {
ServerPluginStarter(MoorPlugin(PhysicalResourceProvider.INSTANCE))
.start(sendPort);
}
class WebSocketPluginServer implements PluginCommunicationChannel {
final dynamic address;
final int port;
HttpServer server;
WebSocket _currentClient;
final StreamController<WebSocket> _clientStream =
StreamController.broadcast();
WebSocketPluginServer({dynamic address, this.port = 9999})
: address = address ?? InternetAddress.loopbackIPv4 {
_init();
}
Future<void> _init() async {
server = await HttpServer.bind(address, port);
print('listening on $address at port $port');
server.transform(WebSocketTransformer()).listen(_handleClientAdded);
}
void _handleClientAdded(WebSocket socket) {
if (_currentClient != null) {
print('ignoring connection attempt because an active client already '
'exists');
socket.close();
} else {
print('client connected');
_currentClient = socket;
_clientStream.add(_currentClient);
_currentClient.done.then((_) {
print('client disconnected');
_currentClient = null;
_clientStream.add(null);
});
}
}
@override
void close() {
server?.close(force: true);
}
@override
void listen(void Function(Request request) onRequest,
{Function onError, void Function() onDone}) {
final stream = _clientStream.stream;
// wait until we're connected
stream.firstWhere((socket) => socket != null).then((_) {
_currentClient.listen((data) {
print('I: $data');
onRequest(Request.fromJson(
json.decode(data as String) as Map<String, dynamic>));
});
});
stream.firstWhere((socket) => socket == null).then((_) => onDone());
}
@override
void sendNotification(Notification notification) {
print('N: ${notification.toJson()}');
_currentClient?.add(json.encode(notification.toJson()));
}
@override
void sendResponse(Response response) {
print('O: ${response.toJson()}');
_currentClient?.add(json.encode(response.toJson()));
}
}
/// Starts the plugin over a websocket service.
void main() {
MoorPlugin(PhysicalResourceProvider.INSTANCE).start(WebSocketPluginServer());
}

View File

@ -1,6 +1 @@
import 'package:build/build.dart';
import 'package:moor_generator/src/backends/build/moor_builder.dart';
export 'src/model/model.dart';
Builder moorBuilder(BuilderOptions options) => MoorBuilder(options);

View File

@ -1,92 +1,3 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:isolate';
import 'package:analyzer/file_system/physical_file_system.dart';
import 'package:analyzer_plugin/channel/channel.dart';
import 'package:analyzer_plugin/protocol/protocol.dart';
import 'package:analyzer_plugin/starter.dart';
import 'package:moor_generator/src/backends/plugin/plugin.dart';
void start(List<String> args, SendPort sendPort) {
ServerPluginStarter(MoorPlugin(PhysicalResourceProvider.INSTANCE))
.start(sendPort);
}
class WebSocketPluginServer implements PluginCommunicationChannel {
final dynamic address;
final int port;
HttpServer server;
WebSocket _currentClient;
final StreamController<WebSocket> _clientStream =
StreamController.broadcast();
WebSocketPluginServer({dynamic address, this.port = 9999})
: address = address ?? InternetAddress.loopbackIPv4 {
_init();
}
Future<void> _init() async {
server = await HttpServer.bind(address, port);
print('listening on $address at port $port');
server.transform(WebSocketTransformer()).listen(_handleClientAdded);
}
void _handleClientAdded(WebSocket socket) {
if (_currentClient != null) {
print('ignoring connection attempt because an active client already '
'exists');
socket.close();
} else {
print('client connected');
_currentClient = socket;
_clientStream.add(_currentClient);
_currentClient.done.then((_) {
print('client disconnected');
_currentClient = null;
_clientStream.add(null);
});
}
}
@override
void close() {
server?.close(force: true);
}
@override
void listen(void Function(Request request) onRequest,
{Function onError, void Function() onDone}) {
final stream = _clientStream.stream;
// wait until we're connected
stream.firstWhere((socket) => socket != null).then((_) {
_currentClient.listen((data) {
print('I: $data');
onRequest(Request.fromJson(
json.decode(data as String) as Map<String, dynamic>));
});
});
stream.firstWhere((socket) => socket == null).then((_) => onDone());
}
@override
void sendNotification(Notification notification) {
print('N: ${notification.toJson()}');
_currentClient?.add(json.encode(notification.toJson()));
}
@override
void sendResponse(Response response) {
print('O: ${response.toJson()}');
_currentClient?.add(json.encode(response.toJson()));
}
}
/// Starts the plugin over a websocket service.
void main() {
MoorPlugin(PhysicalResourceProvider.INSTANCE).start(WebSocketPluginServer());
}
// the plugin code used to live in this file, but it has moved. Kept for
// backwards compatibility, this file should be removed in moor 3.0.0
export 'package:moor_generator/integrations/plugin.dart';