startAnalyzer function

Future<void> startAnalyzer(
  1. List<String> args,
  2. SendPort sendPort, {
  3. List<SidecarBaseConstructor> constructors = const [],
})

Initializes the Sidecar analyzer from generated entrypoint

See analyzerIsolateStarter for server-side equivalent.

Implementation

Future<void> startAnalyzer(
  List<String> args,
  SendPort sendPort, {
  List<SidecarBaseConstructor> constructors = const [],
}) async {
  final root = Uri.file(args.first);
  final channel = CommunicationChannel(sendPort);
  final container = ProviderContainer(overrides: [
    activeTargetRootProvider.overrideWithValue(root),
    communicationChannelProvider.overrideWithValue(channel),
    ruleConstructorProvider.overrideWithValue(constructors),
    loggerProvider.overrideWith((ref) {
      final logger = Logger('sidecar-analyzer');
      final callback = logger.initialize(root, constructors);
      ref.onCancel(callback);
      return logger;
    }),
  ]);
  final logger = container.read(loggerProvider);

  await runZonedGuarded<Future>(() async {
    final analyzer = container.read(sidecarAnalyzerProvider);
    return analyzer.setup();
  }, (error, stack) {
    final context = container.read(activePackageProvider);
    logger.severe(error, null, stack);
    channel.handleError(context, error, stack);
    throw UnimplementedError('INVALID ERROR: $error $stack');
  }, zoneSpecification: ZoneSpecification(
    print: (self, parent, zone, line) {
      logger.info(line);
    },
  ));
}